Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try to use that code</p> <pre><code>package com.hainasoft.web; 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) &amp;&amp; ((length = in.read(byteBuffer)) != -1)) { outStream.write(byteBuffer,0,length); } in.close(); outStream.close(); } } </code></pre> <p>it is important to set the myme type</p> <pre><code>mimetype = "application/octet-stream"; </code></pre> <p>and with this you set a download dialog to the user</p> <pre><code>response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); </code></pre> <hr> <p>I have retrieved all that information from <a href="http://www.java-forums.org/blogs/servlet/668-how-write-servlet-sends-file-user-download.html" rel="nofollow">http://www.java-forums.org/blogs/servlet/668-how-write-servlet-sends-file-user-download.html</a></p> <p>so take a look in it for more accurate information.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload