spring.application.name=pgm
# multipart config
# enable multipart uploads
spring.servlet.multipart.enabled=true
# max file-size
spring.servlet.multipart.max-file-size=50MB
logging.level.org.springframework.web=DEBUG
server.tomcat.max-swallow-size=50MB
主要应用程序文件:
package com.mono.pgm;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class PgmApplication
{
public static void main(String[] args)
{
SpringApplication.run(PgmApplication.class, args);
}
}
控制器:
package com.mono.pgm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController
public class controller
{
@Autowired
private fileUploadhelper helper;
@GetMapping("/")
public String hi()
{
return "hi!";
}
@PostMapping("/upload-file")
public ResponseEntity<String> uploadFile(@RequestParam("theoneofall") MultipartFile file)
{
for(int i=0; i<15;i++)System.out.print('-');
System.out.println(' ');
System.out.println(file.getOriginalFilename());
System.out.println(file.getSize());
System.out.println(file.getContentType());
System.out.println(file.getName());
// file upload code
try {
boolean f = helper.uploadFile(file);
if(f)return ResponseEntity.ok("Successful! upload");
}catch(Exception e){
e.printStackTrace();
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Something went wrong");
}
}
upload类:
package com.mono.pgm;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.FileOutputStream;
import java.io.InputStream;
@Component
public class fileUploadhelper
{
public String uploadDir = "C:\\Users\\comma\\Desktop\\projects\\pgm\\src\\main\\resources\\static\\images";
public boolean uploadFile(MultipartFile file)
{
boolean f = false;
try{
InputStream is = file.getInputStream();
byte[] data = new byte[is.available()];
is.read(data);
FileOutputStream fos = new FileOutputStream(uploadDir + "\\" + file.getOriginalFilename());
fos.write(data);
fos.flush();
fos.close();
f=true;
// Files.copy(file.getInputStream, Paths.get(uploadDir+"\\"+file.getOriginalName()),StandardCopyOption.ReplaceExisting);
}catch(Exception e){
e.printStackTrace();
}
return f;
}
}
i我问了GPT,它告诉我,由于Tomcat中的最大尺寸限制,它可能已经发生了,我尝试过,但没有起作用。
application.properties:
add