Solved

servlets

Posted on 2000-05-17
2
306 Views
Last Modified: 2013-11-24
how to upload an image file to the server using servlets ?
0
Comment
Question by:jampana
[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
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
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 …
Suggested Courses

732 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