I'm doing a simple file upload in jsp. And i seem to have been stopped by this simple path issue. I'm developing on windows but will propably deploy on a linux machine. so i have the tmpdir and the finaldir under mywebsite/tempfiledir and mywebsite/finalfiledir
so i use this code (snippet of the servlet)
public class SyncManagerServlet extends HttpServlet {
private static final String TMP_DIR_PATH = "/tempfiledir";
private File tempDir;
private static final String DESTINATION = "/finalfiledir";
private File destinationDir;
public void init(ServletConfig config){
try {
super.init(config);
tempDir = new File(getAbsolute(TMP_DIR_PATH));
if(!tempDir.isDirectory()){
throw new ServletException(TMP_DIR_PATH + " is not a Directory");
}
destinationDir = new File(getAbsolute(DESTINATION));
if(!destinationDir.isDirectory()){
throw new ServletException(DESTINATION + " is not a Directory");
}
} catch (ServletException ex) {
Logger.getLogger(OrliteSyncManagerServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String email = request.getParameter("email");
String path = request.getContextPath();
DiskFileItemFactory fileItemFactory = new DiskFileItemFactory();
fileItemFactory.setRepository(tempDir);
ServletFileUpload uploadHandler = new ServletFileUpload(fileItemFactory);
try {
List items = uploadHandler.parseRequest(request);
Iterator itr = items.iterator();
while(itr.hasNext()){
FileItem item = (FileItem) itr.next();
if( item.isFormField() && item != null ){
out.println("<html>");
out.println("<head>");
out.println("<body>");
out.println("your email: " + item.getString() + " has been submited and context path is "+ request.getContextPath() );
out.println("</body>");
out.println("</head>");
out.println("</html>");
} else {
out.println("the uploaded file name is : " + item.getName());
out.println("content type is : " + item.getContentType());
out.println("Size is : " + item.getSize());
File file = new File(destinationDir, FilenameUtils.getName(item.getName()));
item.write(file);
}
public String getAbsolute(String relativepath){
return getServletContext().getRealPath(relativepath);
}
//......
}
i'm having this exception
java.io.FileNotFoundException: D:\WORK\java\netbeansProject\Projects\mywebsite-webapp\target\mywebsite\finalfiledir (Access is denied)
i can't figure out why the relative path is failing. I've noticed in a lot of samples online people uses full path for the tempdir. So in my case where i have to worry about linux on deployment, what's the workaround?But first i'ld like to understand why the path i've given is wrong.
Thanks for reading this! so 2 issues here
1 how to solve this path immediate issue?
2 how to do it in a more portable way (with linux privileges in mind)?
thanks!