zoukankan      html  css  js  c++  java
  • Java通过网络图片之地址,下载到服务器

    @RequestMapping("/downloadTableQrcode")
        public String downloadTableQrcode(HttpServletRequest request, HttpServletResponse response, @RequestParam String name, @RequestParam String url) throws Exception {
            try {
                response.setContentType("application/zip");
                String fileName = new String(name.getBytes(), "ISO8859-1"); //正确,不发生乱码
                response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ".jpg");
                javax.servlet.ServletOutputStream out = response.getOutputStream();
                downloadPicture(url, request.getSession().getServletContext().getRealPath("/") + "downloadTableQrcode.jpg");
                File file = null;
                file = new File(request.getSession().getServletContext().getRealPath("/") + "downloadTableQrcode.jpg");
                InputStream inputStream = new FileInputStream(file);
                int b = 0;
                byte[] buffer = new byte[1000000];
                while (b != -1) {
                    b = inputStream.read(buffer);
                    if (b != -1) out.write(buffer, 0, b);
                }
                inputStream.close();
                out.close();
                out.flush();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
                return "https://linliny.com:8443/superllny/downloadTableQrcode.jpg";
        }
    
        //链接url下载图片
        public static void downloadPicture(String urlList, String path) {
            log.info("path==================="+path);
            URL url = null;
            try {
                url = new URL(urlList);
                DataInputStream dataInputStream = new DataInputStream(url.openStream());
    
    
                FileOutputStream fileOutputStream = new FileOutputStream(new File(path));
                ByteArrayOutputStream output = new ByteArrayOutputStream();
    
    
                byte[] buffer = new byte[1024];
                int length;
    
    
                while ((length = dataInputStream.read(buffer)) > 0) {
                    output.write(buffer, 0, length);
                }
                fileOutputStream.write(output.toByteArray());
                dataInputStream.close();
                fileOutputStream.close();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
  • 相关阅读:
    【mysql】八、mysql的学习---查询缓存
    【mysql】七、mysql的学习---应用优化
    【mysql】六、mysql的学习---SQL优化
    【mysql】五、mysql的学习---存储引擎
    【mysql】四、mysql的学习---触发器
    【mysql】三、mysql的学习---存储过程和函数
    【mysql】二、mysql的学习---视图
    server --SSD中,SATA,m2,PCIE和NVME各代表了什么,关系是什么
    windows--禁用驱动程序强制签名
    linux --kernel 和 OS 的关系区别
  • 原文地址:https://www.cnblogs.com/tomingto/p/12487283.html
Copyright © 2011-2022 走看看