JSP Writing to server directory

829 Views Asked by At

So I am using resumable.js to upload files to a server.

The directory that I want to save to is something like

/dir/files/upload/

Obviously just made up, but this directory has user permissions to write to it. I am using JSP to listen to the POST request that resumable.js makes, and writing the

.part

files to that directory. Sample listener:

<%          if(request.getMethod().equals("POST") && request.getParameter("resumableFilename") != null){
               long chunkSize = StringUtils.isEmpty(request.getParameter("resumableChunkSize"))? 0:Long.parseLong(request.getParameter("resumableChunkSize"));
               String fileName = request.getParameter("resumableFilename");
               long totalSize = StringUtils.isEmpty(request.getParameter("resumableTotalSize"))? 0:Long.parseLong(request.getParameter("resumableTotalSize"));
               String temp_dir = "/dir/files/upload/"+request.getParameter("resumableIdentifier");//Add in user_id
               String dest_dir = temp_dir+fileName+".part"+request.getParameter("resumableChunkNumber");
               File fDir = new File(temp_dir);
               fDir.mkdirs();
               if(ServletFileUpload.isMultipartContent(request)){
                   DiskFileItemFactory factory = new DiskFileItemFactory(); 
                   factory.setRepository(new File(temp_dir));                          ServletFileUpload upload = new ServletFileUpload(factory);
                   List items = upload.parseRequest(request);
                   ArrayListIterator iter = (ArrayListIterator)items.iterator();
                   FileItem item = (FileItem)iter.next();
                   File fileWithNewDir = new File(dest_dir);
                   item.write(fileWithNewDir);  // write file to dest_dir (fileName.part*CHUNK_NUM*)
                   }
                }
%>

The script is hosted on

www.site.com/pubs/res.jsp

According to the JS itself for resumable, the process of uploading it gets completed, however, a new directory is not made at all. I know it's not the write permissions, so it must be something else.

Here is my call in javascript for a new resumable object

var resume = new Resumable({
    target:"res.jsp",
    resumableChunkSize: 1*1024*1024,
    simultaneousUploads: 3,
    testChunks: false,
    throttleProgressCallbacks: 1
    });

It seems to be hitting the jsp file, but nothing is happening.

I followed Apache's fileupload page in order to implement that listener, but maybe I went wrong at some point.

Apache's FileUpload

Resumable.js

1

There are 1 best solutions below

5
On

Location of the directory matters. It has to be within the context of the WAR. You cannot write to any location outside the context of the container. If you look at the log you may be abe to see the error message which can explain this.