6 Replies Latest reply on Sep 6, 2009 9:10 PM by 794117

    How to read xls file data uploaded from remote machine?

    843840
      Hello All,
      I am facing problem for reading data from Excel(xls) file uploaded from remote machine(by client).
      It works fine when file is uploaded from local machine where app. server reside using same following code.
      (extractorDataClass.getFilePath() this is giving us uploaded file path)
      FileInputStream fis = new FileInputStream(new File(extractorDataClass.getFilePath().toString()));               
      POIFSFileSystem fs = new POIFSFileSystem(fis);
      wb = new HSSFWorkbook(fs);.......................
      It is working fine while reading data from local machine.
      but giving problem while reading it remotely and giving exception mentioned below....

      java.io.FileNotFoundException: D:\New Microsoft Excel Worksheet.xls (The system cannot find the file specified)
      17:39:23,732 INFO [STDOUT]      at java.io.FileInputStream.open(Native Method)
      17:39:23,732 INFO [STDOUT]      at java.io.FileInputStream.<init>(FileInputStream.java:106)

      D:\New Microsoft Excel Worksheet.xls------- this is the path of remote client but it looks like local machine path.

      So please suggest some solution....

      Thanks and regards
      Only1vinay
        • 1. Re: How to read xls file data uploaded from remote machine?
          843840
          It sounds like that you're getting the file path from the client only and trying to use it to read the file from the local disk file system of the server. This is obviously NOT going to work if the client (who sent the file path) and the server (who needs to get the file contents) are both at physically different machines.

          The client has to send the complete file contents to the server, not only the path. The normal HTTP/HTML way is using an <input type="file"> element in a <form method="post" enctype="multipart/form-data">. This way the file contents is included in the request body. As normal Servlet API doesn't support multipart/form-data encoding out of the box, you need to parse the request body yourself by reading the HttpServletRequest#getInputStream(). This is a tedious work and the commonly used API to take over the precious work is [Apache Commons FileUpload|http://commons.apache.org/fileupload]. Go use it, it is really good stuff. Carefully read both the "User Guide" and(!) the "Frequently Asked Questions" sections over there how to use it and learn the tips and tricks. Good luck.
          • 2. Re: How to read xls file data uploaded from remote machine?
            843840
            I am getting file data in action class but it is unreadable format and also i am using Apache POI for reading file for that it required the uploaded file location and it is searching file in server machine which totally wrong so what should i do?

            in Advance
            Thanks and Regards
            Only1Vinay
            • 3. Re: How to read xls file data uploaded from remote machine?
              843840
              Only1Vinay wrote:
              I am getting file data in action class but it is unreadable format
              Then the code was handling the data the wrong way. XLS files are binary files, so it should be handled as binary file all the time (i.e. there is no means of reader/writer/encodings).
              and also i am using Apache POI for reading file for that it required the uploaded file location and it is searching file in server machine which totally wrong so what should i do?
              Huh? Did you understand my previous reply anyway? When you handle a file upload, you should retrieve the complete file contents (the path is totally irrelevant) and write it to local disk file system. You can NOT access the client's disk file system from the server machine on. That would have been a huge security hole otherwise. Imagine I am the client and you're the server and I tell you that I have a xls file at "c:/foo/bar/test.xls", how would you as being a server ever access it?
              • 4. Re: How to read xls file data uploaded from remote machine?
                843840
                ya thats right,but then how will i write that binary data into the local disk so that it retains its original format. now i am trying to do writing that xls data into the xls file on local disk.so please suggest some solution for it.

                Actually i am getting that data from file control directly and it is in Binary format now i want to retain that data into original xls file on local disk
                how to do that?

                Thanks and Regards
                Only1Vinay

                Edited by: Only1Vinay on Sep 4, 2009 5:34 AM
                • 5. Re: How to read xls file data uploaded from remote machine?
                  843840
                  Use FileOutputStream.

                  You should in no way use a Reader or Writer. Those are for character data only.
                  • 6. Re: How to read xls file data uploaded from remote machine?
                    794117
                    Actually i am getting that data from file control directly and it is in Binary format now i want to retain that data into original xls file on local disk
                    how to do that?
                    Use FileOutputStream.
                    I would actually recommend you use the Commons FileUpload API as previously suggested. Why bother with FileOutputStream?
                    Since saving the uploaded file is such a common requirement, it has a [convenience method|http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/FileItem.html] already there for you.

                    cheers,
                    evnafets