I am making simple upload servlet.
My code is:
private final int FILE_LENGHT = 150;
private final String UPLOAD_DIRECTORY = "/img/";
.
.
.
.
.
@Override
public void uploadFile(HttpServletRequest request) {
//process only if its multipart content
if (ServletFileUpload.isMultipartContent(request)) {
try {
List<FileItem> multiparts = new ServletFileUpload(
new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : multiparts) {
if (!item.isFormField()) {
File f = new File(item.getName());
String ex = suo.getFileExtension(f);
String name = suo.randomString(FILE_LENGHT) + "." + ex;
item.write(new File(UPLOAD_DIRECTORY + File.separator + name));
}
}
//File uploaded successfully
request.setAttribute("message", "File Uploaded Successfully");
} catch (Exception ex) {
request.setAttribute("message", "File Upload Failed due to " + ex);
}
} else {
request.setAttribute("message",
"Sorry this Servlet only handles file upload request");
}
}
I try to upload img to WEB-PAGES folder. It is possible?
Structure of this folder is:
Web Pages
WEB-INF
img
upload.jsp
But i get allways exeption:
File Upload Failed due to java.io.FileNotFoundException:
The system can not find the path
The code it works for path C:\uploadFolder but not for upload to Web Pages folder.
Why?