在服务器上下载文件

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();
}
}

    推荐阅读