文件下载

下载
File file = new File(filePath);
if(file.exists()){
try {
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
byte[] buffer = new byte[1024];
fileName = java.net.URLEncoder.encode(fileName, "UTF-8");// 处理中文文件名的问题
fileName = new String(fileName.getBytes("UTF-8"), "GBK");// 处理中文文件名的问题
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/x-msdownload");// 不同类型的文件对应不同的MIME类型
response.setHeader("Content-Disposition",
"attachment; filename=" + fileName);
OutputStream os = response.getOutputStream();
while (bis.read(buffer) > 0) {
os.write(buffer);
}
bis.close();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}

你可能感兴趣的:(.net,OS)