The moose likes Struts and the fly likes Problem after uploading microsoft excel (.xls) file using struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Problem after uploading microsoft excel (.xls) file using struts" Watch "Problem after uploading microsoft excel (.xls) file using struts" New topic

Problem after uploading microsoft excel (.xls) file using struts


Joined: Oct 27, 2006
Posts: 3
Hi All,

Iam facing a problem with uploding .xls files using struts.iam able to upload .txt,.doc,.csv files.If i try to upload .xls files there are uploading to the loacl drive say for eg.C or D drive.If i try to open the
file from the drive location(C or D), iam not able to view the content.

my code for upload is,

**** htFileList is a HashTable which contains the files to be uploaded.
**** path is a sting array which contains the paths where the files will
be uploaded.

**** I used MultipartRequestHandler for storing the file sin HashTable.

Added UBB code tags for readability
[ October 27, 2006: Message edited by: Merrill Higginson ]
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I've uploaded many an .xls file using the Struts upload function and have not had this problem. The obvious suspect is the code you're using to create the Hashtable of FormFiles. I'd suggest you try an experiment to see if this is the case or not.

Code up a simple JSP that uploads a single file, and code a simple ActionForm to go along with the page that has a single FormFile property. Then just let Struts populate the FormFile normally and use similar code to what you posted above to write the file to disk. If this file is readable and the others are not, then you know the problem is in your code that creates the HashTable.

Consultant, Sima Solutions
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
The problem is with your choice of BufferedWriter. This class is used for text files. For binary files you will want to use BufferedOutputStream. Also, you should move the opening and closing of your output file outside of the loop so you don't have to do this for every byte that you read from the input stream.

- Brent
Brent Sterling
Ranch Hand

Joined: Feb 08, 2006
Posts: 948
FYI, I got a private message from Aswinle saying that the following code worked:

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(pathinfo+"\\"+htFilesList.get("file"+(fileCount)), true));

Cool! I am sure I am not the only one that thinks that the java.io package is a bit confusing. It always seems like there is 10 different ways to do everything. The "InputStream/OutputStream" versus "Reader/Writer" naming convention seems like a bad design decision.

- Brent
It is sorta covered in the JavaRanch Style Guide.
subject: Problem after uploading microsoft excel (.xls) file using struts
Similar Threads
How to store a particular data into project file
Uploading Multiple Files Using html:file
Upload multiple files with separate submit buttons
Uploading files with JSF
Uploading xls files with JSF