views:

122

answers:

4

I have one problem that is to upload file. It is working perfectly on my computer but fails when deploying to a server.

The system is to browse the file, then the system will zip it before uploading it to the server. When a client browse a file, the server will generate an error that the file is not found. Here is my code:

  try {
      //This is a code to read a zipfile.
      String dir = request.getParameter("dirs");
      System.out.println(dir);
      String tmp = dir.replace( '\\', '/' );
      System.out.println(tmp);
      String inFilename = tmp;
      // String inFilename = dir;
      String outFilename = "c:/sms.zip";
      //String outFilename = "/webapps/ROOT/sms.zip";
      FileInputStream in = new FileInputStream( inFilename);
      ZipOutputStream out = new ZipOutputStream(
          new FileOutputStream(outFilename));

      // Add ZIP entry to output stream.
      out.putNextEntry(new ZipEntry(inFilename));

      byte[] buf = new byte[1024];
      int len;

      while ((len = in.read(buf)) > 0) {
          out.write(buf, 0, len);
      }
      //End of zipping file.

      //Start uploading.
      SimpleFTP ftp = new SimpleFTP();

      // Connect to an FTP server on port 21.
      ftp.connect("xxxxx", 21, "xxx", "xxxx");

      // Set binary mode.
      ftp.bin();

      // Change to a new working directory on the FTP server.
      ftp.cwd("web");

      // Upload some files.
      ftp.stor(new File("sms.zip"));

      ftp.disconnect();
      //finish uploading
      out.closeEntry();
      out.close();
      in.close();
      response.sendRedirect("../BakMeClient/success.jsp");
  } 
  catch (IOException e) {
      System.out.println(e);
  }

String dir is the location of file. The error message is:

java.io.FileNotFoundException: D:\RELIVA\listmenu.java (The system cannot find the file specified)


Thanks for all your comments. From my observation the problem is this script is run on the server not on the client.

What I mean is let's say you browse the file for example at c:/test.txt. When you click the upload button, the form will send the path to the server and the server will find the path in its own directory and of course it will not find it.

I hope you get the idea what happened. So now: how to made it read the path at the client?

A: 

Your outFilename must be found in the web. Like: "http://www.sample.com/sms.zip" or the likes..

Treby
my problem is not the output but input..it cannot find a file that have been browse
+3  A: 

Here is definitely a problem:

// Upload some files.
ftp.stor(new File("sms.zip"));

The archive has been created at c:/sms.zip but you try to read it from the relative file location sms.zip (which is equal to ${JAVA_HOME}/sms.zip if I remember correctly The correct part is in Joachim's comment, thanks!!).

Replace these lines with

// Upload some files.
ftp.stor(new File("c:/sms.zip"));

If this doesn't help, then in addition try closing the ZipOutputStream before you send the file with FTP. There's a chance that the ZIP file has not been created yet on the file system just because the stream is still open.

Andreas_D
It's not relative to `$JAVA_HOME` (in fact that variable isn't ever read by Java itself). It is relative to the current working directory, which can be read (but not changed) via the System property `user.dir`.
Joachim Sauer
thanks for your comment..mostly my major problem is at the first line where the browse file not found,even the file is there
A: 

I think, if it is working in your system and not in server, there must be problem with server settings.

Or you can check following things

  1. Need to check path you are working on.

  2. Before uploading, try to list the files in that directory, once you generate ZIP file.

  3. Check for permissions.

sap
+2  A: 

There's a major misunderstanding here. You're sending local disk file system paths around instead of the actual file contents. Imagine that I am the client and I have a file at c:/passwords.txt and I give the path to you. How would you as being a server ever get its contents?

With new FileInputStream("c:/passwords.txt")? No, that is fortunately not going to happen. It will only work when both the client and server runs at physically same machine, as you have found out.

Uploading files with HTML (regardless of if it's inside a JSP file) is supposed to be done with an <input type="file"> field as follows:

<form action="upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit">
</form>

This way the file will be sent in the request body. As the standard Servlet API versions up to with 2.5 doesn't support mulipart/form-data requests, you need to parse the request yourself. The best way is to use Apache Commons FileUpload for this. Follow the link and read both the User Guide and Frequently Asked Questions for code examples and tips&tricks. When you're already on Servlet 3.0, then you can just use the Servlet API provided HttpServletRequest#getParts() for this. You can find here an article with code examples about that.

If you actually want to upload a complete folder with files to the server side and you don't want to use multiple <input type="file"> fields for this, then you'll need Applet or SWF for this, because this isn't possible with plain vanilla HTML. In the server side you can parse the request just the same way.

BalusC