jsf浏览器文件下载的实现及问题!
2012-04-19 10:49
525 查看
经测试IE,chrome,欧鹏等浏览器都可以正常实现文件下载,但是360浏览器下载文件时,当文件太小估计小于6kb时无法正常下载,但文件比较大时就没问题了。
下载代码:
jsf:<h:commandButton value="下载" action="#{userinfoBean.download}"></h:commandButton>
javaBean:
public String download() {
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request=(HttpServletRequest)ctx.getExternalContext().getRequest();
String filepath="D://tt.txt";
// String filepath=conn.getfilepath(Integer.parseInt(request.getParameter("fileid")));//"C://Program Files//Apache Software Foundation//Tomcat 5.0//webapps//FileUploadDown//upload//007.txt";
if(!new File(filepath).exists()){
System.out.println("文件不存在...........");
return "";
}
try {
String contentType = "application/octet-stream;charset=iso8859-1";
HttpServletResponse response = (HttpServletResponse) ctx
.getExternalContext().getResponse();
response.setContentType(contentType);
StringBuffer contentDisposition = new StringBuffer();
contentDisposition.append("attachment;");
contentDisposition.append("filename=\"");
contentDisposition.append(new File(filepath).getName());
contentDisposition.append("\"");
//logger.debug(System.getProperty("file.encoding"));
response.setHeader("Content-Disposition", new String( contentDisposition.toString().getBytes(System.getProperty("file.encoding")), "iso8859-1"));
//logger.debug(contentDisposition.toString());
OutputStream out = response.getOutputStream();
byte[] bytes = new byte[1024];
InputStream is = new FileInputStream(filepath);
int b = 0;
while ((b = is.read(bytes, 0, 1024)) > 0) {
out.write(bytes, 0, b);
}
is.close();
out.flush();
ctx.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
下载代码:
jsf:<h:commandButton value="下载" action="#{userinfoBean.download}"></h:commandButton>
javaBean:
public String download() {
FacesContext ctx = FacesContext.getCurrentInstance();
HttpServletRequest request=(HttpServletRequest)ctx.getExternalContext().getRequest();
String filepath="D://tt.txt";
// String filepath=conn.getfilepath(Integer.parseInt(request.getParameter("fileid")));//"C://Program Files//Apache Software Foundation//Tomcat 5.0//webapps//FileUploadDown//upload//007.txt";
if(!new File(filepath).exists()){
System.out.println("文件不存在...........");
return "";
}
try {
String contentType = "application/octet-stream;charset=iso8859-1";
HttpServletResponse response = (HttpServletResponse) ctx
.getExternalContext().getResponse();
response.setContentType(contentType);
StringBuffer contentDisposition = new StringBuffer();
contentDisposition.append("attachment;");
contentDisposition.append("filename=\"");
contentDisposition.append(new File(filepath).getName());
contentDisposition.append("\"");
//logger.debug(System.getProperty("file.encoding"));
response.setHeader("Content-Disposition", new String( contentDisposition.toString().getBytes(System.getProperty("file.encoding")), "iso8859-1"));
//logger.debug(contentDisposition.toString());
OutputStream out = response.getOutputStream();
byte[] bytes = new byte[1024];
InputStream is = new FileInputStream(filepath);
int b = 0;
while ((b = is.read(bytes, 0, 1024)) > 0) {
out.write(bytes, 0, b);
}
is.close();
out.flush();
ctx.responseComplete();
} catch (Exception e) {
e.printStackTrace();
}
return "";
}
相关文章推荐
- Android开发实践经验谈:如何让各式手机浏览器可以直接下载apk安装文件且实现自动安装APP
- a 标签实现文件下载(解决打开的问题)
- 实现文件下载与中文文件名乱码问题解决
- ASP.NET实现从服务器下载文件问题处理
- php 实现文件下载,兼容IE、Firefox、Chrome等浏览器
- 解决各大浏览器下载文件乱码以及Firefox下载文件名不全问题
- Java Web实现文件打包下载并解决乱码问题
- 多系统,多种浏览器下:文件上下载的文件名和路径问题
- 浏览器下载文件名称乱码问题
- Struts 2中实现文件下载(修正中文问题)
- jsp实现文件下载与中文文件名乱码问题解决
- Ruby on Rails实现文件的下载以及中文文件名乱码问题
- HTML5拖拽文件到浏览器并实现文件上传下载
- JSF文件下载的不同实现
- 解决各大浏览器下载文件乱码以及Firefox下载文件名不全问题
- 针对前台json乱码和不同浏览器文件下载时文件名乱码问题
- 文件下载时 文件名称乱码问题 包含各种浏览器
- jsf 实现文件下载
- 浏览器通过apache访问php文件提示下载问题,更改项目文件目录
- 实例-用JSF实现文件下载