Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

JSP & <input type="file" >

Posted on 2001-09-07
3
452 Views
Last Modified: 2012-08-13

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-data" method="post" action="add.jsp" name="formUpload">




    <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("InputFile");
if( sInputFile == null )
    sInputFile = "You Did Not Send A File";

%>

0
Comment
Question by:Steve34
3 Comments
 
LVL 1

Expert Comment

by:kanthonym
ID: 6464585
File upload JSP or Servlet can be created by HTTP POST request with multipart/form-data encode. Please download the source code of example for file upload JSP and i18n JSP at FREE. Through HTTP POST request with multipart/form-data encode, JSP or Servlet on Apache Tomcat can receive Japanese Shift_JIS, Chinese BIG5, Chinese GB2312 or Russian KOI8-R. OOP MimeParser is the Java API for parsing HTTP POST request with multipart/form-data encode in your JSP or Servlet programming on Apache Tomcat. HTTP POST request with multipart/form-data encode can be used for file upload JSP (JavaServer Pages) and Servlet on Apache Tomcat. This Java API parses the InputStream from by HTTP POST request with multipart/form-data encode. The path where the files to be uploaded are saved can be specified. The maximum size of file to be uploaded can also be restricted. In addition, your Servlet or JSP on Apache Tomcat can specify the charset to be used for parsing the text parameters in HTTP POST request with multipart/form-data encoding. By this way, your JSP and Servlet on Apache Tomcat can parse the multipart/form-data with any charset, even if it is 2 bytes characters such as Japanese Shift_JIS, Chinese BIG5, Chinese GB2312 or Russian KOI8-R. Now, JSP and Servlet on Apache Tomcat can be internationalization (i18n) ready.



For example, if you want to save the uploaded file...

// Somewhere within doPost() method...

// Get the instance of MimeParser...
MimeParser parser=MimeParser.getInstance();

// 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,"Shift_JIS","foo");

// Now, you can retrieve the text value for TEXT INPUT
// whose name is "user"...
String user=data.getParameter("user");

// 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.getBinaryContents("upfile");

...

Or, you may want not to save the uploaded files...

// Somewhere within doPost() method...

// Get the instance of MimeParser...
MimeParser parser=MimeParser.getInstance();

// 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,"Shift_JIS");

// Now, you can retrieve the text value for TEXT INPUT
// whose name is "user"
String user=data.getParameter("user");

// And the binary contents of the uploaded file...
// (Assuming the name of FILE INPUT is "photo")
byte[] contents=data.getBinaryContents("photo");

...

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.getInstance();

// 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("phrase");

...


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.getInstance();

// 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("phrase");

...


Hope this helps
0
 
LVL 2

Accepted Solution

by:
iDeb earned 50 total points
ID: 6464603
I don't think the file is uploaded url-encoded but since you are posting it is uploaded as content. So in the add.jsp, try getting the reader with request.getReader() or request.getInputStream and then operate on it.
0
 

Author Comment

by:Steve34
ID: 6464991
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
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
diffSum example 4 50
tomcat not starting 6 68
What's wrong with this code? 4 26
Java Eclipse Loop 3 20
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question