Latest Tutorials| Questions and Answers|Ask Questions?|Site Map




Have Programming Question? Ask it here!
 
 
 


saravanan
download
2 Answer(s)      5 years and 4 months ago
Posted in : JSP-Servlet



Advertisement
View Answers

April 29, 2009 at 10:56 AM


Hi Friend,

As you have explained that you are getting the file but not getting the content then it seems that what code you have written is just creating a file with the same name for download but not writing the content. Therefore you are required to write the content of the specified file to the new file that you have created in the "content-desposition".

Please try the following code :

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package pack;
import java.io.*;

import javax.servlet.ServletOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
*
* @author saravana
*/
public class download extends HttpServlet {

/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String path= request.getParameter("path");
String filename = request.getParameter("file");
//PrintWriter out = response.getWriter();
System.out.println("inside download servlet");
ServletOutputStream myOut= response.getOutputStream();
try {
File f = new File(path+"\\"+filename);


/*if(f.exists())
// System.out.println(f.toString());
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet download</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet download at " + path + "</h1>");
out.println("<h1>Servlet download at " + filename + "</h1>");*/

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

FileInputStream fileIn = new FileInputStream(f);

byte[] outputByte = new byte[4096];
while(fileIn.read(outputByte, 0, 4096) != -1){
myOut.write(outputByte, 0, 4096);
}
}catch (IOException ioe){

throw new ServletException(ioe.getMessage( ));

}

finally {
if (myOut != null)
myOut.close( );
if (buf != null)
buf.close( );
}
}
}

We hope that this will help you.

Thanks
RoseIndia Team


May 5, 2009 at 5:56 PM


h


Related Tutorials/Questions & Answers:
Download
Download JDK  How to Download the latest Version of JDK?   Download OST to PST converter from: http://www.osttopst.info
Download.
Download.  I need a swing program to download a file from the server
Advertisements
download
download  how to download the file in server using php code: "<form method="post" action=""> enter the folder name<input type="text" name="t1" > <input type="submit" value="search" name="s1"> </form> <
download excel
download excel  hi i create an excel file but i don't i know how to give download link to that excel file please give me any code or steps to give download link
Java download
Java download  Hi, Is it possible to download the Java for Linux Centos 6? From where it can be downloaded for Linux Centos 6? Thanks
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 Java
process of downloading and Installing Java  Hi, How easy is the process of downloading and Installing Java? I have windows 7 machines. Can anyone tell me how to download and install Java? Thanks
download - SQL
download  i want download the mysql server  Hi friend, The latest version of MySQL can be downloaded from http://www.mysql.com. Read for more information. http://www.roseindia.net/mysql/mysql5/Installing
FileUpload and Download
FileUpload and Download  Hello Sir/Madam, I have used the below coding for Upload and download file, but it is not stored in database and also it s not download the file with its content... just it download doc with 0 Bytes
FileUpload and Download
FileUpload and Download  Hello Sir/Madam, I have used the below coding for Upload and download file, but it is not stored in database and also it s not download the file with its content... just it download doc with 0 Bytes
FileUpload and Download
FileUpload and Download  Hello Sir/Madam, I have used the below coding for Upload and download file, but it is not stored in database and also it s not download the file with its content... just it download doc with 0 Bytes
FileUpload and Download
FileUpload and Download  Hello Sir/Madam, I have used the below coding for Upload and download file, but it is not stored in database and also it s not download the file with its content... just it download doc with 0 Bytes
FileUpload and Download
FileUpload and Download  Hello Sir/Madam, I have used the below coding for Upload and download file, but it is not stored in database and also it s not download the file with its content... just it download doc with 0 Bytes
FileUpload and Download
FileUpload and Download  Hello sir/madam, I need a simple code for File upload and Download in jsp using sql server,that uploaded file should... be download with its full content...can you please help me... im in urgent i have
how to download oracle 9i
how to download oracle 9i  how to download oracle 9i
download code from database
download code from database  how to download files from database
Download PDF file
Download PDF file  How to download PDF file with JSF
php download file script
php download file script  PHP script to download file in IE
php download free
php download free  From where i download php free
Free download java
Free download java  Hi, Where can I download java for absolutely free and quickly? Thanks
File Download in jsp
File Download in jsp  file upload code is working can u plz provide me file download
php download file code
php download file code  PHP code to download the files from the remote server
how to download file
how to download file  How do I let people download a file from my page
Download file - JSP-Servlet
Servlet download file from server  I am looking for a Servlet download file example
JAVA S/W download
JAVA S/W download  Hi I'm new to JAVA, I want to download JAVA S/W from where I need to download. Please help me with the link. Thank you,   Hi ! u can download JAVA from http://www.java.com/en/download/index.jsp
How to download Java
How to download Java  Hi, My windows 7 computer is ready. Now I want to download and install Java. Can anyone tell me How to download Java and then install on my computer. Thanks
Java 1.6 download
Java 1.6 download  Hi, Where can you download Java 1.6 download? Thanks   Hi, Please see the tutorial Download and Install Java. Thanks
Free Java Download
Free Java Download   Hi, I am beginner in Java and trying to find the url to download Java. Can anyone provide me the url of Free Java Download ? Is Java free? Thanks
Download free java
Download free java  Hi, How to get Java for free? It is possible to download and install java on windows 64bit system? Thanks
How to download and install Tomcat?
How to download and install Tomcat?  Hi, How to download and install Tomcat? Share me the url to learn Tomcat. Thanks   Hi, Please check the tutorial: Guide to download and install Tomcat 6. Thanks
Download Java Script
Download Java Script  Hi, How I can download JavaScript for my browser? Thanks   Hi, You don't have to download JavaScript for browser. JavaScript support is provided in web browser. You have to enable
download pdf files
download pdf files  pls help me,I don't know how to convert .doc,.docx files into pdf files and download that pdf files using servlet or jsp
Download java 1.6
Download java 1.6  Hi, I have to develop a software in Java 1.6. From where I can Download java 1.6? Provide me the url Downloading java 1.6. Thanks
upload and download mp3
upload and download mp3   code of upload and download Mp3 file in mysql database using jsp and plz mention which data type used to store mp3 file in mysql database
URL folder download to local
URL folder download to local  I have a requirement to download all the folders and files inside from a SVN link to my local directory. Can you help how it could be done
Download a file - Java Beginners
Download a file   Hi, I need a java code to download a file from a website and before downloading it should give me an option to browse and select the location to put the file. Thanks in advance
Oracle 9i free download
Oracle 9i free download  where to download oracle 9i for database connectivity in j2ee
php download file browser
php download file browser  limiting downloading file system via browser only through my web application
download jar - Framework
download jar  hi somebody could ya tell me from where i can get portal-ejb.jar
Java FTP Download
Download. Thanks   Hi, The easiest way to connect to FTP server and then download file from server is to use the Apache FTPClient library. View the complete example at FTP Server : Download file. Thanks
Java Free download
Java Free download   Hi, What is the url for downloading Java for free? Is java Free? Can I get the url to download Java without paying anything? Actually I am student and learning Java. I have to download and install Java on my
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
Upload and Download multiple files
Upload and Download multiple files  Hello Sir/Madam, I need a simple code for upload and download multiple files(it may be image,doc and txt)which has to be store in database and also retrieve the file from database
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
Download and Build from Source
Download and Build from Source Shopping cart application developed using Struts 2.2.1 and MySQL can be downloaded from... Of An Application Download Source Code
download - JSP-Servlet
download  here is the code in servlet for download a file. while...; /** * * @author saravana */ public class download extends HttpServlet...(); System.out.println("inside download servlet"); BufferedInputStream
Junit download & installation.
Junit download & installation.  Hello sir I want to know from which site i can download JUnit & How can i install JUnit? If there is any class path setting plz mention it. Plz reply ASAP.   First, download
Download Button - JSP-Servlet
Download Button  HI friends, This is my maiden question.... And i want the Bank customers to DOWNLOAD the mini statement. Can any one help me how to implement the download button so that on clicking that the mini
file download in struts - Struts
file download in struts  Hello... I've designed an application where i need to download an xml file by creating it... so...i used dom for it.. I need to enter a meterid and then according to the meterid i've to generate
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 you know please send code. Thanks in Advance   Please visit
Advertisements
 

 

 

DMCA.com