0

I am trying to make a servlet to upload a file to a temporary location. When I build the file path everything seems alright.

String uploadFilePath = System.getProperty("java.io.tmpdir") + File.separator + UPLOAD_DIR;

and I get the absolute path: C:\Users\victor\AppData\Local\Temp\uploads

but when I call

part.write(uploadFilePath + File.separator + fileName);

I get: C:\Users\victor\GlassFish_Server\glassfish\domains\domain\generated\jsp\pcpweb\ C:\Users\victor\AppData\Local\Temp\uploads

and an java.io.FileNotFoundException afterwards, far enough I guess.

So "write" is completing my path. I there a way to avoid this? Thanks.

Using: glassfish and netbeans

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");

    // constructs path of the directory to save uploaded file
    String uploadFilePath = System.getProperty("java.io.tmpdir") + File.separator + UPLOAD_DIR;

    // creates the save directory if it does not exists
    File fileSaveDir = new File(uploadFilePath);
    if (!fileSaveDir.exists()) {
        fileSaveDir.mkdirs();
    }
    System.out.println("Upload File Directory=" + fileSaveDir.getAbsolutePath());

    String fileName = null;
    //Get all the parts from request and write it to the file on server
    for (Part part : request.getParts()) {
        fileName = getFileName(part);
        part.write(uploadFilePath + File.separator + fileName);
    }

    request.setAttribute("message", fileName + " File uploaded successfully!");
    getServletContext().getRequestDispatcher("gerencia.jsp").forward(
            request, response);
}
Victor
  • 3,520
  • 3
  • 38
  • 58

1 Answers1

1

The javadoc for Part.write() mentions that:

The file is created relative to the location as specified in the MultipartConfig

My understanding is that for Glassfish, if you don't specify the location, it defaults to the value of:

getServletContext().getAttribute("javax.servlet.context.tempdir")

Which is probably the extra path that you see.

Try specifying the location in the @MultipartConfig annotation, e.g.:

@MultipartConfig(location="/somepath")

You could set the location to the value of the temp dir, and then just pass the bare filename to the write() method.

David Levesque
  • 22,181
  • 8
  • 67
  • 82
  • I will test and I post here again. but I have one question though, is there a way to pass an absolute path at run-time? it seems it need to be hard-coded and using something like System.getProperty("java.io.tmpdir") is not an option. – Victor May 03 '14 at 22:24
  • 1
    See this question: http://stackoverflow.com/q/14044793/1464763. The short answer seems to be no. – David Levesque May 03 '14 at 22:37
  • this seems to work, at leat I dont hae the exception any more. but do I get the location at runtime to process the file. – Victor May 03 '14 at 22:37
  • 1
    You can use the `getInputStream()` method of the Part object to process the file. Or you could try to retrieve the context attribute mentioned in my answer. – David Levesque May 03 '14 at 22:41