首页 > 其他 > 详细

下载图片

时间:2021-06-11 01:03:19      阅读:15      评论:0      收藏:0      [点我收藏+]

一、下载图片到默认地址

①、请求后台


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();
            }
        }
        
    }

 

下载图片

原文:https://www.cnblogs.com/ki16/p/14872765.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!