1

I am developing a GWT application which, among its other functions, permits the user to upload an image file and to store it on the server. So far, that's what I've done..

SERVLET

public class ImageUploadService extends HttpServlet {

private static final int MAX_FILE_SIZE = 1 * 1024 * 1024;

public void doPost(HttpServletRequest request, HttpServletResponse response) {

    wlog("INFO: è partita la servlet");

    if (!ServletFileUpload.isMultipartContent(request))
        wlog("ERR: non è multipart!");
    ServletFileUpload fileUpld = new ServletFileUpload();

    try {
        wlog("INFO: itero file");
        FileItemIterator fileIt = fileUpld.getItemIterator(request);
        while (fileIt.hasNext()) {

            wlog("INFO: trovato file");
            FileItemStream fileStream = fileIt.next();
            BufferedInputStream in = new BufferedInputStream(
                    fileStream.openStream(), 4096);
            BufferedOutputStream out = new BufferedOutputStream(
                    new FileOutputStream("immagineSegnalazione.jpg"));

            byte[] buf = new byte[MAX_FILE_SIZE];
            int byteRead;
            while ((byteRead = in.read(buf, 0, MAX_FILE_SIZE)) >= 0) {
                out.write(buf, 0, byteRead);
            }
            in.close();
            out.flush();
            out.close();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

private void wlog(String s) {
    System.out.println("UPLOAD SERVLET " + s);
}
}

MODULE ON CLIENT SIDE

            [...]

        PopupPanel inserisciSegnalazionePopup = new PopupPanel();
    final FormPanel uploadForm = new FormPanel();
    uploadForm.setEncoding(FormPanel.ENCODING_MULTIPART);
    uploadForm.setMethod(FormPanel.METHOD_POST);
    inserisciSegnalazionePopup.setAutoHideEnabled(true);
    VerticalPanel holder = new VerticalPanel();
    holder.add(new Label("se puoi, allega una foto della segnalazione"));
    final FileUpload fu = new FileUpload();
    uploadForm.add(fu);
    holder.add(uploadForm);
    uploadForm.setAction(GWT.getModuleBaseURL() + "imageUpload");
    Button inviaBtn = new Button("INVIA SEGNALAZIONE");
    inviaBtn.addClickHandler(new ClickHandler() {

        @Override
        public void onClick(ClickEvent event) {
            // TODO check file is image and size and other stuff


            uploadForm.submit();
        }

    });
    holder.add(inviaBtn);

            [...]

..plus I've rightly made the changes needed on web.xml The Servlet is correctly called and the method doPost() starts, but the FileItemIterator is always empty, as if there were no files at all.. Can someone guess what's wrong? I can't really see where's the mistake Thank you in advance

Filippo
  • 361
  • 1
  • 5
  • 16

3 Answers3

1

just guessing I would say the request is parsed somewhere befor you use it. Try taking a look at that question and the answer to it, it seems to like it was nearly the same problem.

Sarajog

Community
  • 1
  • 1
Sarajog
  • 164
  • 16
  • The request is never parsed before the part of code I've shown..I tried to change the code as suggested in the link you provided me, but the iterator still will be empty.. – Filippo Aug 19 '13 at 16:01
1

Have you tried this ??

Iterator<FileItem> iterator = upload.parseRequest(request).iterator();
Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

The solution is... Simply add .setName() to the FileUpload widget

Filippo
  • 361
  • 1
  • 5
  • 16