JSP code
Hi!
Can somebody provide a line by line explanation of the following code. The code is used to upload and download an image.
<%@ 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>
/*Code to download an image*/
<%@ 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();
%>
View Answers
April 28, 2010 at 11:52 AM
Hi Friend,
In uploading a file,
The method request.getContentType() provides the content type information from the jsp header. Then check the two conditions ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)). If the condition is true, create an object of DataInputStream and pass the request.getInputstream() method of Inputstream class to its object. A DataInputstream read primitive Java data types from an underlying input stream.
To get the length of the content type, the method request.getContentLength() is used. Convert the uploaded file into the byte by using while loop and pass the array of dataBytes to the variable file. To save file name, we are using substring() method. After creating sub-string of string, extract the index of file. Then create a new file of same name and write the content in the file using FileOutputStream.
In downloading a file,
we have created a method that will return the mime type of the uploaded file which is then used for setting the content type and character encoding for the page. The setHeader() method creates a HTTP header and allow the browser to show a pop up a "save as" box. The whole data of the file has been read and written to the attached file using input and output streams.
Thanks
Related Tutorials/Questions & Answers:
jsp codejsp code what are the
jsp code for view page in online journal
JSP CODEJSP CODE what is the
code for downloading images from database using
JSP?
Please visit the following link:
http://www.roseindia.net/
jsp/downloadimage.shtml
Advertisements
jsp codejsp 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
jsp codejsp code i want health management system project
code using jsp.its urgent
JSP CODEJSP 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 jsp codejsp code i want to
jsp code which are working as,i have three drop down menu 1st menu for class second menu for subject and third menu as chapter... according to selectde menu,,,,please send me such type
code,,,,plz help me
jsp code - JSP-Servletjsp code Can anyone help me in writing
jsp/servlet
code to retrieve files and display in the browser from a given directory. Hi Friend,
Try the following
code:
Thanks
jsp codejsp code how to display date in drop down list by
jsp thus the date start from current date and for next year it has to check leap year function to february then it display 30 for some months else 31 for other months
jsp code - JSP-Servletjsp code sample
code for change password
example
Old Password:
new Password:
confirm Password
JSP codeJSP code I get an error when i execute the following
code :
<... = con.createStatement();
st.executeQuery(query);
%>
<
jsp:forward page="address.jsp"></
jsp:forward>
HTTP Status 500 -
type Exception report
message
jsp code - JSP-Servletjsp 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-Servletjsp code sample
code to create hyperlink within hyperlink
example:
reservation:
train:
A/C department
non A/c Department
jsp code - JSP-Servletjsp code how to count no of clicks made on a link and save it on database( using
jsp) Hi Friend,
Try the following
code:
1)click.jsp
var numberOfClicks = 0;
function hello(){
numberOfClicks
jsp code - JSP-Servletjsp code in a
jsp a table is existed ,in table each row contain one checkbox and name and desription how to delete specific rows in a table . Hi Friend,
Try the following
code:
1)form.jsp:
Name
JSP code - JSP-Servlet me how to link these pages with each other using
JSP..and how to write the
code using
JSP..
requriment for Newuser:
1>fname,lastname
2>Loginname...
JSP code hello,i working in project with
JSP technology,i have one
jsp code - JSP-Servletjsp code in my table there are 3 fields named orderid ,itemname, itemqty. i want
code that fetch orderid in dropdown... on select dropdown get table of item details of that orderid... there are multiple items at one orderid
JSP Code - JSP-ServletJSP Code Create a html reader
JSP tag that read the html page from a link and will display the contents on the
JSP. Do not use include directive
code in jspcode in jsp hi..
I had started a project on college admission system in which two operators has been introduced one is admin who can view all... want a
code to sort a list of students on the basis of there 12% marks and AIEEE
jsp code - JSP-Servletjsp code how to write a
code in
jsp with out using servelts finding sum and avg of 40 numbers Hi Friend,
Try the following
code:
Thanks Hi Friend,
Do one change in the provided code.As we
jsp code - JSP-Servletjsp code hi
my requirement is generate dynamic drop down lists... statement?
pls provide
code how to get first selected drop down list value to generate second drop down list by using
jsp?
pls ?
Hi Friend
jsp code - JSP-Servletjsp code the value did not passed from script and not inserted in to database in the
code u given .
var numberOfClicks = 0;
function hello(){
numberOfClicks++;
alert( numberOfClicks );
document.form1.text.value
JSP code - JSP-ServletJSP code hi i want to clear text box fields after click on submit.... thanking u.........
This is what my
code is.
Jam Name...;
Hi Friend,
Try the following
code:
Jam Name
jsp code - JSP-Servletjsp code Hello Everybody,
can anyone help me to findout the modules as i am developing a whiteboard application using
jsp?
this application is my dream application.
Thank you
JSP Code - JSP-ServletJSP Code Hi,
Do we have a datagrid equivalent concept in
JSP...,
Please visit the following links:
http://www.roseindia.net/
jsp/data-grid.shtml
http://www.roseindia.net/
jsp/paging.shtml
Thanks
jsp code - JSP-Servletjsp code i want to add below
code data in mysql database using
jsp... using below
code we got data in text box i want to add multiple data in database...
Add/Remove dynamic rows in HTML table
JSP Code - JSP-ServletJSP Code Hi,
I have a problem in limiting the number of row... to display only 10 records per pages in
jsp, then how can i achieve this concept... Rai. Hi Friend,
Try the following
code:
Pagination
jsp code - JSP-Servletjsp code hello frns
i want to display image from the database along... from database in
Jsp to visit....
http://www.roseindia.net/
jsp/retrieve-image.shtml
http://www.roseindia.net/
jsp/downloadimage.shtml
Thanks
jsp code - JSP-Servletjsp code hi
i am doing project work i am generating time table for this i have taken form
courseyear textbox
semistername textbox
no of periods... will be allotted to six days.
pls send me
code for this pls
thankyou
jsp code - JSP-Servletjsp code how to get multi-chat client using
jsp?
i will be very glad if i'd get an idea from you people.
thank you . Hi Friend,
Chat server is a standlone application that is made up the combination of two
JSP code - JSP-ServletJSP code Hi!
Can somebody provide a line by line explanation of the following
code. The
code is used to upload and download an image.
<... have successfully upload the file by the name of:
Download
/*
Code jsp code - JSP-Servletjsp code i want to add and remove rows dynamically ... in that row 3 colums are there... first colum shows one number , second colum shows second... colum. Hi Friend,
Try the following
code:
Add/Remove
jsp code - JSP-Servletjsp code i want to design page into four pages
1) header to diplay
2) left page has to display hyperlink
3)right page has to display related page of hyperlink
4) bottom page has to display footer
example
header
jsp code - JSP-Servletjsp code i want to design page into four pages
1) header to diplay
2) left page has to display hyperlink
3)right page has to display related page of hyperlink
4) bottom page has to display footer
example
header
Jsp code - JSP-ServletJsp code
function Calculate()
{
var fpval;
var cityamt;
var newspaper;
var sizeval;
var city = document.form.city.value;
var noofwd = document.form.Count.value;
var nofw
Jsp code - JSP-ServletJsp code PlaceAd.jsp
function Calculate()
{
var fpval;
var cityamt;
var newspaper;
var sizeval;
var city = document.form.city.value;
var noofwd = document.form.Count.value
JSP code for forget passwordJSP code for forget password I need forget password
JSP code..
example http://www.roseindia.net/users/forgotpassword.html
tree using jsp codetree using
jsp code i want to draw a tree structure of a family hierarchy using
jsp code Jsp Code Problem - JSP-ServletJsp 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
JSP code problem - JSP-ServletJSP 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 excel code - JSP-Servletjsp excel code Hi
how to insert form data into excel file using
jsp? Hi Friend,
Try the following
code:
1)register.jsp:
Registration Form
First Name:
Last Name:
User Name:
Password
jsp logout codejsp logout code hello sir,
im doing
jsp project, i have implemented the session
code of
jsp.
session.invalidate(). but its not working. can u tell me what may be the problem. OR if possible pls send me the proper
code jsp code problem - JSP-Servletjsp code problem Hi,
I have employee details form in
jsp. After... friend,
Please give me detail and send me error
code page.
Please....
http://www.roseindia.net/
jsp/
Thanks
Searching for Code - JSP-ServletJSP, Servlet Searching for Code Hi, i am looking for a
jsp servlet
code examples for the search function in my application.Thanks
jsp code error - JSP-Servletjsp code error hello,
is anyone here who can solve my problem.
what happen experts where r u? r u not able to do
jsp code error - JSP-Servletjsp code error hello,
is anyone here who can solve my problem.
what happen experts where r u?
or
u r not able to do
need JSP Code.need
JSP Code. **Hi I need
JSP code for selling Products which are in Database.
Please can any one help for this.URGENT...
Thank You..!
Abhijeet