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);
}