aspose file tools*
The moose likes JSP and the fly likes File upload only handles .txt documents?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "File upload only handles .txt documents??" Watch "File upload only handles .txt documents??" New topic
Author

File upload only handles .txt documents??

Annemarie McKeown
Ranch Hand

Joined: Nov 07, 2003
Posts: 47
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

Please use UBB code tags when posting code. Many people will not take the time to try and decipher unformatted blobs of code.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Annemarie McKeown
Ranch Hand

Joined: Nov 07, 2003
Posts: 47
What are UBB code tags and how do I use them?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Nov 07, 2003
Posts: 47
Thank you, can you please help me, ive been stuck at this problem for days.
Annemarie.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Nov 07, 2003
Posts: 47
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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

Joined: Nov 07, 2003
Posts: 47
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

Joined: Nov 07, 2003
Posts: 4982
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.


SCJP 1.2, OCP 9i DBA, SCWCD 1.3, SCJP 1.4 (SAI), SCJD 1.4, SCWCD 1.4 (Beta), ICED (IBM 287, IBM 484, IBM 486), SCMAD 1.0 (Beta), SCBCD 1.3, ICSD (IBM 288), ICDBA (IBM 700, IBM 701), SCDJWS, ICSD (IBM 348), OCP 10g DBA (Beta), SCJP 5.0 (Beta), SCJA 1.0 (Beta), MCP(70-270), SCBCD 5.0 (Beta), SCJP 6.0, SCEA for JEE5 (in progress)
Jeroen Wenting
Ranch Hand

Joined: Oct 12, 2000
Posts: 5093
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.


42
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File upload only handles .txt documents??