aspose file tools*
The moose likes JSP and the fly likes How To Fetch a File ( .doc, .txt, .pdf, ..., etc.) From Client's Machine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How To Fetch a File ( .doc, .txt, .pdf, ..., etc.) From Client Watch "How To Fetch a File ( .doc, .txt, .pdf, ..., etc.) From Client New topic
Author

How To Fetch a File ( .doc, .txt, .pdf, ..., etc.) From Client's Machine

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
In my JSP I have this:

for visitors of the web page to browse their PCs' directories to select a file to upload (when the Submit button is clicked).

What appears in the text field will be a file name. How do I get both the file name and the content of that file to be saved in the database?
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Any ideas and suggestions? Please. By the way, I do not have to convert those files into a certain format. I simply want to upload files straight from client's machine and store them into the database.
Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 140
1) u need to make the form content type as multipart/formdata <form>
2) then you need to implement a MIME parser and use it in your servlet or jsp to which you are submitting the form, to parse the binary contents that comes through the request.
3) with that parser identify what are the form fields, which part of that binary content is the actual file and then process accordingly.


instead of re-inventing the wheel, ie instead of developing the MIME parser, you can use available components like Apache's FILEUPLOAD component to handle the binary request.


Work like you don't need the money. Love like you've never been hated. Dance like nobody's watching. Sing like nobody's listening. Live like it's Heaven on Earth.
Currently I Reside Here WEBlog
Senthil B Kumar
Ranch Hand

Joined: Feb 09, 2004
Posts: 140
The enctype attribute of the FORM element specifies the content type used to encode the form data set for submission to the server.

application/x-www-form-urlencoded , This is the default content type.

The control names/values are listed in the order they appear in the document. The name is separated from the value by `=' and name/value pairs are separated from each other by `&'.


The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.

A "multipart/form-data" message contains a series of parts, each representing a successful control. The parts are sent to the processing agent in the same order the corresponding controls appear in the document stream. Part boundaries should not occur in any of the data; how this is done lies outside the scope of this specification.

As with all multipart MIME types, each part has an optional "Content-Type" header that defaults to "text/plain". User agents should supply the "Content-Type" header, accompanied by a "charset" parameter.

Each part is expected to contain:

a "Content-Disposition" header whose value is "form-data".
a name attribute specifying the control name of the corresponding control.

Thus, for example, for a control named "mycontrol", the corresponding part would be specified:

Content-Disposition: form-data; name="mycontrol"

As with all MIME transmissions, "CR LF" (i.e., `%0D%0A') is used to separate lines of data.


If the contents of a file are submitted with a form, the file input should be identified by the appropriate content type (e.g., "application/octet-stream"). If multiple files are to be returned as the result of a single form entry, they should be returned as "multipart/mixed" embedded within the "multipart/form-data".

The user agent should attempt to supply a file name for each submitted file. The file name may be specified with the "filename" parameter of the 'Content-Disposition: form-data' header, or, in the case of multiple files, in a 'Content-Disposition: file' header of the subpart.

This is convenient for those cases where, for example, the uploaded files might contain references to each other (e.g., a TeX file and its ".sty" auxiliary style description).

The following example illustrates "multipart/form-data" encoding. Suppose we have the following form:

<FORM action="http://server.com/cgi/handle"
enctype="multipart/form-data"
method="post">
<P>
What is your name? <INPUT type="text" name="submit-name"><BR>
What files are you sending? <INPUT type="file" name="files"><BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</FORM>


If the user enters "Larry" in the text input, and selects the text file "file1.txt", the user agent might send back the following data:

Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
Content-Disposition: form-data; name="files"; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--AaB03x--

If the user selected a second (image) file "file2.gif", the user agent might construct the parts as follows:

Content-Type: multipart/form-data; boundary=AaB03x

--AaB03x
Content-Disposition: form-data; name="submit-name"

Larry
--AaB03x
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y

--BbC04y
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of file2.gif...
--BbC04y--
--AaB03x--


So this form should be submitted to Servlet in the server, and in the servlet you have to decode the request using the boundary, identify the individual controls , their names and their values.......

there are few 3rd party Servlets available doing the above work....

you may find in Apache site the FileUpload component.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How To Fetch a File ( .doc, .txt, .pdf, ..., etc.) From Client's Machine