Having problem with image upload....

Having problem with image upload....

I am uploading profile pictures in my site.But after uploading when I visit my home page where it is supposed to show my profile picture,it's not showing.My profile picture is getting uploaded successfully and there is no doubt about that because I checked on the server that the image is getting uploaded.I also right clicked on the home page and went through it's source.At the place of my profile picture I found the url of the image absolutely correct.But when I click on the url it is saying requested resource is not found on server.The same problem is occurring in case of updating.Getting updated my profile picture successfully but not showing while the url is correct.I have renamed the image after uploading with time stamp so as to prevent caching problems.Please help me.I am using JSP.

View Answers

April 6, 2012 at 12:39 PM

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>
<center>
<table border="0" bgcolor=#ccFDDEE>
<tr>
<center>
<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>
</center>
</FORM>
</BODY>
</HTML>

2)upload.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.zip.*"%>
<%
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();
%><Br><table border="2"><tr><td><b>You have successfully upload the file:</b>
<%out.println(saveFile);%></td></tr></table>
<%
Connection connection = null;
String connectionURL = "jdbc:mysql://localhost:3306/test";
ResultSet rs = null;
PreparedStatement psmnt = null;
FileInputStream fis;
try{
Class.forName("com.mysql.jdbc.Driver").newInstance();
connection = DriverManager.getConnection(connectionURL, "root", "root");
File f = new File(saveFile);
psmnt = connection.prepareStatement("insert into file(file_data) values(?)");
fis = new FileInputStream(f);
psmnt.setBinaryStream(1, (InputStream)fis, (int)(f.length()));
int s = psmnt.executeUpdate();
if(s>0){
out.println("Uploaded successfully !");
ResultSet rs=st.executeQuery("select file_data from file");
if(rs.last()){
byte[] bytearray = new byte[1048576];
int size=0;
sImage = rs.getBinaryStream(1);
response.reset();
response.setContentType("image/jpeg");
while((size=sImage.read(bytearray))!= -1 ){
response.getOutputStream().write(bytearray,0,size);
}
}
}
else{
System.out.println("Error!");
}
}
catch(Exception e){e.printStackTrace();}
}
%>









Related Tutorials/Questions & Answers:
Having problem with image upload....
Having problem with image upload....  I am uploading profile pictures... that the image is getting uploaded.I also right clicked on the home page... of the image absolutely correct.But when I click on the url it is saying requested
problem to do coding upload image in php
problem to do coding upload image in php  can i get example coding how to upload image in php?i have try it but i dont no wheter it is correct or not because i can't save the image in folder and not connected to database mysql
Advertisements
upload an image
upload an image  Hello, i would like to upload an image to the database. how can i do it? what field type should i set in the database? thanx
upload image
upload image  I want to upload image from user using Jdbc connectivity and servlets and Html pages. I tried code but getting error using multi parts,so please send the code
upload image
upload image  how can i retreive image from mysql using jsp in netbeans.The image type is varchar with size 200.the image is uploaded succesfully.The problem is when i use the retreival code,it displays only a small box instead
pojo class having problem
pojo class having problem  Cannot find any information on property 'Username' in a bean of type 'LoginBean.LoginBeanClass' org.apache.jasper.runtime.JspRuntimeLibrary.handleSetProperty(JspRuntimeLibrary.java:664
upload image data
upload image data  Hii Upload image dat in csv files
upload image data
upload image data  Hii Upload image dat in csv files
Upload image
Upload image  Hai i beginner of Java ME i want code to capture QR Code image and send to the server and display value in Mobile Screen i want code in Java ME .java extension.. Regards senthil   To capture an image
image upload in webapp/upload folder
image upload in webapp/upload folder  sir i want to store upload image in my project directory WebApp/Upload_image/ pls send the jsp servlet code when i upload the image one error found "system cannot found the specified path
image upload
image upload  Hello sir I want to upload image or any other type... be upload in the server and their path should be stored in database either in oracle or my sql. kindly help me.   JSP Upload file and save file path
Upload image
Upload image  Hai team, I beginner of Java me now i crated code for to capture image and upload to server. Here i taken a snap but when am going... help me.  Jsp Upload Image 1)page.jsp: <%@ page language="java
upload a image
upload a image  sir,how can i upload a image into a specified folder...;&lt;HEAD>&lt;TITLE>Display file upload form to the user&lt...;center">&lt;B>UPLOAD THE FILE&lt;/B>&lt;center>&lt
image upload in java
image upload in java  Hi, I am working with java. In my application i want to give facility to user to add and change image. I use open dialog box to select image, it will work properly i.e on button click open dialog is open
image upload
image upload  How to access image file from database using jsp?  ... rs=st.executeQuery("SELECT images FROM image WHERE id = 1"); if(rs.next...("image/jpeg"); while((size=sImage.read(bytearray))!= -1
To scan a image and upload to server
To scan a image and upload to server  I am beginner of JavaME I want a code to scan a image and upload to server
scan a image and upload to server
scan a image and upload to server  Hai i am beginner of Java ME..I want the code To scan a image and upload to server
image upload - Struts
image upload  Can any one help me how to upload an image using struts
Text Editor Image upload
Text Editor Image upload   how to browse an image from text editor instead of giving url of particular image using javascript
Having problem to set font and color
Having problem to set font and color  Hello Sir, i have some problem to set font and color in my program.Can you take a look for the moment please? Here is my program: import java.awt.event.*; import javax.swing.*;ADS
Capture a image and upload to server
Capture a image and upload to server  Hai team i have trouble to capture a image and upload to server in java me........... So i want a code immediately,.......To capture a image and upload to server in j2me...... If you give
scan a image and upload to server
scan a image and upload to server  I have code for capture image and upload to server..after capture that image decoded it going to be convert digit value....My client want to scan a image and upload to server in j2me please
upload image to database
upload image to database  i am try to upload image to MySql database using netbeans. when jsp execute it return no error. but also data does inserted in database. i am using blob datatype and preopared statement
hibernate image Upload
hibernate image Upload  Sir, I had posted question regarding upload image, th answer you had given is using SQL, but I am using Hibernate to insert data in the same table where I want to insert image. Plz hlp me. Thanks
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
Upload image to the server in J2ME?
Upload image to the server in J2ME?  HI Team, I am new to the j2me and now I am working on the program that, upload captured image to the server by using j2me. I did this by using simulator but when I made hit from the mobile
Image Upload Error
Image Upload Error  hi, I created one education website in jsp....in my registration page image uploading option also i attached in real path...it's working well in local file....when i upload file in live Using FTP
upload image in php
upload image in php  after select image,how to save it in a folder...; <td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td> </tr> </table> </form> 2
struts2 upload image public_html
struts2 upload image public_html  How to upload images in public_html file in Struts 2 application. Thanks in advance
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
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
I am having problem with configuring Hibernate sessionfactory. Please Help
I am having problem with configuring Hibernate sessionfactory. Please Help  Sir, I am having problem with configuring Hibernate sessionfactory.. I was using the old version of Hibernate before now I am using Hibernate 4
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
ModuleNotFoundError: No module named 'dynamic-upload-image-field'
ModuleNotFoundError: No module named 'dynamic-upload-image-field'  Hi...: No module named 'dynamic-upload-image-field' How to remove the ModuleNotFoundError: No module named 'dynamic-upload-image-field' error? Thanks
ModuleNotFoundError: No module named 'dynamic-upload-image-field'
ModuleNotFoundError: No module named 'dynamic-upload-image-field'  Hi...: No module named 'dynamic-upload-image-field' How to remove the ModuleNotFoundError: No module named 'dynamic-upload-image-field' error? Thanks
ModuleNotFoundError: No module named 'multiimages_image_upload'
ModuleNotFoundError: No module named 'multiimages_image_upload'  Hi...: No module named 'multiimages_image_upload' How to remove the ModuleNotFoundError: No module named 'multiimages_image_upload' error? Thanks  
ModuleNotFoundError: No module named 'django-ajax-image-upload'
ModuleNotFoundError: No module named 'django-ajax-image-upload'  Hi...: No module named 'django-ajax-image-upload' How to remove the ModuleNotFoundError: No module named 'django-ajax-image-upload' error? Thanks  
ModuleNotFoundError: No module named 'dynamic-upload-image-field'
ModuleNotFoundError: No module named 'dynamic-upload-image-field'  Hi...: No module named 'dynamic-upload-image-field' How to remove the ModuleNotFoundError: No module named 'dynamic-upload-image-field' error? Thanks
Multiple image upload using JSP
Multiple image upload using JSP  I have written a code to upload...; fileSavePath_upload_user_dir = fileSavePath_upload_dir + > File.separator + user_image...="text/html; charset=utf-8" /> <FORM action="upload_jsp.jsp" enctype
Problem in uploading image to to mysql database
Problem in uploading image to to mysql database  Hi, need some help here, i have a program where a user can input name, city and upload image. when... have no problem in saving the image in the folder, my problem is it can't save
Problem in uploading image to to mysql database
Problem in uploading image to to mysql database  Hi, need some help here, i have a program where a user can input name, city and upload image. when... have no problem in saving the image in the folder, my problem is it can't save
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 is an example of uploading an image on the server. The user can browse the image
Image problem Urgent - JSP-Servlet
Image problem Urgent  Respected Sir/madam, I am R.Ragavendran.. Actually I got your coding.. But This code is not enabling the user to click and change the image.. I request you to remember the code which you have
upload image and fields.....fields is id name.....
upload image and fields.....fields is id name.....  Get Data using Java Servlet The frame takes following input.. Id: Name: browse:Image are file
upload image and fields.....fields is id name.....
upload image and fields.....fields is id name.....  Hi this is sreenu my problam is capcharing data to the servler thi is my frame Id: Name: browse:Image are file
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
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 the Image in DB I want to check this program. I include the commons
having problem with menu ans sub menu items css ...plz help!!
having problem with menu ans sub menu items css ...plz help!!  PLZ help ...this is my html menu > <div id="content"> <div > class="navigation" id="nav"> > <div class="item user"> >
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

Ads