I'm trying to develop a file upload function on AWS.
I wrote a servlet to process post request, and the framework is shown below:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
boolean isMulti = ServletFileUpload.isMultipartContent(request);
if (isMulti) {
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
FileItemStream item = iter.next();
InputStream inputStream = item.openStream();
if (item.isFormField()) {
} else {
String fileName = item.getName();
if (fileName != null && fileName.length() > 0) {
//read stream of file uploaded
//store as a temporary file
//upload the file to s3
}
}
}
} catch (Exception e) {
}
}
response.sendRedirect("location of the result page");
}
I think these classes should be used to upload files. I tried but in S3, the size of the file is always 0 byte. Are there any other ways to upload file with multiple parts?
- InitiateMultipartUploadRequest
- InitiateMultipartUploadResult
- UploadPartRequest
- CompleteMultipartUploadRequest
I refer to the code http://docs.aws.amazon.com/AmazonS3/latest/dev/llJavaUploadFile.html