禁用掉spring boot默认开启的上传功能配置
spring.http.multipart.enabled=false
利用commons-fileupload 直接调用API进行操作
@Controller
@RequestMapping("fileManager/")
public class FileManager{
@RequestMapping(value="upload", method=RequestMethod.POST)
public void upload(HttpServletRequest request) {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
return;
}
String filename = request.getParameter("name");
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("字段名称:" + name + " 值:" + Streams.asString(stream) );
} else {
OutputStream out = new FileOutputStream("incoming.gz");
IOUtils.copy(stream, out);
stream.close();
out.close();
}
}
}catch (FileUploadException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}