2

I'm writing a servlet that receives an xml file from the client and works with it.

My problem is, that in the servletinputstream (which i get with: request.getInputStream()) is some upload information at the beginning and at the end:

-----------------------------186292285129788
Content-Disposition: form-data; name="myFile"; filename="TASKDATA - Kopie.XML"
Content-Type: text/xml

<XML-Content>

-----------------------------186292285129788--

Is there a smart solution to cut those lines away from the servletinputstream?

greetings

Simon Lenz
  • 2,732
  • 5
  • 33
  • 39

2 Answers2

1

If your issue is that you want to read your file in streaming (for performance), check this link http://commons.apache.org/proper/commons-fileupload/streaming.html

(From the link):

// Check that we have a file upload request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload();

// Parse the request
FileItemIterator iter = upload.getItemIterator(request);
while (iter.hasNext()) {
    FileItemStream item = iter.next();
    String name = item.getFieldName();
    InputStream stream = item.getInputStream();
    if (item.isFormField()) {
        System.out.println("Form field " + name + " with value "
            + Streams.asString(stream) + " detected.");
    } else {
        System.out.println("File field " + name + " with file name "
            + item.getName() + " detected.");
        // Process the input stream
        ...
    }
}
Shil Nevado
  • 716
  • 1
  • 11
  • 30
Zied Hamdi
  • 2,400
  • 1
  • 25
  • 40
1

That's a multipart/form-data header (as specified in RFC2388). Grab a fullworthy multipart/form-data parser rather than reinventing your own. Apache Commons FileUpload is the defacto standard API for the job. Drop the required JAR files in /WEB-INF/lib and then it'll be as easy as:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items) {
            if (item.isFormField()) {
                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                String fieldname = item.getFieldName();
                String fieldvalue = item.getString();
                // ... (do your job here)
            } else {
                // Process form file field (input type="file").
                String fieldname = item.getFieldName();
                String filename = FilenameUtils.getName(item.getName());
                InputStream filecontent = item.getInputStream();
                // ... (do your job here)
            }
        }
    } catch (FileUploadException e) {
        throw new ServletException("Cannot parse multipart request.", e);
    }

    // ...
}

Once again, don't reinvent your own. You really don't want to have a maintenance aftermath.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555