1

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?

4
  • You want to upload into your webapplication so that you can download that file again? For some ServletEngines servletRequest.getSession().getServletContext().getRealPath("/") resolves to a "real" filesystem path (if app was deployed exploded and so on...) Commented Dec 3, 2015 at 14:02
  • @Jan Thank you, it work! Commented Dec 3, 2015 at 14:07
  • Its not ideal to store images in your webapp itself..instead just use a particular location outside of your webapp keep it configurable. Commented Dec 3, 2015 at 14:11
  • The points is only part of the deal - by accepting an answer you mark that issue "done" in a way, so others willing to help would not be drawn into reading and understanding your question, then reading all the comments only to find out after that, that it's already been taken care of. But the points are nice nonetheless. With now 17 you can even upvote answers ;-) Commented Dec 3, 2015 at 14:14

1 Answer 1

1

You want to upload into your webapplication so that you can download that file again? For some ServletEngines

servletRequest.getSession().getServletContext().getRealPath("/")

resolves to a "real" filesystem path (if app was deployed exploded and so on...)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.