在服务器上下载文件
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();
}
}
推荐阅读
- 你到家了吗
- Y房东的后半生14
- 闲杂“细雨”
- 杜月笙的口才
- 赢在人生六项精进二阶Day3复盘
- 祖母走了
- 陇上秋二|陇上秋二 罗敷媚
- 樱花雨
- 眼观耳听美食的日子
- MediaRecorder前后摄像头同时录像