Hi,
I can upload my files outside of the WEB-INF folder and to the path specified but am unable to upload them into the WEB-INF folder. I hope someone can point me in the right direction. Thanks!!
IDE : Netbeans
Server : Tomacat
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
boolean isMultiPart= ServletFileUpload.isMultipartContent(request); // find the request object enctype
//String uploadDirectory = "D:\\upload\\images";
String uploadDirectory="/WEB-INF/data"; // location to upload the file
ServletContext sc = getServletContext();
String realPath = sc.getRealPath(uploadDirectory);
System.out.println(realPath);
FileItemFactory fileFactory = new DiskFileItemFactory(100,new File(uploadDirectory)); //100 is the bytes to hold in memory beyond which write the file to the directory mentioned.
ServletFileUpload serFileUpload = new ServletFileUpload(fileFactory); //commons API to handle multipart encoding type
try {
List fileItems = serFileUpload.parseRequest(request);
Iterator iter = fileItems.iterator();
while (iter.hasNext()) {
FileItem item = (FileItem) iter.next();
if (item.isFormField()) { // identifies the normal form field , false when it identifies the upload field
//simply process the form fields
System.out.println("Text Value -->"+item.getString());
} else {
try {
String fileName= null;
if (item.getFieldName().equals("imageUpload")){
if(item.getName() != null){
fileName=FilenameUtils.getName(item.getName());//FilenameUtils - commons.io API
item.write(new File(uploadDirectory+File.separator+fileName)); // writes the file to the upload dir
request.setAttribute("fileAddress",uploadDirectory+File.separator+fileName);
RequestDispatcher rD = request.getRequestDispatcher("UnZipImage.do");
rD.forward(request, response);
}
}
else{
RequestDispatcher rD = request.getRequestDispatcher("UploadForm.view");
rD.forward(request, response);
}
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}