Java通过URL下载网络文件,获取文件流并修改文件名
2017-11-08 18:36
906 查看
下载网络文件,地址是 http://sk.sit.fosuntech.cn/group1/M00/00/72/CqYKHVn69wyAMl6YAAVf953sp4Y075.pdf
前端调用下载链接
后台用的spring,接受请求参数
前端调用下载链接
function downloadAttachement(imageId,fileName){ var url = _HTTP_IP_PORT +'repayment/downloadAttachment.do?imageId='+ imageId +'&fileName='+fileName; var form = $("<form></form>").attr("action", url).attr("method", "post"); form.appendTo('body').submit().remove(); }
后台用的spring,接受请求参数
@RequestMapping(value = "/repayment/downloadAttachment", method = RequestMethod.POST) @ResponseBody public String downloadAttachment(String imageId,String fileName,HttpServletRequest request,HttpServletResponse response) { if (StringUtils.isEmpty(imageId)) { throw new CommonException("影像ID不能为空!"); } if (StringUtils.isEmpty(fileName)) { throw new CommonException("文件名不能为空!"); } //这里获取的下载链接 http://sk.sit.fosuntech.cn/group1/M00/00/72/CqYKHVn69wyAMl6YAAVf953sp4Y075.pdf String downLoadPath = templateService.getDownloanUrl(imageId); BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //如果不存在乱码的情况可以忽略,由于请求参数文件名为中文,到后台会乱码,考虑操作系统和客户端浏览器默认编码 //判断服务器操作系统,本地开发使用windows String os = System.getProperty("os.name"); if(os.toLowerCase().indexOf("windows") != -1){ fileName = new String(fileName.getBytes("GBK"), "ISO-8859-1"); }else{ //判断浏览器 String userAgent = request.getHeader("User-Agent").toLowerCase(); if(userAgent.indexOf("msie") > 0){ fileName = URLEncoder.encode(fileName, "ISO-8859-1"); } } //响应二进制流 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=" + fileName); 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();//将所有的读取的流返回给客户端 } catch (IOException e) { logger.error("文件下载失败!", e); throw new CommonException("文件下载失败!"); }finally{ try{ if(null != bis){ bis.close(); } if(null != bos){ bos.close(); } }catch(IOException e){ logger.error("文件下载失败!", e); throw new CommonException("文件下载失败!"); } } return null; }
相关文章推荐
- Java io字符写入 下载网络文件 获取url文件
- Android里根据下载文件的url获取文件名
- Java通过Ntp服务器获取网络时间修改本地服务器时间
- 通过JSON文件获取图片URL,下载并显示在布局中
- android之通过URL来获取网络资源并下载资源简单实例
- java 通过 URL 类和 URLConnection类 以及输入流实现文件下载功能
- java中通过文件路径获取文件名
- java 从网络Url中下载文件
- java 两个通过java代码操作FTP的类,上传下载删除,获取文件
- Java 通过 JNA (Java Native Access) 获取、设置 Windows 操作系统的文件创建时间、文件修改时间、文件访问时间
- JAVA 实现通过URL下载文件到本地库
- ASP.NET获取远程网页下载到本地文件,通过URL生成静态文件的做法
- java 从网络Url中下载文件
- java 从网络Url中下载文件
- Java学习笔记之网络编程基础-通过URL获取网络图片
- java 获取网络文件获取流下载,支持IE火狐 直接下载而不直接打开
- JAVA通过访问页面中的URL实现Http文件下载到本地
- Java学习笔记之网络编程基础-通过URL获取HTML页面
- Java 获取网络重定向文件的真实URL
- java 两个通过java代码操作FTP的类,上传下载删除,获取文件