tzylwl 发表于 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;
   
   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();
}
}
页: [1]
查看完整版本: 在服务器上下载文件