客户端下载文件核心代码
程序员文章站
2024-03-24 23:40:04
...
/**
* 从服务器下载文件到客户端
* @param filezPath文件在服务器段的路径
* @param response
* @param filename文件名
*/
public void download(String filePath, HttpServletResponse response, String filename){
try {
response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(filename,"utf-8"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
InputStream in=new FileInputStream(filePath);
OutputStream out=response.getOutputStream();
byte[] buffer=new byte[1024*8];
int length=0;
while((length=in.read(buffer, 0, 1024*8))!=-1){
out.write(buffer, 0, length);
}
out.flush();
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
需要注意的是,不要在Action层进行转向否则会出错。
上一篇: PyTorch中的scatter()与scatter_()函数
下一篇: 面试复习之Js实现继承