Solved

upload file to a servlet

Posted on 2001-08-28
6
624 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
The viewer will learn how to implement Singleton Design Pattern in Java.

707 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

14 Experts available now in Live!

Get 1:1 Help Now