protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
response.setContentType("application/octet-stream;charset=UTF-8");
response.setHeader("Connection", "close");
// 由id得到模板信息
String docPath = "/WEB-INF/classes/com/byd/cmms/web/servlet/MaterialMaintain.xls";
String realPath = getServletContext().getRealPath(docPath);
String filename = "MaterialMaintain.xls";
FileInputStream fis = null;
try { // 写出文件
response.setHeader("Content-Disposition", "attachment;filename="
+ new String(filename.getBytes("GBK"), "iso8859-1"));//目前windows的编码是GBK
ServletOutputStream out = response.getOutputStream();
fis = new FileInputStream(realPath);
byte[] b = new byte[1024];
int i = 0;
while ((i = fis.read(b)) != -1) {
out.write(b, 0, i);
}
out.flush();
out.close();
} catch (Exception e) {
System.out.println(e);
} finally {
fis.close();
}
}
private function downloadTemplate():void {
var downloadFileWin:DownloadFileWin = new DownloadFileWin();
PopUpManager.addPopUp(downloadFileWin,Application.application.mainVS,true);
downloadFileWin.load("servlet/DownloadTempDoc","MaterialMaintain.xls");
PopUpManager.centerPopUp(downloadFileWin);
// var url:String = "servlet/DownloadTempDoc";
// var request:URLRequest = new URLRequest(url);
// navigateToURL(request);
}
<servlet-mapping>
<servlet-name>DownloadTempDocServlet</servlet-name>
<url-pattern>/servlet/DownloadTempDoc</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>DownloadTempDocServlet</servlet-name>
<servlet-class>com.byd.cmms.web.servlet.DownloadTempDocServlet</servlet-class>
</servlet>
本文介绍了一个使用Java Servlet实现的简单文件下载功能。通过设置HTTP响应头,使浏览器能够下载指定路径下的文件。示例中具体展示了如何读取服务器上的Excel文件并发送给客户端。
794

被折叠的 条评论
为什么被折叠?



