Steve34
asked on
JSP & <input type="file" >
Hi;
I am trying to use the HTML input type "file" with JSP to let my users upload files from their machines.
So far I have had nothing but null pointers. My code is simple and is included below. "upload.jsp" has
the html form, "add.jsp" has the jsp code to catch the file...........which never makes it.
Am I missing something.
Any tips would be greatly appreciated.
Thanks
Steve
//------------------------
// From File upload.jsp
//------------------------
<form ENCTYPE="multipart/form-da
<input TYPE="FILE" NAME="InputFile">
<br>
<br>
<input type="submit" name="buttonUploadFile" value="Upload File">
</form>
//------------------------
// From File: add.jsp
//------------------------
<%
String sInputFile;
sInputFile = request.getParameter("Inpu
if( sInputFile == null )
sInputFile = "You Did Not Send A File";
%>
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks!
I looked in several places in the books and on the internet and I didn't get a clue as to what to do.
Your answer was simple and easy to understand.
Thanks again
Steve
I looked in several places in the books and on the internet and I didn't get a clue as to what to do.
Your answer was simple and easy to understand.
Thanks again
Steve
For example, if you want to save the uploaded file...
// Somewhere within doPost() method...
// Get the instance of MimeParser...
MimeParser parser=MimeParser.getInsta
// Parse the text values submitted from TEXT INPUT
// or SELECTION.
// At the same time, the files uploaded from FILE INPUT
// are saves into the local file system on the server side.
// The text will be decoded using Shift_JIS.
// The file will be saved under "foo" directory.
ParsedData data=parser.parse(req,"Shi
// Now, you can retrieve the text value for TEXT INPUT
// whose name is "user"...
String user=data.getParameter("us
// If the name of FILE INPUT in the FORM is "upfile",
// then java.io.File object can be retrieved by:
File file=data.getFile("upfile"
// Or, you may want to get the binary contents of the
// uploaded file...
byte[] contents=data.getBinaryCon
...
Or, you may want not to save the uploaded files...
// Somewhere within doPost() method...
// Get the instance of MimeParser...
MimeParser parser=MimeParser.getInsta
// Parse the text values submitted from TEXT INPUT
// or SELECTION.
// The contents of the files uploaded through FILE INPUT
// is NOT saved on the server side, but you can access it
// later.
// The text will be decoded using Shift_JIS.
ParsedData data=parser.parse(req,"Shi
// Now, you can retrieve the text value for TEXT INPUT
// whose name is "user"
String user=data.getParameter("us
// And the binary contents of the uploaded file...
// (Assuming the name of FILE INPUT is "photo")
byte[] contents=data.getBinaryCon
...
While reading InputStream, these methods will pick up charset parameter among all the submitted parameters. Then, these method parse the rest of the parameters using it. The charset parameter can be submitted as HIDDEN INPUT or SELECTION in the FORM.
For example, the FORM below will send the charset parameter to another JSP.
In the destination JSP, the text value which is submitted by the second INPUT will be decoded by the charset specified by the first INPUT, i.e. the value for phrase parameter will be decoded by Shift_JIS. The destination JSP can pick the charset automatically, so you need not bother about it at all:
// Somewhere within doPost() method...
// Get the instance of MimeParser...
MimeParser parser=MimeParser.getInsta
// Parse the text values submitted from TEXT INPUT
// or SELECTION. Note that the second parameter is null.
// But the instance of MimeParser can find the charset!
// In this case, the text will be decoded using Shift_JIS.
// (See the above FORM.)
ParsedData data=parser.parse(req,null
// Now, you can retrieve the decoded text.
// (Assuming that the name of TEXT INPUT is "phrase".)
String phrase=data.getParameter("
...
While reading InputStream, these methods will pick up charset parameter among all the submitted parameters. Then, these method parse the rest of the parameters using it. The charset parameter can be submitted as HIDDEN INPUT or SELECTION in the FORM.
For example, the FORM below will send the charset parameter to another JSP.
In the destination JSP, the text value which is submitted by the second INPUT will be decoded by the charset specified by the first INPUT, i.e. the value for phrase parameter will be decoded by Shift_JIS. The destination JSP can pick the charset automatically, so you need not bother about it at all:
// Somewhere within doPost() method...
// Get the instance of MimeParser...
MimeParser parser=MimeParser.getInsta
// Parse the text values submitted from TEXT INPUT
// or SELECTION. Note that the second parameter is null.
// But the instance of MimeParser can find the charset!
// In this case, the text will be decoded using Shift_JIS.
// (See the above FORM.)
ParsedData data=parser.parse(req,null
// Now, you can retrieve the decoded text.
// (Assuming that the name of TEXT INPUT is "phrase".)
String phrase=data.getParameter("
...
Hope this helps