我試圖使用'streaming'Apache Commons文件上傳API上傳大文件。SpringBoot:使用Apache Commons FileUpload上傳大文件流文件
我使用Apache Commons File Uploader而不是默認的Spring Multipart上傳器的原因是,當我們上傳非常大的文件大小(〜2GB)時,它失敗。我正在開發一個GIS應用程序,這種文件上傳很常見。
爲我的文件上傳控制器完整的代碼如下:
@Controller
public class FileUploadController {
@RequestMapping(value="/upload", method=RequestMethod.POST)
public void upload(HttpServletRequest request) {
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
// Inform user about invalid request
return;
}
//String filename = request.getParameter("name");
// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();
// Parse the request
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("Form field " + name + " with value " + Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name " + item.getName() + " detected.");
// Process the input stream
OutputStream out = new FileOutputStream("incoming.gz");
IOUtils.copy(stream, out);
stream.close();
out.close();
}
}
}catch (FileUploadException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
@RequestMapping(value = "/uploader", method = RequestMethod.GET)
public ModelAndView uploaderPage() {
ModelAndView model = new ModelAndView();
model.setViewName("uploader");
return model;
}
}
麻煩的是,getItemIterator(request)
始終返回沒有任何元素的迭代器(即iter.hasNext()
)總是返回false
。
我application.properties文件如下:
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.url=jdbc:postgresql://localhost:19095/authdb
spring.datasource.username=georbis
spring.datasource.password=asdf123
logging.level.org.springframework.web=DEBUG
spring.jpa.hibernate.ddl-auto=update
multipart.maxFileSize: 128000MB
multipart.maxRequestSize: 128000MB
server.port=19091
的JSP視圖的/uploader
如下:
<html>
<body>
<form method="POST" enctype="multipart/form-data" action="/upload">
File to upload: <input type="file" name="file"><br />
Name: <input type="text" name="name"><br /> <br />
Press here to upload the file!<input type="submit" value="Upload">
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</body>
</html>
什麼可能我是做錯了什麼?
你是否禁用了彈簧multipart支持,否則你的解決方案將無法工作,並且Spring已經解析了請求。將所有'multipart'屬性替換爲一個'multipart.enabled = false'來禁用默認處理。 –
我沒有做任何特定的禁用彈簧多部分支持。我嘗試在我的'application.properties'文件中添加'multipart.enabled = false'。但是,一旦我這樣做,我每次上傳時都會收到'405'請求方法'POST'not supported'錯誤。 – balajeerc
這將表明一個錯誤的映射或張貼到錯誤的url ...啓用調試日誌記錄,並查看您要發佈到哪個URL以及您的控制器方法匹配的URL。 –