File Download in jsp

File Download in jsp

file upload code is working can u plz provide me file download

View Answers

March 26, 2011 at 12:53 AM

<%@page contentType="application/octet-stream"%>
<%@page pageEncoding="UTF-8"%>
&lt;%@ page language="java" import="java.io.<em>,java.net.</em>,java.util.<em>,javax.servlet.</em> "%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
&lt;%
BufferedInputStream filein = null;
BufferedOutputStream outputs = null;
try {
File file = new File("./path/filename.csv");//specify the file path
byte b[] = new byte[2048];
int len = 0;
filein = new BufferedInputStream(new FileInputStream(file));
outputs = new BufferedOutputStream(response.getOutputStream());
response.setHeader("Content-Length", ""+file.length());
response.setContentType("application/force-download");
response.setHeader("Content-Disposition","attachment;filename=Backup.csv");
response.setHeader("Content-Transfer-Encoding", "binary");
while ((len = fin.read(b)) > 0) {
outputs.write(b, 0, len);
outputs.flush();
}
}
catch(Exception e){
out.println(e);
}
%>
</body>
</html>

May 16, 2012 at 12:01 PM

*Create 2 Files* 

 **DownloadPage.JSP code :-** 

&lt;a href=&#039;downloadPdf.jsp?file=FILE.pdf&#039; &gt;Download PDF File&lt;/a&gt;

**downloadPdf.JSP code :-** 


&lt;%@ page import=&quot;java.util.*,java.io.*&quot;%&gt;               

&lt;%

  File f = new File (&quot;E:/PDFfiles/Downloads/&quot; + request.getParameter(&quot;file&quot;) );
  response.setContentType (&quot;application/pdf&quot;);
  response.setHeader (&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot;&quot;+request.getParameter(&quot;file&quot;)+&quot;&quot;&quot;);
  String name = f.getName().substring(f.getName().lastIndexOf(&quot;/&quot;) + 1,f.getName().length());
  InputStream in = new FileInputStream(f);
  ServletOutputStream outs = response.getOutputStream();


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

Ads









Related Tutorials/Questions & Answers:
File Download in jsp
File Download in jsp  file upload code is working can u plz provide me file download
Capture browser file download event - JSP-Servlet
Capture browser file download event  Hi, I appreciate your help and thaanks for that.But my question was when filedownload dialog box comes in browser,how we can come to know that user has clicked on Open,save or cancel button
Advertisements
how to create xls file and give download option using jsp/servlet?
how to create xls file and give download option using jsp/servlet?  Hi, how to create file and give download option to user,so that the user can save the file on defined path
file download
file download  I uploaded a file and saved the path in database. Now i want to download it can u plz provide code
Download CSV File from Database in JSP
Download CSV File from Database in JSP  ... to download CSV  file from database in JSP.  In this example, we have... : Download the CSV File : Download the application
JSP File
JSP File  Hi, What is JSP File? How to create JSP file? Thanks   Hi, JSP file is simple text file with .jsp extenstion. You can run JSP file on tomcat server. Read more at JSP tutorials section. Thanks
download or save jsp page
download or save jsp page  hii.., this is my jsp page .. This page is theater ticket. i want to download this page. so i need to save this page or download.. that depend on user wish how to make this page save or download <
download or save jsp page
download or save jsp page  hii.., this is my jsp page .. This page is theater ticket. i want to download this page. so i need to save this page or download.. that depend on user wish how to make this page save or download <
csv file download
csv file download  Hello Every one, when user clicks download button I want to retrieve data from mysql database table in csv format using java.if... the following link: JSP Retrieve data from MySQL Database
jsp-file
jsp-file  i want to upload a file by browse button and the file should be save in ms access database.....how i can implement trough jsp plz help me sir
Download PDF file
Download PDF file  How to download PDF file with JSF
File Download Security
File Download Security   Hello, I trying to create an application in PHP to prevent or provide file download security. So, I need the help of senior... files downloading. Please read this article about File Download Security
Download LINK - JSP-Servlet
Download LINK  sorry i am the new1 to java. I am learning and doing the project, can you please send me the complete code. So that i can understand easily. i want the code like by Clicking DOWNLOAD button the SAVE as dialog box
java/jsp code to download a video
java/jsp code to download a video  how can i download a video using jsp/servlet
how to download a file from a folder??
how to download a file from a folder??  i can upload any kind of files to a folder, and only the path of the file is saved into the database, now how a client can download the file from my folder. pls provide me the jsp code
Upload and Download Large files in jsp
Upload and Download Large files in jsp  I am not able to download large files (>200mb) from any server. I need a code to download and upload large files (atleast 4 gb) to a server using jsp page
upload and download a file - Java Beginners
upload and download a file  how to upload a file into project folder in eclipse and how to download the same using jsp   Hi Friend, Try the following code: 1)page.jsp: Display file upload form to the user
written in jsp file
written in jsp file  how to create and write in a jsp file through simple core java code
download mp3 file on server in iphone
download mp3 file on server in iphone  I want to download mp3 file on the server in iphone......plz help me give me the demo code
parsing xml file in jsp
parsing xml file in jsp  example that pars XML file in JSP
jsp upload file to server
jsp upload file to server  How to create and upload file to server in JSP?   Find the given example that explains how to upload single and multiple file on server using JSP
Uploading and download pdf or .txt file.
Uploading and download pdf or .txt file.  I want admin user to upload pdf file into database and the users can download those pdf format from database
How to upload and download file in hadoop?
How to upload and download file in hadoop?  Hi, I am trying to learn to upload the file on the Hadoop HDFS and then download the same file for learning the process. How to upload and download file in hadoop? What
how to upload and download images using buttons in jsp?
how to upload and download images using buttons in jsp?  how to upload and download images using buttons in jsp
include a jsp file
include a jsp file   Sir, I have 2 jsp pages. I want to include second jsp in the first jsp page when a button in the first jsp is clicked. how can I do this. I tried in sevaral ways . I am using jsp and javascript... anyone
Creating dynamic jsp file in java file to include another jsp file
Creating dynamic jsp file in java file to include another jsp file  I am able to create jsp file in java that includes ; table tr td and img tags..... Need to include jsp file in the same content. The below code doesn't help me
JSP Include File
JSP Include File          JSP Include File is used to insert a html file and the text file in the jsp page when the JSP page is compiled. The insert
how to download the uploaded folder files using jsp
how to download the uploaded folder files using jsp  how to download the uploaded folder files using jsp
pdf file in jsp
pdf file in jsp  i have a pdf, which has to be displayed in a jsp page along with some html tags, the pdf which is to be displayed shold be readonly and wont allow the uer to save too
External file reading in jsp
External file reading in jsp  i have written a jsp page(ReadExt.jsp) in my application which reads a text file content from a system... used java code in jsp page ... and tel me how can i make it work in other system
JSP Excel report download for .xlsx format
JSP Excel report download for .xlsx format  <%@ page contentType...; The above code works fine because i am trying to save/open excel file in .xls format but my requirement is to download excel with format .xlsx (m.S 2007),so i changed

Ads