Solved

upload file to a servlet

Posted on 2001-08-28
6
628 Views
Last Modified: 2013-11-24
Hello All,
 I would like to upload a file to a java servlet from a form with a <INPUT TYPE="file" > control. I would like to java servlet to save to the file system.

Can anyone point me to some sample code which can do this?

Thanks in advance,
Nicholas Waltham
0
Comment
Question by:nwaltham
6 Comments
 

Accepted Solution

by:
jerk0main earned 30 total points
ID: 6431309
Note: when submitting a form that contains a 'file' input, the resulting http requests has content type set to
'multipart/form-data'
One solution to your problem is:
Find and download a file called 'MultipartRequest.java' from the Web. It is free.
Then wrap the original request that your servlet received in a MultipartRequest. Then use the additional methods of MultipartRequest to get the uploaded files.
0
 
LVL 5

Expert Comment

by:Jan Louwerens
ID: 6438845
If your JSP engine doesn't natively support forms with enctype="multipart/form-data", then use the following com.oreilly.servlet package:

http://www.servlets.com/cos/index.html
0
 
LVL 3

Expert Comment

by:black
ID: 6439237
here's code for a jsp file that you can use with the package mentioned by jlouwere, i use it and I can certainly recommend it. call the file FileUpload.jsp

<%@ page import="com.oreilly.servlet.*"%>
<%@ page import="java.io.*"%>
<%
File myFile = null;
try {
  MultipartRequest multi = new MultipartRequest(request, ".", 8 * 1024);
  myFile = multi.getFile("myFile");
} catch (Exception e) {}
%>
<html>
<head>
<title>FileUpload.jsp</title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="FileUpload.jsp" name='myForm'>
  <input type="file" class='file' name="myFile">
  <input type="submit" value="Submit">
</form>
<%
if(myFile != null) {
  StringWriter writer = new StringWriter();
  InputStream is = new FileInputStream(myFile);
  for(int i = is.read(); i != -1; i = is.read()) {
    writer.write((char) i);
  }
  out.println(writer.getBuffer());
}
%>
</body>
</html>
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 7

Expert Comment

by:Ravindra76
ID: 6444706
look:

Uploading:
http://www.servlets.com/jservlet2/examples/
http://coldjava.hypermart.net/servlets/upload.htm 
http://www.servlets.com/jservlet2/examples/ch04/index.html

20. How do I upload a file to my servlet?
From Thomas Moore's Servlet FAQ:

Form-based file upload requires a couple of steps.

The server must supply (and the client must support) encoding type multipart/form-data.
 Most current browsers do,
but it's not a guarantee. Secondly (and this is usually the trickiest part), your servlet
 has to parse the binary data and
do something with it (e.g., write it to a file on the server).

The intrepid programmer is referred to RFC 1867 for cluefulness on how to parse this data.
 Less brave souls can use
either Jason Hunter's implementation of a MultipartRequest
(available from http://www.servlets.com), or
CParseRFC1867 (available from http://www.servletcentral.com).

Note that the source code is available for both of these examples, but both assume that you
will be writing the file to
a file on the server. Other uses (e.g. storing the file as a binary object in a database)
 will require adaptation.

There is a multipart/form parser availailable from Anders Kristensen (ak@hplb.hpl.hp.com)
at http://www-
uk.hpl.hp.com/people/ak/java/#utils. JavaMail also has MIME-parsing routines
(see the References section).

Here is an example of HTML code that allows file upload, courtesy of Detlef Pleiss
(dpleiss@os-net.de) :

<FORM ENCTYPE="multipart/form-data" method=post
action="..."> put the servlet URL here, of course
<INPUT TYPE="file" NAME="mptest"><INPUT TYPE="submit" VALUE="upload">
</FORM>

0
 

Expert Comment

by:Dsys
ID: 6447970
listening...
0
 

Author Comment

by:nwaltham
ID: 6664360
Thanks everybody for their input!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JList custom Cell Renderer refresh 15 57
HTTPSessionBindingListernter not configured in deployment descriptor 2 53
java jdbc batch example 8 34
Eclipse with gitlab 1 43
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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

777 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