1

Help needed!

I am using this article to upload a file in my project http://www.theserverside.com/news/1365153/HttpClient-and-FileUpload

Using this article I am able to upload the file but the file gets uploaded in my /home/parinati/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/CMTint/

i.e. The eclipse tomcat webapps folder.

But I want my file to be uploaded in my project folder. When I do request.getContextPath() the output is CMTint/ and I want the file to get uploded into CMTint/Uploads/

My upload code is -->

 System.out.println("Content Type ="+request.getContentType());

    DiskFileUpload fu = new DiskFileUpload();
    // If file size exceeds, a FileUploadException will be thrown
    fu.setSizeMax(1000000);

    List fileItems = fu.parseRequest(request);
    Iterator itr = fileItems.iterator();

    while(itr.hasNext()) {
      FileItem fi = (FileItem)itr.next();

      //Check if not form field so as to only handle the file inputs
      //else condition handles the submit button input
      if(!fi.isFormField()) {
        System.out.println("nNAME: "+fi.getName());
        System.out.println("SIZE: "+fi.getSize());
        //System.out.println(fi.getOutputStream().toString());
        File fNew= new File(application.getRealPath("/"), fi.getName());

        System.out.println(fNew.getAbsolutePath());
        fi.write(fNew);
      }
      else {
        System.out.println("Field ="+fi.getFieldName());
      }
    }

Can you suggest what changes are required. Any help will be greatly appreciated.

Thanks in advance Sweta

2 Answers 2

2

I never like using the "/", what happens if you change OS. I would do something like this

String filePath = String.format("%s%s%s", request.getContextPath(), File.separatorChar, "Uploads");

You can then add a nice log debug message logging the path being used...

File fNew= new File(filePath);

I would extract that into a method too

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

Comments

0

How's about

File fNew= new File(application.getRealPath("/") + "Uploads", fi.getName());

?

As for the project folder - that's the matter of your Eclipse bundled Tomcat configuration (webapps folder is possibly configurable).

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.