servlet question

servlet question

sir please provide a web apps program in which it has a button through which i can download my uploaded image.

View Answers

November 19, 2012 at 6:07 PM

Here is a code to upload and download the file.

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"> </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)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>

November 19, 2012 at 6:07 PM

continue..

3)download.jsp:

<%@ page import="java.util.*,java.io.*"%>
<%
String file=request.getParameter("f");
File f = new File (file);
String filename=f.getName();
response.setContentType ("application/text");
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();
%>









Related Tutorials/Questions & Answers:
servlet question
servlet question  which method is used by destory() in servlet to release the resources and destroy the instances
servlet question
servlet question  I have a jsp page m_box.jsp in SchoolProject/Student/ folder. now from here we click a button and go to servlet which is at com.edao package but it always go SchoolProject/Student/Inbox where Inbox is servlet
Advertisements
servlet question
servlet question  sir please provide a web apps program in which it has a button through which i can download my uploaded image.   Here is a code to upload and download the file. 1)page.jsp: <%@ page language="java
Servlet Question - JSP-Servlet
Servlet Question  I want to call one servlet when the browser window is closed by User
Servlet Question - JSP-Servlet
Servlet Question  I want to call one servlet when the browser window is closed by User
servlet question on web application
servlet question on web application   how to write a programg on the web application. create two modules like admin and customer. customer can registrate with user id and password. after finishing of his registration he'll get
servlet question on web application
What is servlet context?   What is servlet Context? Given me an example
interview question - Servlet Interview Questions
of question on Servlet, that can be simple or complex one depend on your... according to your experience.And for collection of Java Servlet Interview Question you...interview question  What is Servlet? Need interview questions on Java
servlet question on web application
How can we send request from JSP to servlet?  How can we send request from JSP to servlet
question paper - JSP-Servlet
question paper  I am doing a project in jsp of creating question paper from question bank. Here we have to generate a paper based on the language... question from each area like inheritence,exception handling.the question has
jsp servlet question
jsp servlet question  I have an HTML form which has a couple of radio buttons for example (gender: male/female) and some check boxes with select lists. I have created a servlet to connect to access database and process the form
Question about JSP - JSP-Servlet
Question about JSP  Please tell me how to retrieve an image from SQL Server 2000 using JSP code.I know how to store the image as it stores in binary format. But i don't know hoe to retrieve
Servlet question of saving song from database to user's hard disk. - JSP-Servlet
Servlet question of saving song from database to user's hard disk.  I... files.There is also a "Download" button in the form. When I click on that button a servlet... no problem till now. My problem is that when the servlet finds that file
pl z provide code for below question in javascripr or jsp - JSP-Servlet
pl z provide code for below question in javascripr or jsp  Once the parallel request happened, the user will not be able to continue his work, until... Friend, Please clarify your question. Thanks
Jsp question - JSP-Servlet
JSP Question - JSP-Servlet
servlet question on web application
servlet question on web application
servlet question on web application
servlet question on web application
servlet question on web application
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
question
question   sir plz tell me what should i give in title box. just i want java program for the question typed in this area
Question
Question   When there is an exception in my program how java runtime system handles
question
question  dear sir/madam my question is how to compare two text format in java..we are java beginners..so we need the complete source code for above mentioned question...we have to compare each and every word
Question
Question  1- what is Servlet work flow 2- how to identify the Jsp Size(by default
question
question  Dear sir i had some typing mistake at previous question so its my humble request to let me know the steps to start the tomcat6 under the tomcat directory
question
question  Hi good morning sir, how to send an error message to an html page after checking the database for different users.for eg:your user name or password is incorrect.reply me with code, without using bean.using servlet,html
question
question  Gud morning sir, I have asked u some question regarding jsp in saturaday for that i didnot find any answere in which u send me the some of the links.U have asked me the specify some details. There is a entity name
Question?
Question?  My question is how to: Add a menu bar to the program with a File menu. In the File menu, add a submenu (JMenuItem) called About. When the user clicks on the About menu item, display a JOptionPane message dialog
question
;/tr> </table> this is my jsp page package servlet; import java.io.
to display single row from database and next to display other question - JSP-Servlet
questions in database we have to display first question on clicking the button it has to display the next question. i tried it first question is displayed, but i am facing a problem while retieving the next question in the jsp page. its
Servlet
Servlet  What is Servlet
Servlet
Servlet  how to navigate one servlet page to another servlet page
Servlet Interview Questions - Page 2
; tag.ADS_TO_REPLACE_2 Question: What mechanisms are used by a Servlet Container... patterns are processed by Struts Controller Servlet. Question: What must... by all servlets.ADS_TO_REPLACE_3 Question: What are the differences between Servlet
servlet
servlet  is there any way to include pdf's in servlet
servlet
servlet  How many times the servlet is accessed
servlet
servlet  what are the methods and interfaces in the servlet api ?   Servlet Tutorials
servlet
servlet  what are the all necessary configuration to run a servlet
servlet
servlet  how to interact with a servlet from a swing program
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  I designed 1 html form & a servlet but when I click on form I don't get output of servlet Please help
servlet
servlet  i want to create a login page with servlet using database mysql? only in servlet not in jsp plzz help me out
Servlet
override Service method in the servlet when you extend GenericServlet to create servlet as it is mandatory to override it. But, when you extend HttpServlet to create a servlet then you can't override service method as there is a need to override
servlet - Servlet Interview Questions
question in detail? You can read more about servlet at http://www.roseindia.net..., Service method in called by the servlet container to process a request from the browser. When user calls a servlet, it's service method is executed
servlet
servlet  i want a program for counting the no of times the servlet has been invoked
Servlet
Servlet  What must be implemented by all Servlets?    The Servlet Interface must be implemented by all servlets
servlet
servlet  how to create a login form using servlet using submit,edit delete button
servlet
servlet  dear sir servlet and html not run on eclips plz help me
servlet
servlet  can i stoar record in variable which selected from table in servlet

Ads