通过URL下载两种类型图片
来源:互联网 发布:网络诈骗追回钱的几率 编辑:程序博客网 时间:2024/04/30 10:01
@RequestMapping(produces="application/html;charset=UTF-8",value="downMociImg") public void downLoadImages(String imagePath,String imageName,Integer picType){ List<String> suffixList = Arrays.asList(Constants.FILETYPE_DOWNLOAD_IMG); String suffix = imagePath.substring(imagePath.lastIndexOf(".") + 1); String pngsuffix = ".png"; String svgsuffix = ".svg"; if(imagePath!=""&&!imagePath.equals(null)){ BufferedInputStream in = null; BufferedOutputStream out = null; try { URL url = new URL(imagePath); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5 * 1000); conn.setRequestMethod("GET"); conn.setRequestProperty( "Accept", "image/gif, image/jpeg, image/pjpeg, image/pjpeg, " + "application/x-shockwave-flash, application/xaml+xml, " + "application/vnd.ms-xpsdocument, application/x-ms-xbap, " + "application/x-ms-application, application/vnd.ms-excel, " + "application/vnd.ms-powerpoint, application/msword, */*"); conn.setRequestProperty("Accept-Language", "zh-CN"); conn.setRequestProperty("Charset", "UTF-8"); InputStream inStream = conn.getInputStream(); if (suffixList.contains(suffix)) {// 文件格式不对 if(picType==1){ imageName += pngsuffix; response.setContentType("application/x-png"); }else{ imageName +=svgsuffix; response.setContentType("text/xml"); } } response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", "attachment; filename="+new String(imageName.getBytes("gbk"),"iso-8859-1")); in = new BufferedInputStream(inStream); out = new BufferedOutputStream(response.getOutputStream()); byte[] data = new byte[1024]; int len = 0; while (-1 != (len=in.read(data, 0, data.length))) { out.write(data, 0, len); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ try { if (in != null) { in.close(); } if (out != null) { out.flush(); out.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
阅读全文
0 0
- 通过URL下载两种类型图片
- 通过图片URL下载图片到本地
- IOS通过URL下载保存图片
- 通过URL下载图片并压缩
- 通过网络Url下载图片并下载到本地相册
- java WEB项目通过url下载图片到本地
- php最简单的方法通过url下载图片。
- 通过url下载图片 并保存到服务器
- 通过URL实现下载
- 通过url下载文件
- 通过url下载文件
- 通过URL下载文件
- 通过URL下载文件
- 通过URL读取图片
- 通过URL获取图片
- 下载url图片到沙盒中
- java 下载URL图片
- URL,下载网页图片。
- MYSQL存储过程遍历结果集游标 从一张表插入数据到另一张表
- jQuery常用事件和方法总结
- 记录一些意外:ora-09817
- ionic http 数据通信
- 轻松理解skip-gram模型
- 通过URL下载两种类型图片
- python中文分词jieba总结
- php文件上传 中文名称乱码
- 什么是Servlet?
- Flask和@app.route
- ubuntu network ip setting
- 获取多台主机命令执行结果
- <c:forEach>终止循环
- Web安全——前端JS表单验证过滤