File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File upload only handles .txt documents??

 
Annemarie McKeown
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using the following code to handle file uploads using JSP.
The form which calls this upload.jsp is of type multipart.
However the upload only works for txt documents, word douments .doc and .pdf documents do not open once uploaded to server. They say error opening file when trying to open.
Can someone help me with the following code, what do I need to do to handle binary uploads, I think at the minute it only supports ascii.


The whole upload process is handled by this upload.jsp file.
Some help please.
[ March 08, 2004: Message edited by: Annemarie McKeown ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use UBB code tags when posting code. Many people will not take the time to try and decipher unformatted blobs of code.
 
Annemarie McKeown
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What are UBB code tags and how do I use them?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you are at the page where you enter a topic or reply there are a bunch of buttons that will help enter UBB tags (similar to HTML markup, except using square brackets). There is also a link named "What is UBB Code?" that might be helpful.
 
Annemarie McKeown
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, can you please help me, ive been stuck at this problem for days.
Annemarie.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Without looking into it in any great detail, I'd take a look around your handling of "lines" and the '\r' character. Are you stripping these out? That makes no sense for binary files.
Is there a good reason that you are loathe to use one of the proven multi-part parsers and file uploaders such as the oreilly package and others that have been discussed in earlier topics?
 
Annemarie McKeown
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, because I wanted to try it for myself and also because I couldn't get the o'reilly package working.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64178
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can certainly respect the "try it myself" reasoning, but if this is something you are under a deadline for, you might have had better luck trying to get help getting oreilly to work.
That said, I still believe that your primary problem is that you seem to be treating the file content as text-oriented; e.g. lines terminated with a \r. You should just treat the file content as 'blocks of bytes" with no interpretation. That way, the interpretation, be it text or binary, is preserved.
 
Annemarie McKeown
Ranch Hand
Posts: 47
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks thats a big help.
Do you think I should use MultipartRequest as defined in the O'Reilly package?
Do you have any code for methods which handles with file uploads rather than reading it in by lines.
Annemarie
 
Nicholas Cheung
Ranch Hand
Posts: 4982
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure whether pure JSP has such coding.
But I have worked with upload using Struts. We can define a Form which captures the upload parameters (defined in the Struts config file), then we need to define an ActionDispatcher to handle the request. When the container calls the ActionDispatcher's method, the files and the form parameters have already been uploaded to the Struct Form, and we can get all info, including the upload file, from the Form.
Before using Struts, I have also used JSP Smart upload, which is a free API for JSP to upload file. However, it has a serious security issue, and finally, this API is not recommended.
Nick.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your problem (or at least one of them, I've not done exhaustive research) is that you're using a StringWriter to write out the uploaded data.
I think if you use some other mechanism (maybe a ByteArrayOutputStream) you should be able to write your data as binary without it getting corrupted.
The JavaDoc confirms this:

FileOutputStream is meant for writing streams of raw bytes such as image data. For writing streams of characters, consider using FileWriter.

in fact, all Writers work on character arrays only and can cause corruption in binary data.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic