import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.util.logging.Logger; /** * ClassName: FileUploadController<br/> * Description:文件上传 <br/> * date: 2018/11/30 10:06 AM<br/> * * @author chengluchao * @since JDK 1.8 */ @RestController public class FileUploadController { public final static Logger logger = Logger.getLogger("FileUploadController"); //文件大小限制 //spring.http.multipart.maxFileSize: 200MB @PostMapping(value = "/fileUpload") @ResponseBody public Object fileUpload(MultipartFile file) throws Exception { logger.info("文件名=" + file.getOriginalFilename()); file.transferTo(new File("/mnt/" + file.getOriginalFilename())); return "success"; } }
调用
<html> <body> <form action="http://127.0.0.1:9010/fileUpload" method="POST" enctype="multipart/form-data"> <input type="file" name="file"/> <input type="submit" name="submit"/> </form> </body> </html>
配置
#单个大小
spring.http.multipart.maxFileSize: 200MB
#总大小
spring.http.multipart.maxRequestSize: 200MB