JSP 下载文件~~~防止多次调用抛异常
2013-02-20 08:41
274 查看
<%
String filenamedownload =(String) request.getAttribute("fileName");
String filenamedisplay =(String) request.getAttribute("fileDisplayName");
response.reset();
response.setContentType("application/bin"); //告诉浏览器,我服务器发送的data属于何种MIME类型
filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ filenamedisplay);
OutputStream output = null;
FileInputStream fis = null;
try {
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);
byte[] b = new byte[1024];
int i = 0;
while ((i = fis.read(b)) > 0) {
output.write(b, 0, i);
}
output.flush();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fis != null) {
fis.close();
fis = null;
}
if (output != null) {
output.close();
output = null;
}
out.clear();
out = pageContext.pushBody();
}
%>
String filenamedownload =(String) request.getAttribute("fileName");
String filenamedisplay =(String) request.getAttribute("fileDisplayName");
response.reset();
response.setContentType("application/bin"); //告诉浏览器,我服务器发送的data属于何种MIME类型
filenamedisplay = URLEncoder.encode(filenamedisplay, "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename="
+ filenamedisplay);
OutputStream output = null;
FileInputStream fis = null;
try {
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);
byte[] b = new byte[1024];
int i = 0;
while ((i = fis.read(b)) > 0) {
output.write(b, 0, i);
}
output.flush();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
if (fis != null) {
fis.close();
fis = null;
}
if (output != null) {
output.close();
output = null;
}
out.clear();
out = pageContext.pushBody();
}
%>
相关文章推荐
- Servlet.service() for servlet jsp threw exception java.lang.Il在jsp中使用jspsmartupload组件下载文件时将抛出异常的解决方案
- 防止使用Struts2下载时取消出现异常,并实现支持下载完成后删除临时文件
- 在jsp文件中调用自定义控件(如果不存在,自动下载CAB文件安装注册)
- jsp内存数据下载到本地--弹出的文件保存框时 两次调用action中下载方法问题
- MFC CInternetSession OpenUrl 下载文件 防止异常
- 完善flex调用jsp打开和下载文件的例子
- 如何处理:下载文件的按钮在事件未完成之前防止多次点击,事件完成之后又能继续点击提交
- 在jsp下载文件如jpeg、word等使用getOutputStream()时出现异常
- post请求时参数过大导致请求异常(导出excel时post请求下载成了jsp文件或请求路径)
- Android2.2以上版本下载网络文件getContentLength()大小异常
- Java / JSP 文件上传与下载
- Jsp+Servlet实现文件上传下载 删除上传文件(三)
- 简单介绍用JSP来实现文件下载功能的几种方式
- 项目开发技巧(四):使用JspSmartupload实现文件上传下载(二):jspSmartUpload上传下载全攻略(ZZ)
- jsp之文件的上传和下载
- ExtJS中使用jspSmartUpload实现文件下载
- perl 调用 shell 文件 防止出问题,最好写绝对路径
- Android下载文件时对MediaScanner的调用
- 防止flash文件被下载
- 防止事件导致的oncreate的多次调用 (转)