Solved

upload file to a servlet

Posted on 2001-08-28
6
629 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

789 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