ENDRPrint_12CRelease/.svn/pristine/95/95774f0144a695ea8912bd7b25a6ee7cbc3c2167.svn-base

59 lines
1.8 KiB
Plaintext
Raw Normal View History

2024-08-14 10:58:03 +07:00
package th.co.muangthai.endrprint;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
/**
* Created by IntelliJ IDEA.
* User: Zizu
* Date: 2/27/14
* Time: 11:40 AM
* To change this template use File | Settings | File Templates.
*/
public class DownloadServlet extends HttpServlet {
protected void doGet( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String filePath = request.getParameter("file");
String type = request.getParameter("type");
String fileType = "";
if ("excel".equals(type.toLowerCase()))
{
fileType = "application/vnd.ms-excel";
}
// Find this file id in database to get file name, and file type
// You must tell the browser the file type you are going to send
// for example application/pdf, text/plain, text/html, image/jpg
response.setContentType(fileType);
File myFile = new File(filePath);
// Make sure to show the download dialog
response.setHeader("Content-disposition","attachment; filename=" + myFile.getName());
// Assume file name is retrieved from database
// For example D:\\file\\test.pdf
// This should send the file to browser
OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(myFile);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}