在工作中遇到的一个下载文件的功能,自己将其抽取出来,代码简单,希望能帮到大家,好了,话不多说,上代码!
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 > -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(); } } }
以上就是本文javaweb文件下载的代码,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
unity的错误解决办法:NullReferenceException: Object reference not set to an instance of an object;tiny proje
Hadoop 之 HDFS (HDFS 数据流的 读写 流程)
听说你一读Spring源码就懵逼?我帮你把架子搭好了,你填就行!
首席架构师推荐:金融保险领域数字化转型实践--如何优雅地修改业务中台中分层应用Maven多模块的版本号?(命令导入式)
[JVM学习之路]一、初识JVM,了解其结构、模型及生命周期
网友评论