文件下载,通过<a href="url">也是可以的,但是这样直接下载,一般文件名就是服务器端的没有任何意义的文件名。
今天自己用到了另外一种,先说需求:1.文件服务器与系统没有在同一服务器,所以需要使用网络地址来进行下载;
2.上传时为了避免文件重名,使用uuid来生成了文件名,真实的文件名存与数据库中;
3.所有的文件都只能下载,不可直接在浏览器上打开。
根据代码来分析:这里使用的springmvc
@RequestMapping("/download")
public String downloadAmachment(String downloadUrl, String realFileName, HttpServletRequest request,
HttpServletResponse response) {
response.setContentType("text/html;charset=UTF-8");
try {
request.setCharacterEncoding("UTF-8");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
//此处使用的配置文件里面取出的文件服务器地址,拼凑成完整的文件服务器上的文件路径
//写demo时,可以直接写成http://xxx/xx/xx.txt.这种形式
String downLoadPath = ConfigHelper.getString("img.server.url") + downloadUrl;
response.setContentType("application/octet-stream");
response.reset();//清除response中的缓存
//根据网络文件地址创建URL
URL url = new URL(downLoadPath);
//获取此路径的连接
URLConnection conn = url.openConnection();
Long fileLength = conn.getContentLengthLong();//获取文件大小
//设置reponse响应头,真实文件名重命名,就是在这里设置,设置编码
response.setHeader("Content-disposition",
"attachment; filename=" + new String(realFileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(conn.getInputStream());//构造读取流
bos = new BufferedOutputStream(response.getOutputStream());//构造输出流
byte[] buff = new byte[1024];
int bytesRead;
//每次读取缓存大小的流,写到输出流
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
response.flushBuffer();//将所有的读取的流返回给客户端
bis.close();
bos.close();
} catch (IOException e) {
LOG.error(e.getMessage(), e);
return ErrorPages._500;
}
return null;
}
前台页面可以用一个<a href="/download?downloadUrl=xxxxx&realFileName=yyyy">这里的xxxxx为文件的网络地址,yyy为文件的真实具有意义的文件名。
这种做法,主要是针对不同服务器上,不能直接通过磁盘盘符例如:D:/xx/xx.txt这种形式来构建File来进行下载。同时,生产系统,文件服务器万一更改了,到时候还需要直接修改代码,维护性不高;同时解决文件下载下来,得到的是有具体意义的文件名。