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



Home Answers Viewqa Servlet-Interview-Questions download code using servlet
Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


rayappa
download code using servlet
1 Answer(s)      6 years and 4 months ago
Posted in : Servlet Interview Questions



Advertisement
View Answers

August 9, 2012 at 11:42 AM


import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException;

import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;

public class DownloadServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { static final long serialVersionUID = 1L; private static final int BUFSIZE = 4096; private String filePath;

public void init() {
    // the file data.xls is under web application folder
    filePath = getServletContext().getRealPath("") + File.separator + "data.xls";
}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    File file = new File(filePath);
    int length   = 0;
    ServletOutputStream outStream = response.getOutputStream();
    ServletContext context  = getServletConfig().getServletContext();
    String mimetype = context.getMimeType(filePath);

    // sets response content type
    if (mimetype == null) {
        mimetype = "application/octet-stream";
    }
    response.setContentType(mimetype);
    response.setContentLength((int)file.length());
    String fileName = (new File(filePath)).getName();

    // sets HTTP header
    response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");

    byte[] byteBuffer = new byte[BUFSIZE];
    DataInputStream in = new DataInputStream(new FileInputStream(file));

    // reads the file's bytes and writes them to the response stream
    while ((in != null) && ((length = in.read(byteBuffer)) != -1))
    {
        outStream.write(byteBuffer,0,length);
    }

    in.close();
    outStream.close();
}

}



Related Tutorials/Questions & Answers:
download code using servlet - Servlet Interview Questions
download code using servlet  How to download a file from web to our system using Servlet
download xml file from website using java code
download xml file from website using java code  how to download xml file from website using java code
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
upload and download files from ftp server using servlet - Ajax
for upload and download files from ftp server using servlet and how to use servlet for these applications.I have send my code to you previous time...upload and download files from ftp server using servlet  Hi,Sir
java/jsp code to download a video
java/jsp code to download a video  how can i download a video using jsp/servlet
download code from database
download code from database  how to download files from database
Download file - JSP-Servlet
Servlet download file from server  I am looking for a Servlet download file example
php download file code
php download file code  PHP code to download the files from the remote server
download - JSP-Servlet
download  here is the code in servlet for download a file. while... help me out its urgent!! code servlet: /* * To change this template...(); System.out.println("inside download servlet"); BufferedInputStream
code for password strength using jsp-servlet
code for password strength using jsp-servlet  hi.............. plz help me to give code for password strength using jsp-servlet for implementation in my project as soon as possible because i want to show this functionality in my
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 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
HTML code to servlet to database using jdbc
HTML code to servlet to database using jdbc  pls send me the code related to the title   Hi Friend, Try the following code: 1)form.html: <html> <form method="post" action="http://localhost:8080/examples/Data
How to download files from server to local disk using ZIP format in JSP page - JSP-Servlet
How to download files from server to local disk using ZIP format in JSP page  hi i have an application to upload and download multiple files. As i can upload multiple files .now i have to download all uploaded files on local
download image using url in jsp
download image using url in jsp  how to download image using url in jsp
How to download JDK source code?
How to download JDK source code? This articles explains you how you can download the source code of JDK. You will learn how to download JDK source code... to download JDK source code? JDK installer is packaged with the binary of the JDK
upload and download files - JSP-Servlet
upload and download files  HI!! how can I upload (more than 1 file) and download the files using jsp. Is any lib folders to be pasted? kindly... and download files in JSP visit to : http://www.roseindia.net/jsp/file_upload
JSP file download - JSP-Servlet
of the word file in database. when i want to download that file i am eliminating that file path using path.subString(16).. upto docs/. when i am clicking on the link it is asking open or save. Upto this is fine..... I want to download
org.jfree package download - JSP-Servlet
org.jfree package download  I saw a code in roseindia but that code requires a package "org.jfree.chart.*" and "org.jfree.ui.*" Please give me the packages .  Hi friend, Plz download the jar files
Upload and Download in JSP - JSP-Servlet
Upload and Download in JSP  Respected Sir/Madam, I am... files and similarly when the user clicks the download option, he must be able to do so.. I am Using a table named file_tbl (MS-Access) Also if opossible
Upload and download file - JSP-Servlet
Upload and download file  What is JSP code to upload and download a document in a web page?  Hi Friend, Try the following code to upload............ Now to download the word document file, try the following code
download java source code for offline referral
download java source code for offline referral   how can i download complete java tutorial from rose india so that i can refer them offline
code to upload and download a file - Java Magazine
code to upload and download a file  Is their any code to upload a file and download a file from databse, My requirement is how can i store two... user send one a.doc file to x ,he has to download his a.doc another user sends
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
java code to upload and download a file - Java Beginners
java code to upload and download a file  Is their any java code to upload a file and download a file from databse, My requirement is how can i..., one user send one a.doc file to x ,he has to download his a.doc another
File Upload And download JSP Urgent - JSP-Servlet
Ragavendran, i am sending code of file download in jsp. Plz implement this code...File Upload And download JSP Urgent  Respected Sir/Madam, I... Download in JSP.. In the Admin window, There must be "Upload" provision where admin
download
download  how to download the file in server using php code: "<form method="post" action=""> enter the folder name<input type="text" name... the wrong foldername"; end1: } ?> </form>`print("code sample
i want code of signing off from a account how its done in jsp and servlet by using either cookies or session
i want code of signing off from a account how its done in jsp and servlet by using either cookies or session   sig
Source Code for Implementing Search Feature in JSP using Java action/Servlet - JSP-Servlet
Source Code for Implementing Search Feature in JSP using Java action/Servlet  How do I write the source code to implement search feature in JSP using Java action/servlet? My query is: SELECT @rownum:=@rownum+1 'rownum', X
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
 

 

 

DMCA.com