Flask接口签名sign原理与实例代码浅析
189
2023-07-12
JavaWeb文件下载功能实例代码
在工作中遇到的一个下载文件的功能,自己将其抽取出来,代码简单,希望能帮到大家,好了,话不多说,上代码!
public void downloadFile(File file, String downName, HttpServletRequest request, HttpServletResponse response) {
OutputStream out = null;
FileInputStream fin = null;
BufferedInputStream bin = null;
try {
if (file.exists()) {
String finalFileName = null;
String agent = request.getHeader("User-Agent");
boolean isMSIE = (agent != null && agent.indexOf("MSIE") != -1);
if (isMSIE) {
finalFileName = URLEncoder.encode(downName, "UTF8");
} else {
finalFileName = new String(downName.getBytes("UTF-8"), "ISO-8859-1");
}
response.setContentType("application/x-msdownload");
response.setHeader("Content-Disposition", "attachment; filename=".concat(finalFileName));
out = response.getOutputStream();
fin = new FileInputStream(file);
bin = new BufferedInputStream(fin);
for (int data = bin.read(); data > http://-1; data = bin.read()) {
out.write(data);
}
} else {
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (bin != null)
bin.close();
if (fin != null)
fin.close();
if (out != null)
out.close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~