直接上代码吧
OutputStream out = null;
try {
response.reset();//设置页面不缓存
response.setContentType(“application/octet-stream; charset=utf-8”);
response.setHeader(“Content-Disposition”, “attachment; filename=abc.txt”);
out = response.getOutputStream();
out.write(FileUtils.readFileToByteArray(new File(“D:\XMP\abc.txt”)));
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上代码只能判断出读取的路径,那么 response 写入的路径是在哪里? 能否能自定义写入路径?
OutputStream out = null;
try {
response.reset();//设置页面不缓存
response.setContentType(“application/octet-stream; charset=utf-8”);
response.setHeader(“Content-Disposition”, “attachment; filename=abc.txt”);
out = response.getOutputStream();
out.write(FileUtils.readFileToByteArray(new File(“D:\XMP\abc.txt”)));
out.flush();
} catch (Exception e) {
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
以上代码只能判断出读取的路径,那么 response 写入的路径是在哪里? 能否能自定义写入路径?
解决方案
20
页面点击下载的时候,会让你选择的,这是浏览器做了的,不用操心。