views:

25

answers:

1

I am trying to create a upload servlet that handles enctype="multipart/form-data" from a form. The file I am trying to upload is a zip. However, I can upload and read the file on localhost, but when I upload to the server, I get a "File not found" error when I want to upload a file. Is this due to the Struts framework that I am using? Thanks for your help. Here is part of my code, I am using FileUpload from http://commons.apache.org/fileupload/using.html

I have changed to using ZipInputStream, however, how to I reference to the ZipFile zip without using a local disk address (ie: C://zipfile.zip). zip is null because its not instantiated. I will need to unzip and read the zipentry in memory, without writing to the server.

For the upload servlet: > private ZipFile zip; private CSVReader reader; boolean isMultipart = ServletFileUpload.isMultipartContent(request); if(isMultipart){ DiskFileItemFactory factory = new DiskFileItemFactory();

        ServletFileUpload upload = new ServletFileUpload(factory);
       List <FileItem> items = upload.parseRequest(request);
        Iterator iter = items.iterator();
        while (iter.hasNext()) {
            //Iterating through the uploaded zip file and reading the content
            FileItem item = (FileItem) iter.next();

             ZipInputStream input = new ZipInputStream(item.getInputStream()); 
             ZipEntry entry = null;
             while (( entry= input.getNextEntry()) != null) {
               ZipEntry entry = (ZipEntry) e.nextElement();
               if(entry.getName().toString().equals("file.csv")){
                   //unzip(entry)
               }

               }
            }


  public static void unzip(ZipEntry entry){
        try{
            InputStream inputStream = **zip**.getInputStream(entry);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            reader = new CSVReader(inputStreamReader);
        }
        catch(Exception e){
            e.printStackTrace();
        }
    }

<

A: 

Here,

zip = new ZipFile(new File(fileName));

You're assuming that the local disk file system at the server machine already contains the file with exactly the same name as it is at the client side. This is a wrong assumption. That it worked at localhost is obviously because both the webbrowser and webserver "by coincidence" runs at physically the same machine with the same disk file system.

Also, you seem to be using Internet Explorer as browser which incorrectly includes the full path in the filename like C:/full/path/to/file.ext. You shouldn't be relying on this browser specific bug. Other browsers like Firefox correctly sends only the file name like file.ext, which in turn would have caused a failure with new File(fileName) (which should have helped you to spot your mistake much sooner).

To fix this "problem", you need to obtain the file contents as InputStream by item.getInputStream():

ZipInputStream input = new ZipInputStream(item.getInputStream());
// ...

Or to write it to disk by item.write(file) and reference it in ZipFile:

File file = File.createTempFile("temp", ".zip");
item.write(file);
ZipFile zipFile = new ZipFile(file);
// ...

Note: don't forget to check the file extension beforehand, else this may choke.

BalusC
thanks for your reply, but I am stuck at referencing the ZipFile zip. I can't get it to work without referencing the actual file, which will not work on the actual server. I have edited the codes above.
JustToHelp
Write it to disk and then reference the `File` as `ZipFile`.
BalusC
do you mean ZipFile zip = (ZipFile) item.write(file)? This file will reference to where?
JustToHelp
I updated the answer with an example.
BalusC
oh yes creating a temp file, I missed that out. Thanks!
JustToHelp