`
tzylwl
  • 浏览: 90649 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

在服务器上下载文件

OS 
阅读更多
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();
}
}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics