下载服务器上的文件的jsp
2012-12-14 15:25
260 查看
<%@ page import="java.io.*"%> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <% response.setCharacterEncoding("UTF-8"); request.setCharacterEncoding("UTF-8"); String filepath = new String(request.getAttribute("redirectUrl") .toString().getBytes("ISO-8859-1"), "UTF-8"); System.out.println("============================" + filepath); if (filepath != null) { OutputStream os = null; FileInputStream fis = null; try { String file = filepath; if (!(new File(file)).exists()) { System.out.println("没有文件"); return; } String filename = file .substring(file.lastIndexOf("\\") + 1); System.out.println("文件名为:" + filename); os = response.getOutputStream(); response.setHeader("content-disposition", "attachment;filename=" + new String(filename.getBytes("GBK"), "ISO-8859-1")); response.setContentType("application/octet-stream");//八进制流 与文件类型无关 byte temp[] = new byte[1024]; fis = new FileInputStream(file); int n = 0; while ((n = fis.read(temp)) != -1) { os.write(temp, 0, n); } } catch (Exception e) { out.print("出错了"); } finally { if (os != null) os.close(); if (fis != null) fis.close(); } out.clear(); out = pageContext.pushBody(); } %>
用来下载服务器上的文件,支持中文文件名,路径
来源于百度来的某个文章
稍许更改
相关文章推荐
- jsp实现从服务器下载xls文件到客户端的方法
- JSP实现远程文件下载保存到服务器指定目录中的方法
- JSP实现从不同服务器上下载文件的方法
- 【转载】使用jsp实现文件上传到服务器或者从服务器上下载文件到本地计算机完整说明版
- JSP实现从不同服务器上下载文件的方法
- JSP下载服务器文件的方法
- jsp+servlet实现大型文件下载(tomcat服务器),将自己今天做的和大家一起分享
- 【转载】使用jsp实现文件上传到服务器或者从服务器上下载文件到本地计算机完整说明版
- JSP下载服务器文件
- JSP下载服务器文件的方法
- Jsp 下载远程文件至服务器
- jsp从服务器下载xls文件到客户端
- jsp实现从服务器下载xls文件到客户端的方法
- jsp页面点击下载文件 java方式(服务器上带路径文件 以文件流输出)
- jsp从服务器下载xls文件到客户端
- 用压缩流jsp实现从Tomcat服务器下载多个文件
- JSP实现远程文件下载保存到服务器指定目录中的方法
- jsp下载服务器文件
- JAVA中的FtpClient与FTPClient,并实现jsp页面下载ftp服务器上的文件
- java+jsp代码实现从FTP服务器上传下载文件