How To Fetch the Content of a File from Client's PC?

In my JSP I have this:

<input type="file" name="filename">

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?

[408 byte] By [Natalie999] at [2007-9-30 18:51:58]
# 1

From http://www.htmlhelp.com/reference/html40/forms/input.html:

A form that includes a file INPUT must specify METHOD=post and ENCTYPE="multipart/form-data" in the <FORM> tag. CGI libraries such as CGI.pm allow simple handling of such forms.

Form-based file upload is unsupported by many currently deployed browsers. Authors should provide alternative methods of input where possible.

The following example allows the user to upload an HTML document for validation:

<FORM METHOD=post ACTION="/cgi-bin/validate.cgi" ENCTYPE="multipart/form-data">

<P>Select an HTML document to upload and validate. If your browser does not support form-based file upload, use one of our <A HREF="methods.html">alternate methods of validation</A>.</P>

<P><INPUT TYPE=file NAME="html_file" ACCEPT="text/html"></P>

<P><INPUT TYPE=submit VALUE="Validate it!"></P>

</FORM>

--

Your servlet would then have to process whatever data is put into that parameter. You can look at O'Reilly's servlet utilities, or one of the other other links here:

http://www.google.com/search?q=java+servlet+file+upload&sourceid=opera&num=0&ie=utf-8&oe=utf-8

jverd at 2007-7-6 21:09:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2
I'm not sure how you get the filename though. Not sure why you'd need it though either. It shouldn't matter to you what your user calls that file on his PC.
jverd at 2007-7-6 21:09:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3
The reason that I also need the name of that uploaded file is that when the user visits the web site later on (an hour later, a week later, or ....), he/she will see that file name as a link. The user clicks on that link and the content of that file will be displayed for that user.
Natalie999 at 2007-7-6 21:09:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 4
The filename is sent with the file data. Whatever upload library you use should give it to you. You should only expect that it's the actual filename, not the full path. I think some browsers send the whole path, although I personally think that's a security "issue".
bsampieri at 2007-7-6 21:09:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 5

> The reason that I also need the name of that uploaded

> file is that when the user visits the web site later

> on (an hour later, a week later, or ....), he/she will

> see that file name as a link. The user clicks on that

> link and the content of that file will be displayed

> for that user.

Okay.

It might be that it gets somehow put into the parameter that contains the file contents, thought I wouldn't think so.

The only other way I can think of is to set a separate request parameter to hold the filename. I don't know how to automatically populate that from what the user selects via that input element though, or if you even can. If you can do it, it should be in some of those links in the google search I posted. Otherwise you might just have to have them explicitly enter a name of their choice by which to remember that upload.

jverd at 2007-7-6 21:09:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 6
> The filename is sent with the file data. I stand corrected.
jverd at 2007-7-6 21:09:48 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...