Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

servlets

Posted on 2000-05-17
2
Medium Priority
?
314 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 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

636 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