File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Websphere and the fly likes .xlsx file not uploading properly Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Products » Websphere
Bookmark ".xlsx file not uploading properly" Watch ".xlsx file not uploading properly" New topic
Author

.xlsx file not uploading properly

venkatesh warlu
Ranch Hand

Joined: Mar 11, 2008
Posts: 33
Hi,

I am having a web application running on WSAD 5.0 and its struts based. While i try to upload the files like .xlsx and .docx the file gets uploaded but its in corrupted format. When i again try to download the file and when i try open file option instead of save it shows the file is corrupted and does not open properly. I tried to modify my web.xml file by adding the mime types but its not showing any improvement. What else option i can try to make the files open properly i.e, suggest some ways to upload the files properly.

Thanks in advance.

Regards
Venkat
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
MIME types have no bearing on file uploads. Are the uploaded files byte for byte copies of the original files?

Or are you inferring that there's an upload problem because after download the files are corrupted? Start by finding out which one it is.
venkatesh warlu
Ranch Hand

Joined: Mar 11, 2008
Posts: 33
Yes the uploaded files are copied byte by byte like this byte [] fc = new byte [1000]; but this works for .xls files perfectly problem occurs only when it comes for uploading .xlsx or .docx file.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Yes the uploaded files are copied byte by byte like this byte [] fc = new byte [1000]

Does that mean ALL files are byte for byte identical? Note that I didn't ask whether the files were copied byte for byte (which is the question you answered), but whether they are byte for byte identical after copying. You need to examine the uploaded file with a hex editor and compare it to the original file to make sure of this.

Assuming that the upload works fine, the next step is the download. Does that result in byte for byte identical copies of the uploaded files as well?
venkatesh warlu
Ranch Hand

Joined: Mar 11, 2008
Posts: 33
No the files are not byte to byte identical. I am using fileinputstream for reading the file and fileoutputstream (write(byte[] b, int off, int len) ) method for writing it. The piece of code works fine for all the .xls files only .xlsx files & .docx are creating the issue.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Unless the files are byte for byte identical (both after upload, and after subsequent download) there's little chance that the process will work correctly. Start by investigating what problem causes the files to have differing sizes.
venkatesh warlu
Ranch Hand

Joined: Mar 11, 2008
Posts: 33
When i click on open file option while download it shows blank document with a error while opening saying the MS Excel found unreadable content. But when i click on save button it saves the file , when i open it gets repaired and with a error it opens the file with actual content. Seems a corrupted file.
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
You already know that the file upload and download doesn't work correctly, so there is little point in opening documents.
Start by investigating what problem causes the files to have differing sizes.
venkatesh warlu
Ranch Hand

Joined: Mar 11, 2008
Posts: 33
You are telling me the problem once again. I understand there is a problem while uploading file. I am eager to know what might be the problem when all office 2003 files are working fine and not 2007 files like .xlsx and .docx etc. I am using WSAD 5.1 and Struts 1.0 for uploading the file to a server location using jsp. Why does the files get corrupt for office 2007 files alone?
Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
Some file formats are more susceptible to corruption than others. In a zip file (like .XLSX and .DOCX) even a single missing byte can make the complete file unusable. Other file formats are more forgiving. I think it's likely that all uploaded files are, in fact, corrupt.
venkatesh warlu
Ranch Hand

Joined: Mar 11, 2008
Posts: 33
Yes you might be right. Might be .xls files even loosing few bytes does not make a difference but .xlsx does. Here is a piece of code i used. Is this ok ?

Lester Burnham
Rancher

Joined: Oct 14, 2008
Posts: 1337
No, it's not. You need to take the value of "i" into account in each loop iteration. See http://faq.javaranch.com/java/ReadDoesntDoWhatYouThinkItDoes for an example of how to do that.
venkatesh warlu
Ranch Hand

Joined: Mar 11, 2008
Posts: 33
Hey Lester, thanks a ton. The below method of taking in it as a buffer and then writing it using fileoutput stream worked out. Thanks once again
Sumit Dike
Greenhorn

Joined: Apr 26, 2012
Posts: 2
Hi,
I tried this, it is behaving erratically. some time works some time not

I tried with following code sets.
1.
File newFile = new File(testFile);
FileOutputStream fos = new FileOutputStream(newFile);
if("docx".equalsIgnoreCase(extention)){
byte fileData[] = PPA.getFileData();
fos.write(fileData);


2.
InputStream in = PPA.getInputStream();
byte input[] = new byte[1024];
while(in.read(input)!=-1){
fos.write(input);
}
in.close


please give me any alternate solution
it's urgent
Thanks in advance


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

Hello, Sumit, welcome to the Ranch!

I see you searched (that's a good thing to do) and found a post which was related to your question. However your next step should have been to read the contents of the thread. It's clear to me that you didn't do that, because it contains the answer you were looking for. In particular if you read the three posts immediately before yours, that's where the answer is:

Venkatesh: Here's my code. (Notice that it's just like your code.)

Lester: Yes, follow this link to see what's wrong with it.

Venkatesh: Thanks, that fixes my problem.
Sumit Dike
Greenhorn

Joined: Apr 26, 2012
Posts: 2
Thanks friends


Now the uploaded file gives the error shown in attached screen
I tried for the solution given on the link :-http://faq.javaranch.com/java/ReadDoesntDoWhatYouThinkItDoes









[Thumbnail for error report.png]

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: .xlsx file not uploading properly