Solved

JSP Jakarta Commons FileUpload- how to save to a directory?

Posted on 2007-03-18
7
848 Views
Last Modified: 2013-11-23
I am using apache jakarta commons upload library and am having a small issue with it.  I check my formfields for the proper type and then extract it.  My question is how Do i upload or save the file that is specified in the form from the previous page?  I am looking for a solid code example not a link to a tutorial that gives me pseudocode.  Any help would be appreciated.  Thanks.
0
Comment
Question by:RalphMercurio
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 15

Expert Comment

by:Javatm
ID: 18745686
Create a servlet to handle the request. This code is the same that you can find on the tutorial of apache commons upload site:

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
            String name = request.getParameter("user");
            System.out.println(name);
            
            // boolean isMultipart = ServletFileUpload.isMultipartContent(request);

            FileItemFactory factory = new DiskFileItemFactory();
            ServletFileUpload upload = new ServletFileUpload(factory);

            try {
                  List items = upload.parseRequest(request);
                  Iterator iter = items.iterator();

                  while (iter.hasNext()) {
                        FileItem item = (FileItem) iter.next();
                        String fileName = item.getName();
                        File theFile = new File(fileName);
                        if (item.isFormField()) {
                              File uploadedFile = new File("C:/YourFolder/"+ theFile.getName());
                              try {
                                    item.write(uploadedFile);
                                    insertToHibernate(name, uploadedFile.toString());
                                    redirectPage(request, response, uploadedFile
                                                .getAbsolutePath());
                              } catch (Exception e) {
                                    e.printStackTrace();
                              }
                        } else {
                              File uploadedFile = new File("C:/YourFolder/"+ theFile.getName());
                              try {
                                    item.write(uploadedFile);
                                    insertToHibernate(name, uploadedFile.toString());
                                    redirectPage(request, response, uploadedFile
                                                .getAbsolutePath());
                              } catch (Exception e) {
                                    e.printStackTrace();
                              }
                        }
                  }
            } catch (FileUploadException e1) {
                  // TODO Auto-generated catch block
                  e1.printStackTrace();
            }
      }
0
 
LVL 15

Expert Comment

by:Javatm
ID: 18745689
Remove this:
 insertToHibernate(name, uploadedFile.toString());
                                    redirectPage(request, response, uploadedFile
                                                .getAbsolutePath());
0
 
LVL 15

Expert Comment

by:Javatm
ID: 18745697
Don't forget to put the jar commons library on your path and import this together with the code above:
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 30

Expert Comment

by:Mayank S
ID: 18745722
We can't give complete working code without you coming up with some coding efforts of your own - its against site rules. Pls come up with some code and we will help you fix it if there are issues.
0
 

Author Comment

by:RalphMercurio
ID: 18745724
Here is my code:

for (int i = 0; i < fileItems.size(); i++) {
        FileItem fi = (FileItem) fileItems.get(i);
        if (!fi.isFormField() && "document".equals(fi.getFieldName())) {
            f.setFileLocation(fi.getName());
            fileInput = fi.getInputStream();
           
           
           
        } else if (fi.isFormField() && "User".equals(fi.getFieldName())) {
            a.setEmployeename(fi.getString());
        } else if (fi.isFormField() && "asset".equals(fi.getFieldName())) {
            a.setAsset(fi.getString());
            f.setAsset(fi.getString());
            n.setAsset(fi.getString());
        } else if (fi.isFormField() && "documenttype".equals(fi.getFieldName())) {
            f.setDocumentType(fi.getString());
        } else if (fi.isFormField() && "".equals(fi.getFieldName())) {

        } else if (fi.isFormField() && "ITUsers".equals(fi.getFieldName())) {
             f.setTechOpsEngineer(fi.getString());
        } else if (fi.isFormField() && "serial".equals(fi.getFieldName())) {
            a.setSerial(fi.getString());
        } else if (fi.isFormField() && "status".equals(fi.getFieldName())) {
            a.setStatus(fi.getString());
        } else if (fi.isFormField() && "notes".equals(fi.getFieldName())) {
            n.setNotes("{Assigned to " + a.getEmployeename() + " Status is now " + a.getStatus() + "} -- " + fi.getString());

        }
    }

Can I still do it with your code and if so which parts?
0
 
LVL 15

Accepted Solution

by:
Javatm earned 500 total points
ID: 18746117
In the while part:
while (iter.hasNext()) {
}
0
 

Author Comment

by:RalphMercurio
ID: 18855136
thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question