Uploaded image for project: 'Commons FileUpload'
  1. Commons FileUpload
  2. FILEUPLOAD-165

Reading an uploaded file and returning that uploaded file in the exact same structure as the input file

    XMLWordPrintableJSON

Details

    • Task
    • Status: Closed
    • Critical
    • Resolution: Invalid
    • 1.2
    • 1.2.1
    • None
    • Windows XP Professional

    Description

      I am using response.getWriter().write(new String(uploadItem.get() to write the uploaded input file. The code works pretty well except the output is very disorganized. I want the output to look exactly the same as the input file, in structure, the same space, the same columns, a perfect copy. However the output file, is scrammbled all together. What can be done to alter this code so that the input file looks like the output file.

      Sample Code Below:

      package de.herbstcampus.server;

      import java.io.IOException;
      import java.util.List;

      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;

      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;

      public class FileUploadServlet extends HttpServlet {
      private static final long serialVersionUID = 1156467149259077140L;

      protected void doPost(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
      FileItem uploadItem = getFileItem(request);

      /*

      • Note this must be 'text/html', otherwise the onSubmitComplete(...)
      • won't work properly and the browser may open a save dialog.
        */
        response.setContentType("text/html");

      if (uploadItem == null)

      { response.getWriter().write("No data"); return; }

      else

      { response.getWriter().write(new String(uploadItem.get())); }

      }

      @SuppressWarnings("unchecked")
      private FileItem getFileItem(HttpServletRequest request) {
      FileItemFactory factory = new DiskFileItemFactory();
      ServletFileUpload upload = new ServletFileUpload(factory);

      try {
      List<FileItem> items = upload.parseRequest(request);

      for (FileItem item: items) {
      if (!item.isFormField()
      && "uploadFormElement".equals(item.getFieldName()))

      { return item; }

      }
      } catch (FileUploadException e)

      { return null; }

      return null;
      }

      Attachments

        Activity

          People

            jochen@apache.org Jochen Wiedmann
            bcubeb3 Barry Barrios
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Time Tracking

                Estimated:
                Original Estimate - 1h
                1h
                Remaining:
                Remaining Estimate - 1h
                1h
                Logged:
                Time Spent - Not Specified
                Not Specified