Solved

servlets

Posted on 2000-05-17
2
302 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JAVA part two 5 58
Responding to Java JComponent extended classes's resize event 4 52
Basic Java Case or If-Else statement... 3 43
split string containing \r\n in Java 46 38
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…
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 …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

867 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

15 Experts available now in Live!

Get 1:1 Help Now