JSP code problem

JSP code problem

Hi friends,
I used the following code for uploading an image file. After compilation it shows the required message, "Image successfully uploaded", but when i try to retrieve the image I'm unable to do so. The uploaded image is not getting saved anywhere.
The following is the code:

<%@ page language="java" %>
<HTml>
<HEAD><TITLE>Display file upload form to the user</TITLE></HEAD>
<% // for uploading the file we used Encrypt type of multipart/
form-data and input of file type to browse and submit the file %>
<BODY> <FORM ENCTYPE="multipart/form-data" ACTION=
"sinle_upload_page.jsp" METHOD=POST>
<br><br><br>
<center><table border="2" >
<tr><center><td colspan="2"><p align=
"center"><B>PROGRAM FOR UPLOADING THE FILE</B><center></td></tr>
<tr><td><b>Choose the file To Upload:</b>
</td>
<td><INPUT NAME="F1" TYPE="file"></td></tr>
<tr><td colspan="2">
<p align="right"><INPUT TYPE="submit" VALUE="Send File" ></p></td></tr>
<table>
</center>
</FORM>
</BODY>
</HTML>





<%@ page import="java.io.*" %>
<%
//to get the content type information from JSP Request Header
String contentType = request.getContentType();
//here we are checking the content type is not equal to Null and
as well as the passed data from mulitpart/form-data is greater than or
equal to 0
if ((contentType != null) && (contentType.indexOf("multipart/
form-data") >= 0)) {
DataInputStream in = new DataInputStream(request.
getInputStream());
//we are taking the length of Content type data
int formDataLength = request.getContentLength();
byte dataBytes[] = new byte[formDataLength];
int byteRead = 0;
int totalBytesRead = 0;
//this loop converting the uploaded file into byte code
while (totalBytesRead < formDataLength) {
byteRead = in.read(dataBytes, totalBytesRead,
formDataLength);
totalBytesRead += byteRead;
}
String file = new String(dataBytes);
//for saving the file name
String 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;
//extracting the index of file
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;
// creating a new file with the same name and writing the
content in new file
FileOutputStream fileOut = new FileOutputStream(saveFile);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully
upload the file by the name of:</b>
<% out.println(saveFile); %></td></tr></table> <%
}
%>




Please can somebody rectify this problem........
View Answers

April 22, 2010 at 4:32 PM

Hi Friend,

Try the following code:

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=
"uploadandstore.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">&nbsp;</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">&nbsp;</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Send File"> </td>
</tr>
<table>
</center>
</FORM>
</BODY>
</HTML>

2)uploadandstore.jsp:

<%@ page import="java.io.*" %>
<%@ page import="java.sql.*" %>

<%
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;
saveFile="C:/"+saveFile;
File f = new File(saveFile);
FileOutputStream fileOut = new FileOutputStream(f);
fileOut.write(dataBytes, startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><Br><table border="2"><tr><td><b>You have successfully upload the file by the name of:</b>
<% out.println(saveFile);%></td></tr></table>
<%
}
%>
<a href="download.jsp?f=<%=saveFile%>">Download</a>

April 22, 2010 at 4:32 PM

continue..

3)download.jsp:

<%@ page import="java.util.*,java.io.*"%>
<%@ page import="java.net.*"%>
<%!
public static String getMimeType(String fileUrl)
throws java.io.IOException, MalformedURLException
{
String type = null;
URL u = new URL(fileUrl);
URLConnection uc = null;
uc = u.openConnection();
type = uc.getContentType();
return type;
}

%>
<%
String file=request.getParameter("f");
File f = new File (file);
String filename=f.getName();
String type=getMimeType("file:"+file);

response.setContentType (type);
response.setHeader ("Content-Disposition", "attachment; filename=\""+filename+"\"");

String name = f.getName().substring(f.getName().lastIndexOf("/") + 1,f.getName().length());
InputStream in = new FileInputStream(f);
ServletOutputStream outs = response.getOutputStream();

int bit = 256;
int i = 0;
try {
while ((bit) >= 0) {
bit = in.read();
outs.write(bit);
}
} catch (IOException ioe) {
ioe.printStackTrace(System.out);
}
outs.flush();
outs.close();
in.close();
%>


Thanks









Related Tutorials/Questions & Answers:
jsp code problem - JSP-Servlet
jsp code problem  Hi, I have employee details form in jsp. After... have a problem with open the next form. plz, help me. thanks,  Hi friend, Please give me detail and send me error code page. Please
Jsp Code Problem - JSP-Servlet
Jsp Code Problem  I use DocType in my Jsp Page. The Links are not functioned after Applying the DocType. Could you tell me any way to activate the link. Thank You.   Hi Friend, Please send your code. Thanks
Advertisements
JSP code problem - JSP-Servlet
JSP code problem  HI.. I have a DB2 stored procedure wich return a result set. I have made a report basing on this procedure using Crystal Reports. How to pass parameters to this procedure with java code in a JSP page
jsp code problem - JSP-Servlet
jsp code problem  i want to make the bill from the barcode. how do i convert a barcode into a decimal number
code for this problem - JSP-Servlet
code for this problem   i have buttom submit .It is working with single click.Suppose if i am At the same time clicking(parallel) twise second request will be stoped until process the proces first requst and terminate
JSP code problem - JSP-Servlet
JSP code problem  Hi friends, I used the following code... is the code: Display file upload form to the user...: <% //to get the content type information from JSP
jsp code problem - JSP-Servlet
jsp code problem  hi, I am going to execute the following code which has been given your jsp tutorial. retrive_image.jsp: but while I try to execute the code it gives the following error message in the tomcat
jsp code problem - Java Beginners
jsp code problem  Hi, I have a problem with else part. It did not show the message box when the result set is null. plz, help me. thank u in advance
javascript code problem - JSP-Servlet
; "> in above code which is jsp and struts form bean...javascript code problem  Thanks for sending answer.but actually what u send is not my actual requirement.first look this code. Subject
servlet code problem - JSP-Servlet
servlet code problem  This is my JSP code index.jsp Sync Data Sync Data Please use the following input box to upload file or enter... showing null value and i downloaded this code from net but this also not showing
servlet code problem - JSP-Servlet
servlet code problem  This is my JSP code index.jsp Sync Data Sync Data Please use the following input box to upload file or enter the user id in input box directly. Upload File: Enter UserId
Date Time Problem In Jsp code - Development process
Date Time Problem In Jsp code  Hi Friends, By using this code , am storing date and time into msaccess database. But while retriving i want to get same date and time .send me code for that. SimpleDateFormat
Java Script Code Problem - JSP-Servlet
Java Script Code Problem  how to concatenate html table values(there are 3 rows,each row contains 3 fields) into variable and display it using javascript  Hi Friend, Try the following code: document.write
this code will be problem it display the error again send jsp for registration form
this code will be problem it display the error again send jsp for registration... RESEND THE CODE org.apache.jasper.JasperException: java.lang.NumberFormatException... in database as text and set the mobile field or telephone field as string in your code
this code will be problem it display the error again send jsp for registration form
this code will be problem it display the error again send jsp for registration... RESEND THE CODE org.apache.jasper.JasperException: java.lang.NumberFormatException... in database as text and set the mobile field or telephone field as string in your code
Problem in jsp.
Problem in jsp.  hello friends, I have a problem in jsp.I want to insert data, which is given by user through a html page into a table.And the table..." action="http://localhost:8080/examples/jsp/insertdata.jsp"> <table> <
Problem in jsp.
Problem in jsp.  hello friends, I have a problem in jsp.I want to insert data, which is given by user through a html page into a table.And the table..." action="http://localhost:8080/examples/jsp/insertdata.jsp"> <table> <
Problem in jsp.
Problem in jsp.  hello friends, I have a problem in jsp.I want to insert data, which is given by user through a html page into a table.And the table..." action="http://localhost:8080/examples/jsp/insertdata.jsp"> <table> <
jsp problem
jsp problem  problem:::::::: On JSP form ,when i insert data in text field........at that time action is perform and data is retriev from data base... (Exception e) { System.out.println(e); } %> 3)For the above code, we have
Jsp problem
Jsp problem  Hello friends.I want to share my jsp problem.I want to show all of my database(mysql) tables except two(user,manufacturer) in a jsp page.plssss help me
jsp problem
jsp problem  Hello Friends, I want to show one page for 5 seconds and after that i want to redirect the page to another URL
jsp problem - JSP-Servlet
jsp problem  here is a code for Retrieving image from mysql database through jsp. but i cann't set size of image when it was display so what should... working but my problem is size of image. so can u just help?? code
jsp problem - JSP-Servlet
jsp problem  here is a code for Retrieving image from mysql database through jsp. but i cann't set size of image when it was display so what should... working but my problem is size of image. so can u just help?? code
jsp problem
jsp problem  Hi every one plz give me answer for below question ASAP I created one JSP(used pre defined javascript in jsp to get rtf format in browser) which creats RTF Format in browser to enter data. MY question is after i
problem with code - Ajax
problem with code  hi friends i am sending a code in that when we select the countries states have to be selected by using only jsp in that we have to use ajax i wrote the code of jsp and the remaning code by using ajax
problem in jsp programming - JSP-Servlet
problem in jsp programming  Write a JSP program which displays... branches of a retail company.   Hi friend, Code to help in solving the problem : Connection with mysql database
JSP Problem - JSP-Servlet
JSP Problem  Hi, I have problem,this is my senario I have one jsp,this jsp having the 3 fields.let me take 3fields userName,emailAddress... for more information, http://www.roseindia.net/jsp Thanks
hibernate code problem - Hibernate
/selectclause.shtml If you have any problem this send me detail and source code...hibernate code problem  suppose i want to fetch a row from the table through a value which i got from the jsp page. how can i do it and iterate
jsp code - JSP-Servlet
jsp code  I need code for bar charts using jsp. I searched some code but they are contain some of their own packages. Please give me asimple code... friend, Code to solve the problem : Thanks
jsp code
jsp code  what are the jsp code for view page in online journal
Session Problem in JSP - JSP-Servlet
normally from Remote machine by only writing JSP code.  Hi friend...Session Problem in JSP  I have developed a online feedback form in JSP platform. I have created normal session in JSP page. It is running in my
jsp problem - JSP-Servlet
jsp problem  hi, i am working on a project of developing a shopping cart for online book store.can it be done using jsp?if yes, can u please help me doing so?  Hi Friend, Please visit the following link: http
JSP CODE
JSP CODE  what is the code for downloading images from database using JSP?   Please visit the following link: http://www.roseindia.net/jsp/downloadimage.shtml
jsp code
jsp code  hi i am Ruchi can anybody plz tell me the jsp code... visit the following links: http://www.roseindia.net/jsp/user-search.shtml http://www.roseindia.net/servlets/search.shtml www.roseindia.net/jsp/searchbook.shtml
code problem:ajax - Ajax
code problem:ajax  Hi,I am using ajax to populate a select box.for this I am writing out.write("ONE"); like that.it runs fine in firefox.bt not in IE.Can anyone help me out this... thanks
problem in programming - JSP-Servlet
problem in programming  Hi! I am new with jsp. I am facing a problem in programming to calculate the time interval between login time and logout time of user
Hibernate code problem - Hibernate
Hibernate code problem  Hi, This is Birendra Pradhan.I want... in the DAO.Can it be possibe. Please send some sample code.. thanks & Regards Birendra  Hi friend, For solving the problem visit
code problem - Java Beginners
code problem  Dear sir, my problem is that I've a string value if this String value has "quit" then output should be "bye". i want to make this program using SWITCH CASE statement. how to implement String value in Switch plz
jsp code
jsp code  i want health management system project code using jsp.its urgent
code problem - Java Beginners
code problem  Dear sir, I'm havin a problem that suppose i've got... java script j2ee j2me sql plz help me to sort out this problem. thnx   Hi Friend, Please try the following sample code to solve your problem
Hibernate code problem - Hibernate
problem please send me code. Visit for more information. http...Hibernate code problem  Hi This is Raju.I tried the first example........How can i solve this problem. Am i need to Kept any other jars in path. 
JSP Code - JSP-Servlet
JSP Code  Hi, Do we have a datagrid equivalent concept in JSP? If so, then please help me to find the solution of the problem. Its Urgent..., Please visit the following links: http://www.roseindia.net/jsp/data-grid.shtml
Problem with code - Java Beginners
Problem with code  Hi Deepak. Im a newbie here at your forum. I have got a simple code of mine which is having a little problem. When I compile it, i get an...,identifier expected'...error. Could you help me out? Thank you
jsp usebean problem - Struts
popupwindow jsp below code run.. ...here iam getting the problem..in the below...jsp usebean problem   --Select... jsp
code problem - Java Beginners
code problem  Dear sir, My problem is that i have some string value and in some case i want to remove all the value of this string, i tried this code- Response.str.clear(); but it shows some error called "response package
code problem - Java Beginners
code problem  Dear sir, my problem is given below: suppose a file Carries the following lines- Name: john age: 45 Address: goa phone...; Hi friend, Code to help in solving the problem : import java.io.
code problem - Java Beginners
code problem  Dear sir, my question was actually how to find a particual line's contain, example if a file contains- hy, how r u? regrd, john... your problem in details. Which keyword search the line. Thanks
Code Problem - Struts
Code Problem  i want to insert multiple row values (from html table which is in jsp) into oracle database using struts.for example oracle table contains 3 fields as sub1,sub2,sub3.html table contains 10 rows of sub1,sub2,sub3
JSP CODE
JSP CODE  Please help me as soon as possible.Its Urgent. I am working on my college ALUMNI PORTAL. I want to have a ADD FRIEND option in a user's profile. Please send me code
code problem - Java Beginners
of program. thnx  Hi friend, Code to help in solving the problem...code problem  Dear sir, I've some integer value in ArrayList like- 10 40 30 i want to add them and print should be like- "total value

Ads