六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 65|回复: 0

在服务器上下载文件

[复制链接]

升级  64%

6

主题

6

主题

6

主题

童生

Rank: 1

积分
32
 楼主| 发表于 2013-1-15 02:17:58 | 显示全部楼层 |阅读模式
String filePath=request.getParameter("filePath");         //全路径
String fileName=request.getParameter("fileName");         //文件名

try {
    response.setContentType("APPLICATION/OCTET-STREAM");
     File file = new File(filePath);
     if (file.exists()) {
     response.setHeader("Content-Disposition", "attachment; filename=\""
     + new String((fileName).getBytes("gb2312"), "ISO8859-1") + "\"");
     } else {
     }
     
     FileInputStream fis = null;
     
     fis = new FileInputStream(filePath);
     BufferedInputStream bis = new BufferedInputStream(fis);
     OutputStream os = response.getOutputStream();
     BufferedOutputStream bos = new BufferedOutputStream(os);
     byte[] cbuf = new byte[1024];
     
     while (bis.read(cbuf) != -1) {
     bos.write(cbuf);
     }
     bos.flush();
     try {
     bos.close();
     } catch (IOException e) {
     e.printStackTrace();
     }
     try {
     bis.close();
     } catch (IOException e) {
     e.printStackTrace();
     }
   
   
} catch (Exception e) {
e.toString();
}
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表