java下载网络文件并重命名

文件下载,通过<a href="url">也是可以的,但是这样直接下载,一般文件名就是服务器端的没有任何意义的文件名。

今天自己用到了另外一种,先说需求:1.文件服务器与系统没有在同一服务器,所以需要使用网络地址来进行下载;

2.上传时为了避免文件重名,使用uuid来生成了文件名,真实的文件名存与数据库中;

3.所有的文件都只能下载,不可直接在浏览器上打开。

根据代码来分析:这里使用的springmvc

[java]  view plain  copy
  1. @RequestMapping("/download")  
  2.     public String downloadAmachment(String downloadUrl, String realFileName, HttpServletRequest request,  
  3.             HttpServletResponse response) {  
  4.         response.setContentType("text/html;charset=UTF-8");  
  5.         try {  
  6.             request.setCharacterEncoding("UTF-8");  
  7.   
  8.             BufferedInputStream bis = null;  
  9.             BufferedOutputStream bos = null;  
  10.             //此处使用的配置文件里面取出的文件服务器地址,拼凑成完整的文件服务器上的文件路径  
  11.                         //写demo时,可以直接写成http://xxx/xx/xx.txt.这种形式  
  12.                         String downLoadPath = ConfigHelper.getString("img.server.url") + downloadUrl;  
  13.   
  14.             response.setContentType("application/octet-stream");  
  15.             response.reset();//清除response中的缓存  
  16.             //根据网络文件地址创建URL  
  17.                         URL url = new URL(downLoadPath);  
  18.                         //获取此路径的连接  
  19.                         URLConnection conn = url.openConnection();  
  20.               
  21.                         Long fileLength = conn.getContentLengthLong();//获取文件大小  
  22.             //设置reponse响应头,真实文件名重命名,就是在这里设置,设置编码  
  23.                         response.setHeader("Content-disposition",  
  24.                     "attachment; filename=" + new String(realFileName.getBytes("utf-8"), "ISO8859-1"));  
  25.             response.setHeader("Content-Length", String.valueOf(fileLength));  
  26.   
  27.             bis = new BufferedInputStream(conn.getInputStream());//构造读取流  
  28.             bos = new BufferedOutputStream(response.getOutputStream());//构造输出流  
  29.             byte[] buff = new byte[1024];  
  30.             int bytesRead;  
  31.             //每次读取缓存大小的流,写到输出流  
  32.                         while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
  33.                 bos.write(buff, 0, bytesRead);  
  34.             }  
  35.             response.flushBuffer();//将所有的读取的流返回给客户端  
  36.             bis.close();  
  37.             bos.close();  
  38.   
  39.         } catch (IOException e) {  
  40.             LOG.error(e.getMessage(), e);  
  41.             return ErrorPages._500;  
  42.         }  
  43.         return null;  
  44.     }  


前台页面可以用一个<a href="/download?downloadUrl=xxxxx&realFileName=yyyy">这里的xxxxx为文件的网络地址,yyy为文件的真实具有意义的文件名。

这种做法,主要是针对不同服务器上,不能直接通过磁盘盘符例如:D:/xx/xx.txt这种形式来构建File来进行下载。同时,生产系统,文件服务器万一更改了,到时候还需要直接修改代码,维护性不高;同时解决文件下载下来,得到的是有具体意义的文件名。



扫描二维码关注公众号,回复: 1890064 查看本文章


java 技术交流群:317628801




猜你喜欢

转载自blog.csdn.net/qq_33545491/article/details/80491481