下载图片
Posted ki1616
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了下载图片相关的知识,希望对你有一定的参考价值。
一、下载图片到默认地址
①、请求后台
var curPath = window.document.location.href;
var pathName = window.document.location.pathname;
var pos = curPath.indexOf(pathName);
var localhostPaht = curPath.substring(0,pos);
var projectName = pathName.substring(0,pathName.substr(1).indexOf(\'/\')+1);
var baseuri = localhostPaht + projectName;
function onloadurl(url){ var qrcode = baseuri + "/xxxxx/onloadurl?qrcodeUrl=" + url; var alink = document.createElement("a"); alink.href = qrcode; alink.download = new Date().getTime() + ".jpg"; alink.click(); }
②后台处理
/** * 下载图片信息 * @param request * @param response * @param id * @return * @throws IOException * @throws MalformedURLException */ @RequestMapping("/onloadurl") public void onloadurl(HttpServletRequest req, HttpServletResponse response, String qrcodeUrl) throws MalformedURLException, IOException { HttpURLConnection connection = (HttpURLConnection) new URL(qrcodeUrl).openConnection(); connection.setReadTimeout(5000); connection.setConnectTimeout(5000); connection.setRequestMethod("GET"); if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream inputStream = connection.getInputStream(); if (inputStream != null) { BufferedImage bufferedImage = ImageIO.read(inputStream); ImageIO.write(bufferedImage, "jpg", response.getOutputStream()); response.getOutputStream().close(); inputStream.close(); } } }
以上是关于下载图片的主要内容,如果未能解决你的问题,请参考以下文章
Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题
Swift新async/await并发中利用Task防止指定代码片段执行的数据竞争(Data Race)问题
Python练习册 第 0013 题: 用 Python 写一个爬图片的程序,爬 这个链接里的日本妹子图片 :-),(http://tieba.baidu.com/p/2166231880)(代码片段