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 ]
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.
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?
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.
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
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.
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.