views:

663

answers:

2

I need to upload a file using Apache fileupload with ProgressListener but alongwith that I also need to show the progressbar for the upload status.

Actual requirement is I just need to parse a local XML file parse the xml into appropriate objects and put them in Database. Do I really need to upload the file to server to get it parsed. As I am getting exception like file not found on remote server while it runs fine on my local m/c.

Any quick help would be appreciated.

Thanks in advance !!!

+1  A: 

If you have access to the server side, I advise to debug the upload process. The exception suggests that you want to open the file on the server based on the uploaded file name. On your local machine this works, because it runs on the same file system. On the server side, the Apache FileUpload receives binary data, which needs to be extracted from the request data stream:


@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
  if (ServletFileUpload.isMultipartContent(request)) {
    FileItemFactory factory = new DiskFileItemFactory(Integer.MAX_VALUE, null);
    ServletFileUpload upload = new ServletFileUpload(factory);
    List items = upload.parseRequest(request);
    for (FileItem item : items) {
       byte[] data = item.get();
       // do something with the binary data
    }
  } else {
    System.err.println("Not a multipart/form-data");
  }
}

And also you need the form to be:

<form name='frm' method="POST" action='UploadServlet' 
id="frm" enctype="multipart/form-data">
kd304
A: 

From your description it sounds like your servlet is trying to read the file from the filesystem itself, based on the filename submitted in the form. This isn't going to work if the servlet is running on a different machine to where the file is.

Make sure your servlet is getting the file contents from the fileupload API, not from the local filesystem.

skaffman