• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

.xlsx file not uploading properly

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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


 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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







error-report.png
[Thumbnail for error-report.png]
 
reply
    Bookmark Topic Watch Topic
  • New Topic