Solved

upload file to a servlet

Posted on 2001-08-28
6
627 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
firstswap challenge 20 65
Eclipse IDE - Cannot copy/paste from console output 8 126
Java passing arguments (type error) 2 48
Basic Java Case or If-Else statement... 3 43
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

914 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now