1

Possible Duplicate:
How to upload files to server using JSP/Servlet?

I'm implementing a fileupload servlet that is used alongside resumable.js

Everytime I try to read a file, I either get a NoSuchElement exception or a NumberFormatException with a string inside the file I'm reading. I'm sure I made a hiccup somewhere, but can't seem to tell

Here's a snippet of what I use to read request and write to file

if(ServletFileUpload.isMultipartContent(request)){
            DiskFileItemFactory factory = new DiskFileItemFactory();
            factory.setRepository(new File(temp_dir));
            ServletFileUpload upload = new ServletFileUpload(factory);

            Iterator<FileItem> iter = upload.parseRequest(request).iterator();
            FileItem item = iter.next();
            OutputStream out;

            try {

                out = new FileOutputStream(new File(dest_dir));
                IOUtils.copy(item.getInputStream(), out);
                logger.debug("Wrote file " + resumableIdentifier + " with chunk number "
                        + resumableChunkNumber + " to " + temp_dir);

            } catch (FileNotFoundException fnfe) {

                fnfe.printStackTrace();

            }
        }

Did I do something wrong that is making the code actually read and interpret the contents of the file?

Community
  • 1
  • 1
Fasih Awan
  • 1,891
  • 4
  • 17
  • 24
  • possible duplicate of [How to upload files to server using JSP/Servlet?](http://stackoverflow.com/a/2424824) – BalusC Jan 25 '13 at 20:10

1 Answers1

2

You have to iterate over the FileItems. Right after this line:

Iterator<FileItem> iter = upload.parseRequest(request).iterator();

You should have something like this:

File dir = new File(dest_dir);
if (!dir.isDirectory()) dir.mkdirs();
while(iter.hasNext()) {
    FileItem item = iter.next();

Also do not forget to close the output stream for every file item.

out = new FileOutputStream(new File(dir, item.getName()));
IOUtils.copy(item.getInputStream(), out);
out.close();
jdb
  • 4,419
  • 21
  • 21
  • 1
    Where is the OP missing that code block? Please provide more context to your suggestion. – Jeff Jan 25 '13 at 20:36
  • Ah, I thought that I wouldn't need to since it was one file at a time but I guess I forgot its a multi-part request so it would have to write all the data from the POST that it needs to. Thanks. Now just to do a lot of debugging to get the rest of it fixed – Fasih Awan Jan 25 '13 at 20:47
  • @jdb I was just wondering, should I keep the `out = new OutputStream(...);` in the while loop or call it once before the while loop? Also, does `IOUtils.copy(inputStream,outputStream)` append or re-write? The files saved on my drive, but they're all 0 bytes – Fasih Awan Jan 25 '13 at 20:58
  • You should inspect your data. Dump the http request in plain text for example. – jdb Jan 25 '13 at 21:15
  • @jdb Looking at the requests in Developer Tools on chrome shows the correct data, and having a breakpoint in Eclipse and seeing the data for the request variable it looks like the data is coming in, but maybe its not writing it correctly. – Fasih Awan Jan 25 '13 at 21:31
  • 1
    Look at the code above for more detailes. It is still far from perfect but it will help you understand. – jdb Jan 25 '13 at 21:50
  • Sorry for the extended questions. I was checking the size of the `upload.parseRequest(request);` and it's apparently 0 everytime. Could this be because I had already used the `HttpServletRequest` object when checking for `ServletFileUpload.isMultipartContent()`? – Fasih Awan Jan 25 '13 at 22:32
  • Looks like you can try without it. Here is one example. http://www.servletworld.com/servlet-tutorials/servlet-file-upload-example.html – jdb Jan 25 '13 at 23:13