public synchronized String downTsfj() throws Exception {
HttpServletResponse resp = ServletActionContext.getResponse();
// 输出响应正文的输出流
OutputStream out;
// 读取本地文件的输入流
InputStream in;
// 获得本地输入流
bean=tsglService.queryEditTsxx(bean.getTsxx().getTsid());
File file = new File(bean.getTsxx().getTsfj());
String filename = bean.getTsxx().getTsfj().substring(
bean.getTsxx().getTsfj().lastIndexOf("\\") + 1);
in = new FileInputStream(file);
// 设置响应正文的MIME类型
resp.setContentType("Content-Disposition;charset=utf-8");
resp.setHeader("Content-Disposition", "attachment;" + " filename="
+ new String(filename.getBytes(), "ISO-8859-1"));
// 把本地文件发送给客户端
out = resp.getOutputStream();
int byteRead = 0;
byte[] buffer = new byte[512];
while ((byteRead = in.read(buffer)) != -1) {
out.write(buffer, 0, byteRead);
}
in.close();
out.close();
return null;
}
解决中文文件名的文件下载代码~
最新推荐文章于 2023-08-29 11:26:52 发布