I am trying to fully implement a resumable file upload system in Java. The library I am using is resumable.js which sends chunks of a file to save as .part
and then merge them together at the end. When I receive the POST
request, in my doPost
method I take the request, save it into a HttpServletRequestWrapper
and then use that to get all the data I need. However, when saving the files as .part
I end up with them being empty and have a size of 0 bytes.
I have checked and it seems that the data is all there, but I can't seem to get the data to save. Is there something that I implemented incorrectly?
Here is a small snippet of the code I use to do this task:
public void doPost(HttpServletRequest request, HttpServletResponse response) {
try {
HttpServletRequestWrapper wrapReq = new HttpServletRequestWrapper(request);
BufferedReader reader = wrapReq.getReader();
/**
* Get some data from the BufferedReader
*/
if(ServletFileUpload.isMultipartContent(wrapReq)){
File mkd = new File(temp_dir);
if(!mkd.isDirectory())
mkd.mkdirs();
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
Iterator<FileItem> iter = upload.parseRequest(request).iterator();
OutputStream out;
out = new FileOutputStream(new File(dest_dir));
while(iter.hasNext()){
try {
FileItem item = iter.next();
IOUtils.copy(item.getInputStream(), out);
logger.debug("Wrote file " + resumableIdentifier + " with chunk number "
+ resumableChunkNumber + " to " + temp_dir);
out.close();
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
}
}
}
tryCreateFileFromChunks(temp_dir, resumableFileName, resumableChunkSize, resumableTotalSize);
} catch (Exception e) {
e.printStackTrace();
}
}
Where the tryCreateFileFromChunks()
method just checks if all the parts are there and merges them. It isn't the problem. The .part
files themselves are being stored empty.
So, did I handle this the wrong way? I've been struggling to get this working correctly.