?
Solved

upload file to a servlet

Posted on 2001-08-28
6
Medium Priority
?
635 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
[X]
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
6 Comments
 

Accepted Solution

by:
jerk0main earned 90 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
Understanding Linux Permissions

Linux for beginners: How to view the permissions associated with files and directories and also how you can change them.

 
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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses
Course of the Month13 days, 23 hours left to enroll

800 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