'Streaming' Apache Commons File Upload APIを使用して大きなファイルをアップロードしようとしています。SpringBoot:Apache Commonsを使用した大規模なストリーミングファイルアップロードFileUpload
デフォルトのSpring MultipartアップローダではなくApache Commons File Uploaderを使用しているのは、非常に大きなファイルサイズ(〜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
を返します。私は間違って何をしているのかもしれない
<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>
次のように/uploader
ため
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図であり、以下のよう
マイapplication.propertiesファイルがありますか?
あなたのソリューションは動作しません。また、Springはすでにリクエストを解析しています。すべての 'multipart'プロパティを単一の' multipart.enabled = false'に置き換え、デフォルトの処理を無効にします。 –
私はspring multipartサポートを無効にするために何もしていませんでした。私は 'application.properties'ファイルに' multipart.enabled = false'を追加しようとしました。しかし、これを行うと、アップロードを行うたびに '405:リクエストメソッド 'POST'がサポートされていないというエラーが出ます。 – balajeerc
マッピングが間違っているか、間違ったURLに投稿された可能性があります...デバッグロギングを有効にして、あなたが投稿しているURLとコントローラメソッドが一致するURLを確認してください。 –