download

download

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
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
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
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
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
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
download oracle jdk 11 - How to download
download oracle jdk 11 - How to download  Hi, Java 11 is released I want to know where to download it for my computer. What is the url to download.../javase/downloads/index.html For windows you can download installer file
php download free
php download free  From where i download php free
php download file script
php download file script  PHP script to download file in IE
Download PDF file
Download PDF file  How to download PDF file with JSF
download code from database
download code from database  how to download files from database
how to download oracle 9i
how to download oracle 9i  how to download oracle 9i
Download file - JSP-Servlet
Servlet download file from server  I am looking for a Servlet download file example
how to download file
how to download file  How do I let people download a file from my page
php download file code
php download file code  PHP code to download the files from the remote server
File Download in jsp
File Download in jsp  file upload code is working can u plz provide me file download
Free download java
Free download java  Hi, Where can I download java for absolutely free and quickly? Thanks
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,  ... download JAVA from http://www.java.com/en/download/index.jsp and click on Free
ModuleNotFoundError: No module named 'download'
ModuleNotFoundError: No module named 'download'  Hi, My Python... 'download' How to remove the ModuleNotFoundError: No module named 'download... to install padas library. You can install download python with following
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
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
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
xcode 6.1.1 download
xcode 6.1.1 download  Hi, I want to download the Xcode from my windows computer. What is the direct url to download Xcode 6.1.1? Thanks   Hi, Download Xcode 6.1.1. You will still need the Apple user name
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
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
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 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
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
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
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
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 jar - Framework
download jar  hi somebody could ya tell me from where i can get portal-ejb.jar
php download file browser
php download file browser  limiting downloading file system via browser only through my web application
Oracle 9i free download
Oracle 9i free download  where to download oracle 9i for database connectivity in j2ee
mysql connector 6.0.2 download
mysql connector 6.0.2 download  Hi, Where to download JDBC driver mysql connector 6.0.1? I am using maven in my project. Thanks
mysql connector 6.0.2 download
mysql connector 6.0.2 download  Hi, Where to download JDBC driver mysql connector 6.0.1? I am using maven in my project. Thanks
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
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
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
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
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

Ads