chengxuyuan_java 2019-11-29 11:56 采纳率: 0%
浏览 665

java中什么方法可以在微信服务器上面把图片下载到本地服务器并保存(知道微信服务器上面图片的链接)

java中什么方法可以在微信服务器上面把图片下载到本地服务器并保存(知道微信服务器上面图片的链接)

  • 写回答

1条回答 默认 最新

  • VICTOR_fusheng 2019-11-29 17:05
    关注

    根据图片链接,转成byte存入数据库,数据库格式为Blob

        public static byte[] image2Base64(String imageUrl){
            URL url = null;
            InputStream is = null;
            ByteArrayOutputStream outputStream = null;
            HttpURLConnection connection = null;
            try {
                url = new URL(imageUrl);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.connect();
                is = connection.getInputStream();
                outputStream = new ByteArrayOutputStream();
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1){
                    outputStream.write(buffer, 0,len);
                }
                return outputStream.toByteArray();
            } catch (IOException e) {
                e.printStackTrace();
            }
            finally{
                if(is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if(connection != null) {
                    connection.disconnect();
                }
            }
            return null;
        }
    
        /**
         * 
         * @param imageUrl 图片地址
         * @param path 文件下载目录
         */
        public static void downloadImage(String imageUrl, String path) {
            URL url = null;
            InputStream is = null;
            OutputStream outputStream = null;
            HttpURLConnection connection = null;
            try {
                url = new URL(imageUrl);
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setConnectTimeout(5000);
                connection.connect();
                is = connection.getInputStream();
                String suffix = imageUrl.substring(imageUrl.lastIndexOf("."));
    
                File directory = new File(path);
                if (!directory.exists()){
                    directory.mkdirs();
                }
                if (!directory.isDirectory()){
                    throw new RuntimeException("所传的不是目录地址");
                }
                File file = new File(directory,UUID.randomUUID().toString() + suffix);
                outputStream = new FileOutputStream(file);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = is.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, len);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (is != null) {
                    try {
                        is.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (outputStream != null) {
                    try {
                        outputStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (connection != null) {
                    connection.disconnect();
                }
            }
        }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法