Solved

servlets

Posted on 2000-05-17
2
301 Views
Last Modified: 2013-11-24
how to upload an image file to the server using servlets ?
0
Comment
Question by:jampana
  • 2
2 Comments
 
LVL 7

Accepted Solution

by:
Ravindra76 earned 100 total points
ID: 2817075
look:
Uploading:
http://www.servlets.com/jsp/examples/ch04/index.html
http://coldjava.hypermart.net/servlets/upload.htm
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>




The input type "file" brings up a button for a file select box on the browser together with a text field that takes the
file name once selected. The servlet uses the GET method parameters to decide what to do with the upload while the
POST body of the request contains the file data to parse. Tested with IE4, IE5 and Netscape 4.5.
---
Paul wrote:
Go to Oreilly's web site(http://www.oreilly.com), navigate to Java section, finally get to the book titled [Java servlet
programming]. You can download its sample code in a zip file. In chapter 6, you will find the upload.html and
unloadtest.java. But you need to download an extra utility class stored in a zip file named
[cos.zip]http://www.servlets.com/resources/com.oreilly.servlet/cos.zip. The URL is
http://www.servlets.com/resources/com.oreilly.servlet/index.html
With these files, you wil be able to send any type of files.

With those example files, you can send multiple files by adding more input fields in the html file. You need to
modify the servlet if you want to see the whole list of the files you send instead of the last one. You just need to use
an array to keep the list of files you receive. There is no limit to the size of files you can send. The Sun's examples
do have size limitation.
----


0
 
LVL 7

Expert Comment

by:Ravindra76
ID: 2817084

  If you are Using your Own Servlets then it's simple to upload a file from your applet too ,
  check out this
  http://www.servlets.com/jsp/examples/ch04/UploadTest.java

  Also go to
  http://www.servlets.com/resources/com.oreilly.servlet/index.html
  and look for class MultipartRequest's code

  http://www.sun.com/software/jwebserver/faq/faq.html#c8  and
  http://www.sun.com/software/jwebserver/faq/faq.html#c9

  http://www.purpletech.com/java/servlet-faq/#15
0

Featured Post

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.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countX 22 71
count11 challenge 6 49
JAVA part two 5 42
listing all functions in JavaScript 19 114
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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…
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

744 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

12 Experts available now in Live!

Get 1:1 Help Now