Need feeback on resume file upload utility

45 Views Asked by At

I am trying to build a solution on resuming the file upload. If there is any problem occures on the initial file upload, the below code is suppose to resume the upload from the same point where it was failed.

I am first checking the bytes count which are already uploaded and then i am skipping those bytes from the inputStream and continuing with the upload.

        InputStream inputStream = null;
        OutputStream outputStream = null;
        String fileName = null;
        long uploadedBytes = 0;
        try {
            FileItemIterator fileItemIterator = servletFileUpload.getItemIterator(httpServletRequest);
            while (fileItemIterator.hasNext()) {

                FileItemStream fileItemStream = fileItemIterator.next();
                fileName = fileItemStream.getName();
                File file = new File("D:/filetest/output/" + fileName);
                if (file.exists()) {
                    uploadedBytes = file.length();
                    System.out.println("uploaded bytes " + uploadedBytes);
                    outputStream = new FileOutputStream(file, true);
                }else{
                    outputStream = new FileOutputStream(file);
                }
                inputStream = fileItemStream.openStream();

                IOUtils.skip(inputStream,uploadedBytes);
                IOUtils.copy(inputStream,outputStream);
            }
        } 

I tried this code in a .mkv file and the output file was working fine. Is this the correct way of resuming the file upload? Is there some more things i need to take care?

0

There are 0 best solutions below