Uploading image using jsp

Uploading image using jsp

how to upload image using jsp. Already i tried, But that image file does not read. It returns only -1 without reading that image file ...

I want know that solution using by u...

Thanks, P.S.N.

View Answers

August 25, 2012 at 12:35 PM

Here is a jsp code that upload image and display it on the browser.

1)page.jsp:

<%@ page language="java" %>
<HTML>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION="upload.jsp" METHOD=POST>
<br><br><br>
<table border="0" bgcolor=#ccFDDEE>
<tr>

<td colspan="2" align="center"><B>UPLOAD THE FILE</B><center></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td><b>Choose the file To Upload:</b></td>
<td><INPUT NAME="file" TYPE="file"></td>
</tr>
<tr>
<td colspan="2" align="center"> </td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</FORM>
</BODY>
</HTML>

2)upload.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%!
private byte[] getImage(String filename) {
byte[] result = null;
String fileLocation = filename;
File f = new File(fileLocation);
result = new byte[(int)f.length()];
try {
FileInputStream in = new FileInputStream(fileLocation);
in.read(result);
}
catch(Exception ex) {
System.out.println("GET IMAGE PROBLEM :: "+ex);
ex.printStackTrace();
}
return result;
}
%>
<%
String saveFile="";
String contentType = request.getContentType();
if((contentType != null)&&(contentType.indexOf("multipart/form-data") >= 0)){
DataInputStream in = new DataInputStream(request.getInputStream());
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
while(totalBytesRead < formDataLength){
byteRead = in.read(dataBytes, totalBytesRead,formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
saveFile = file.substring(file.indexOf("filename=\"") + 10);
saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
int lastIndex = contentType.lastIndexOf("=");
String boundary = contentType.substring(lastIndex + 1,contentType.length());
int pos;
pos = file.indexOf("filename=\"");
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
pos = file.indexOf("\n", pos) + 1;
int boundaryLocation = file.indexOf(boundary, pos) - 4;
int startPos = ((file.substring(0, pos)).getBytes()).length;
int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
File ff = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(ff);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();

response.reset();
response.setHeader("Content-Disposition", "inline");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Expires", "0");
response.setContentType("image/jpg");
byte[] image = getImage(saveFile);
OutputStream outputStream = response.getOutputStream();
outputStream.write(image);
outputStream.close();
}
%>









Related Tutorials/Questions & Answers:
Uploading image using jsp
Uploading image using jsp  how to upload image using jsp. Already i... that image file ... I want know that solution using by u... Thanks, P.S.N.   Here is a jsp code that upload image and display it on the browser. 1
Uploading an image - JSP-Servlet
Uploading an image  I am doing a jsp project. In this uploading an image is a part. For that i had done a coding for uploading... and uploading is done by different jsp coding. I don't know how to code for file size
Advertisements
video uploading using jsp
video uploading using jsp  how to upload a videos in web page using jsp   Hi, You can upload your video with the help of JSP file upload code. Once file is upload you can play using any video player. Get the code
Using a image for Browse button instead of normal html Browse button for Uploading files from a JSP - JSP-Servlet
Using a image for Browse button instead of normal html Browse button for Uploading files from a JSP  I am using the following code snippet.... Is it not possible to use a image instead of normal html Browse button for uploading files
video uploading using jsp
video uploading using jsp  this is the code i hv written for uploading..but the value in dropdown list is not captured..can any 1 tell the reason...; charset=ISO-8859-1"> <title>uploading page</title> </head>
file uploading using jsp
file uploading using jsp  below file uploading code has one error "toBytes[i-start]=fromBytes; " incompatable type error please help me how to solve... in uploading "); } %>   Hi Friend, Try the following code: 1
Uploading an image into the table - JSP-Servlet
Uploading an image into the table  how to upload an image... a connection by using Connection interface Connection connection = null; /* Create... binary stream of given image. FileInputStream fis; try { // Load JDBC
Uploading Multiple Files Using Jsp
Uploading Multiple Files Using Jsp  ... to understand how you can upload multiple files by using the Jsp. We should avoid... by using Jsp and how they will get stored on the particular memory area. To get
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored..where should we specify the output folder name..   Visit Here
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  Thanks deepak following my...("file.separator"); int last=path.lastIndexOf(seperator+"jsp"); String... to writing image file in this path.. in multipartRequest. if u have any
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  Dear All, I am facing some problem image uploading in server that i working fine in my system, OS Ubuntu...) com.oreilly.servlet.MultipartRequest.(MultipartRequest.java:89) org.apache.jsp.jsp.BSAdminProductsCreate_jsp
Uploading a single file by using JSp
Uploading a single file by using JSp  u have said about submit button..but in program u have not used submit button..and where file will be stored..where should we specify the output folder name..   1)page.jsp: <
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  dear friend... following is my uploading code and i want to save some records.... exception org.apache.jasper.JasperException: Exception in JSP: /jsp
Need help in image uploading - JSP-Servlet
Need help in image uploading  hii, i have one doubt in uploading an image in jsp. I want to display image from DB to my jsp page. For that i... to remote server..??? regards Sreejith Here is my code for image uploading
image uploading perminssion in server - JSP-Servlet
image uploading perminssion in server  thanks dear, but i am working in Linux Ubuntu, how can i set the path in server. my problem is i am not able to access the folders wherever we are uploading the files, we are not able
display image using jsp
display image using jsp  display image using jsp and phonegap on emulator of eclipse   Here is a simple jsp code that displays an image on browser. <%@ page import="java.io.*" %> <%@page contentType="image/gif
uploading a file at another system in lan using jsp
uploading a file at another system in lan using jsp  Thanks for the code at "http://www.roseindia.net/jsp/fileupload/Sinleupload.xhtml.shtml". It is working when i gave address of the local system to savefile But if i want
download image using url in jsp
download image using url in jsp  how to download image using url in jsp
Displaying image using jsp and spring.
Displaying image using jsp and spring.  how to display an image stored in WEB-INF/images folder on the browser using jsp and spring
upload image using JSP Hibernate
upload image using JSP Hibernate  sir, I want to take image from user and save to database(MYSQL) using Hibernate and JSP Thanks in advance
Image name,image path into database and image into folder using jsp
Image name,image path into database and image into folder using jsp  How to insert image path and image name into oracle database and image into folder using jsp
Image uploading
Image uploading  Hi,can anyone explain the following code. The code related to uploading an image file to oracle database using java servlet. CODE import java.io.*; import java.io.IOException; import
image upload&download using jsp - JSP-Servlet
image upload&download using jsp  can any one help in writing jsp... visit the following links: http://www.roseindia.net/jsp/fileupload.shtml http://www.roseindia.net/jsp/downloadimage.shtml Hope that the above links
error in uploading image from jsp to oracle 10g database
error in uploading image from jsp to oracle 10g database  ... try to insert into the image into the database i got the above error please advice how to get rid of error   JSP upload image in Mysql database
Image uploading in FTP Server
Image uploading in FTP Server  I want to upload images to a ftp server every 1 hour automatically using java..please help
how to fetch image from mysql using jsp
how to fetch image from mysql using jsp  how to fetch image from mysql using jsp
uploading image in the form
uploading image in the form  Hi All, I am working to build a form like railway registration form which accepts user id and password and image.../DisplayimageonJSPpageusingXML.shtml To know about file uploading using Struts2 you may go through the link, given
Multiple image upload using JSP
Multiple image upload using JSP  I have written a code to upload the multiple images using jsp, but when I execute this code, it throws Corrupt form...; JSP Code > <%@ page import="java.sql.*" %> <%@ > page > import
uploading image in php - PHP
uploading image in php  I am looking for a PHP script to upload image into mysql database
Crop image Using JQuery, servlet or JSP.
Crop image Using JQuery, servlet or JSP.  Hi sir I need the code of Cropping image using JQuery with the help of servlet or JSP. I need both the code Client side as well as of server side. Please Help me
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 portal but it is not inserting image into database it save in the folder
<img src=""> using retrieve image from database using jsp
using retrieve image from database using jsp  how to <img src="" > tag using retrieve image from database and display in jsp
change the name of the image after uploading the image
change the name of the image after uploading the image   my form consists of a textbox and uploading the image.after uploading the image i want to change the name of the image with the content of the textbox before saving
retrive image from database using jsp without stream
retrive image from database using jsp without stream  How to retrive image from database using jsp without stream like (inputStream
Uploading Multiple Image On Server?
Uploading Multiple Image On Server?  Hello sir, I am stuck with a problem of uploading multiple images on server. i have done a code which works fine for uploading single image,but it doesn't work with uploading multiple
insert name city and upload image in database using mysql and jsp
insert name city and upload image in database using mysql and jsp   insert name city and upload image in database using mysql and jsp
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
access image from ajax store in mysql using jsp
access image from ajax store in mysql using jsp  access image from ajax store in mysql using jsp (code to access image captured by camera and store in mysql
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
struts image uploading
struts image uploading  please let me know how to upload image in database using struts ..i have written form class and 'm hanging out in action class .... form FileUploadForm: package com.action.form; import
File uploading - JSP-Servlet
File uploading  i am using file uploading code for multiple file and aslo for single file but i am getting problem that No such file found.... http://www.roseindia.net/jsp/file_upload/Sinle_upload.xhtml.shtml
Uploading a Profile with image[file] into a sql database
Uploading a Profile with image[file] into a sql database  I need to upload a file along with the some text into a database ......[ Similar to http://www.roseindia.net/jsp/jspfileupload/employeeuploadprofile_image.shtml
struts image uploading
struts image uploading  please let me know how to upload image in database using struts ..i have written form class and 'm hanging out in action class .... form FileUploadForm: package com.action.form; import
struts image uploading
struts image uploading  please let me know how to upload image in database using struts ..i have written form class and 'm hanging out in action class .... form FileUploadForm: package com.action.form; import
struts image uploading
struts image uploading  please let me know how to upload image in database using struts ..i have written form class and 'm hanging out in action class .... form FileUploadForm: package com.action.form; import
how to store image in folder and stored image path in mysql database using JSP
how to store image in folder and stored image path in mysql database using JSP  how to store image in folder and stored image path in mysql database using JSP
How to store user name,city,state,image path into database and image into folder using jsp
How to store user name,city,state,image path into database and image into folder using jsp  How to store user name,city,state,image path into database and image into folder using jsp
Uploading a file using UploadBean
Uploading a file using UploadBean  Dear sir, In my project i have to upload the file and use the same file for getting a values from that uploaded .xls file.I used UploadBean for uploading .For the first time when i
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
retrive the employee details with image from mysql database using jsp servlet
retrive the employee details with image from mysql database using jsp servlet  im doing the web project to retrive the employee profile which i stored in the database using jsp servlet then want to show the result in the next jsp

Ads