How display a Image on Servlet from file upload

How display a Image on Servlet from file upload

View Answers

December 15, 2008 at 1:29 AM

Hi friend,

Code to help in solving the problem :

"upload.html"

<html>
<head><title>Upload page</title></head></p> <p><body>
<form action="tmp" method="post" enctype="multipart/form-data" name="form1" id="form1">
<center>
<table border="2">


<tr>
<td align="right"><b>Employee Image </td>
<td>
<input name="file" type="file" id="file">
<td>
</tr>

<tr>
<td align="center">
<input type="submit" name="Submit" value="Submit"/>
<input type="reset" name="Reset" value="Reset"/>

</td>
</tr>
</table>
</center>
</form>
</body>
</html>

"UploadFile.java"

import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.*;
import org.apache.commons.fileupload.disk.*;


public class UploadFile extends HttpServlet{
public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
response.setContentType("text/html");

PrintWriter out= response.getWriter();
Connection con=null;
String itemName="";
boolean fileUpload=false;
try{
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
FileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List items = null;
try {
items = upload.parseRequest(request);
} catch (FileUploadException e) {
e.printStackTrace();
}
Iterator itr = items.iterator();
while (itr.hasNext())
{
FileItem item = (FileItem) itr.next();
String name = item.getFieldName();
if(name.equals("file"))
{
itemName = item.getName();
File savedFile = new File(request.getRealPath("/")+"image\\"+itemName);
item.write(savedFile);
fileUpload=true;
}
}
if(fileUpload==true)
{
out.println("File has been uploaded successfully");
out.println("<html>");
out.println("<body>");
out.println("<table><tr><td width='210'></td><td> <img border=2 src=image/"+itemName.toString().trim()+" width=137 height=140></td></tr></table>");
out.println("</body>");
out.println("</html>");
}

}
catch (Exception e){
e.printStackTrace();
}
}
}

web.xml:
<web-app>
<servlet>
<servlet-name>one</servlet-name>
<servlet-class>UploadFile</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>one</servlet-name>
<url-pattern>/tmp</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Home.html</welcome-file>
</welcome-file-list>
</web-app>

For more information on Servlet visit to :

http://www.roseindia.net/servlets/

Thanks









Related Tutorials/Questions & Answers:
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload   Dear Sir, My Question is: How display a image on servlet from file upload Your Answer: Hi... reason is inaccessible Thanks Sir, But Servlet page nothing display any type
How display a Image on servlet from file upload - JSP-Servlet
How display a Image on servlet from file upload  Dear Sir, I were ask a question that How display the Image on servlet through file upload. Today I get your answer. But Sir, It code not display the image on servlet
Advertisements
How display a Image on Servlet from file upload - JSP-Servlet
How display a Image on Servlet from file upload  Dear Sir, My requirement is I want to display a Image on Servlet from File Upload. But It not display on servlet,Its appear a downloaded form and downloaded on disk when click
How display a image on servlet from file upload - JSP-Servlet
How display a image on servlet from file upload  Dear Sir, My issue is: How display a image on servlet from file upload I receive your answer today... upload page Employee Image
Image upload file - JSP-Servlet
Image upload file  I want a code for image upload jsp or servlet.  Hi friend, For image upload jsp or servlet visit to : http://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml http
How to display image in jsp from database using Servlet?
How to display image in jsp from database using Servlet?  Hi, How to display image in jsp from database using Servlet? Thanks   Hi, You will find code and example program at Retrieve image from database using Servlet
how to upload a file of apk format in a folder from servlet
how to upload a file of apk format in a folder from servlet  How to upload a file of apk format to a folder from servlet, i am trying to do, its getting uploaded as .zip file and not apk file
how to upload a file - JSP-Servlet
: Display file upload form to the user UPLOAD THE FILE   Choose the file To Upload:   2...how to upload a file  Dear sir, Give me some code
how to upload image from jsp to mssql
how to upload image from jsp to mssql  hi there!!, i'm using jsp and servlet to upload images to the database. however i have difficulty in uploading... uplaodimage.java(servlet)   Here is a code that uploads any file through
how to upload image from jsp to mssql
how to upload image from jsp to mssql  hi there!!, i'm using jsp and servlet to upload images to the database. however i have difficulty in uploading..., uplaodimage.java(servlet)   Here is a code that uploads any file through
Retrieve image from database using servlet and display in JSP
Retrieve image from database using servlet and display in JSP  Hi, I... to use MySQL Database from JSP page. How to retrieve image from database using servlet and display in JSP? Thanks
How to retrieve and display image from database in Java?
How to retrieve and display image from database in Java?  Hi, I am... on the web page. My application is in Java/JSP. How to retrieve and display image.... Please check the it at How to store and retrieve image from database in JSP
Retrview image from sqlserver2000 and display in jsp - JSP-Servlet
Retrview image from sqlserver2000 and display in jsp  How to retreview image from sqlserver2000 to jsp? pls give the solution i am using Tomcat 4 Database Server:SQLSERVER2000 pls give the full coding  Hi friend
How to retrieve image from database in Servlet?
How to retrieve image from database in Servlet?  Hi, How to retrieve image from database in Servlet? Thanks   Hi, Please check the tutorial Retrieve image from database using Servlet. Thanks
servlet file upload
servlet file upload  please tell me the complete code to upload a file on localhost using servlet
how to display a table from database using servlet
how to display a table from database using servlet  how to display a table with values from servletpage   Hi Friend, Please go through the following link:ADS_TO_REPLACE_1 http://roseindia.net/jsp/servlet-jsp-data
image display - JSP-Servlet
image display  how to display image that store in mysql database... column have BLOB datatype....  Hi Friend, Please visit the following link: http://www.roseindia.net/jsp/retrieve-image.shtml Thanks
How to upload a large excel file - JSP-Servlet
How to upload a large excel file   Dear sir , How to upload a large excel file and how to read a that large excel file or how to get a each column values from that large excel file.For small file i am getting values
how to display image from byte arry - Java Magazine
how to display image from byte arry  Hi.. How to display a image using byte array[] object , which have the image data in binary format. If it can be resolved , itz very helpful for me. Regards Rajesh
How to display Jfreechart from servlet in jsp web page at specified location
How to display Jfreechart from servlet in jsp web page at specified... to display the chart in web page. I generated the chart using Jfreechart in Servlet which is in image format.plz sir give me the code to display this chart
how to display image and text in single jsp page from the mysql database
how to display image and text in single jsp page from the mysql database  hello please help me to display the image and text in single jsp page from mysql database if have any reference code please send me Thanks in advance
how do i upload a file by using servlet or jsp?
how do i upload a file by using servlet or jsp?  hi plz tell me the write java code
display multiple image file or pdf file in multiple column of a row from server or database
display multiple image file or pdf file in multiple column of a row from server... want to display that file to the user page but I want to display that file... uploading more files then uploaded file should be display again tow file in another row
how to display data from jsp file into database
how to display data from jsp file into database  this is a jsp file . look at the line with code-> Statement st=con.createStatement(); in the below example. the error is "cannot convert from java.sql.Statement
File upload - JSP-Servlet
----------------------------------------------------------------------------- Display file upload form to the user <...File upload  I am trying to do a file upload program. But, it shows... the file To Upload
image file upload to a web server
image file upload to a web server  Refer roseindia post http... Tomcat 5 server. How can I upload a file to the web server?   Here...;HTML> <HEAD><TITLE>Display file upload form</TITLE></HEAD>
File upload - JSP-Servlet
in the problem part]   Hi friend, Display file upload form to the user... to do a file upload part. For this, i designed two files one for input and other...(); %>You have successfully upload the file by the name
Upload Image to Database through Servlet - JSP-Servlet
Upload Image to Database through Servlet  Hello, I make a application from where I upload the Image from local disk then store in DB.Before storing... isMultipart = FileUpload.isMultipartContent(req); // Create a new file upload
how to display image with listview
how to display image with listview  How can i display the list view with the related images from database
How to read and display data from a .properties file from a jsp page
How to read and display data from a .properties file from a jsp page  I have a .properties file. I have to create a jsp page such that it reads the data from this .properties file and display it in table format. Ex:by using
image upload and stored in database - JSP-Servlet
image upload and stored in database  How can i upload a image and store that image in a database
image upload and stored in database - JSP-Servlet
image upload and stored in database  How can i upload a image and store that image in a database
File Upload - JSP-Servlet
File Upload  Hi everyone, I am facing file uploading problem.the multiple file upload code of roseindia is working on localhost but the same code is not working on server.i think the package commons-fileupload-1.2.jar
image upload with jsp from form
image upload with jsp from form  hi i used the code specified in your...-upload-and-stored-in-database.html but i am getting following error...) org.apache.jsp.upload_jsp._jspService(upload_jsp.java:85
How to upload zip file from android to server programmatically?????
How to upload zip file from android to server programmatically?????  hi , I want to upload zip file from android phone sdcard to server programmatically. So any one do this task in past so please share your experience with me
How to insert image in sql database from user using servlet
How to insert image in sql database from user using servlet  pls tell me accept image from user and insert image in sql server 2005 database using servlet and jsp
file upload error - JSP-Servlet
file upload error  Hello friends, In my project when i am trying to upload a file i used the roseindia.net coding from URL: http://www.roseindia.net/jsp/file_upload/employee_upload_profile_image.shtml
How to retrieve image from database using jsp and servlet?
How to retrieve image from database using jsp and servlet?  Hi, I am trying to find code for displaying the image from database in a JSP page. How to retrieve image from database using jsp and servlet
passing the form values with image upload - JSP-Servlet
the request values from the fields rather than image . I want to know how can cut the values of other filds from the actual file that uploaded.Please give me... : 2. Enter value for field 2: 3. Upload file : and in your servlet you can
how to upload an image from a jsp page to a mysql database table using jsp
how to upload an image from a jsp page to a mysql database table using jsp  how to upload an image from a jsp page to a mysql database table using jspstrong text
display from select box - JSP-Servlet
display from select box  I am doing a jsp project. In this jsp.... Please help me.  Hi friend, For upload image visit to : http://www.roseindia.net/jsp/file_upload/ http://www.roseindia.net/jsp/file_upload
image upload and stored in database - JSP-Servlet
image upload and stored in database  How can i upload a image...: 1)page.jsp: Display file upload form to the user UPLOAD THE FILE   Choose the file To Upload:  
Upload and display image and text records using JSP and Oracle
Upload and display image and text records using JSP and Oracle  Hi all, I'm using JSP and Oracle 10g. I could retrieve and display records from database. But i couldnot retrieve and display image. I want to store
display the generated image from byte array - Struts
display the generated image from byte array  how to create image from byte array using struts and display it in struts html.please give me the sample code
File Upload in FTP - JSP-Servlet
File Upload in FTP  hi sir, i am doing upload files in FTP using java application. is there any jar file to use the privileges of sun.net.ftp. i am writing my program in java servlet. i am getting errors like 1.cannot Find
file upload error - JSP-Servlet
file upload error  Iam doing jsp project. File uploading is one part... ----------- file upload example..., I think jar problem. so Please add servlet-api.jar file in lib folder
Upload and download file - JSP-Servlet
the word document file: 1)page.jsp Display file upload form to the user UPLOAD THE FILE Choose the file To Upload: 2... successfully upload the file by the name of: <% Connection connection = null
File Upload Servlet 3.0 Example
File Upload Servlet 3.0 Example In this tutorial you will learn how to upload a file in servlet 3.0 In this example I have used the @MultipartConfig annotation to upload the file. Earlier versions than the servlet 3.0 specification were
Image upload in mysql database using jsp servlet
Image upload in mysql database using jsp servlet  Hello, I need code to insert image in mysql database, I have seen the code which is already in your... different format of file to upload into mysql db like pdf and doc file
FTP FILE UPload - JSP-Servlet
FTP FILE UPload  Hi sir, i am doing the file upload via ftp. the program compiled well . but while executing i am getting the below errors. exception javax.servlet.ServletException: Servlet execution threw an exception

Ads