• FastDFS工具类-将远程图片保存到本地文件服务器


    如下代码,FastFileStorageClient对象取自fastdfs-client-1.26.5.jar的接口com.github.tobato.fastdfs.service.FastFileStorageClient。

    代码中上传图片调用的fastdfs的api是

    StorePath uploadFile(InputStream inputStream, long fileSize, String fileExtName, Set<MetaData> metaDataSet);
        @Autowired
        private FastFileStorageClient storageClient;
        
        /**
         * 将远程图片保存到fastdfs文件服务器
         * @param remoteFileUrl 图片绝对地址,如:http://192.168.40.84:8888/group1/M00/00/1A/wKgoVF7m0XuAHK0SAAE330xWhSI27.jpeg
         * @return 样例如:group1/M00/00/07/wKgoVF4X5JqAOIh_AAEl8OqJ-7k487.jpg
         */
        public String downAndUpload(String remoteFileUrl) {
            try {
                URL url = new URL(remoteFileUrl);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setConnectTimeout(5 * 1000);
                httpURLConnection.connect();
    //            System.out.println("-------------" + httpURLConnection.getInputStream().available());
    //            System.out.println(httpURLConnection.getContentLength());
                StorePath jpg = storageClient.uploadFile(httpURLConnection.getInputStream(), httpURLConnection.getContentLength(), "jpg", null);
    //            System.out.println("-1  ---------------1324   http://192.168.40.198:8888/" + jpg.getFullPath());
    
                httpURLConnection.disconnect();
                return jpg.getFullPath();
            } catch (IOException ex) {
                logger.error("URL 不存在或者连接超时", ex);
                return "";
            }
        }
  • 相关阅读:
    poj2356
    poj2021
    网络直销:贯通式电子商务营销
    网络营销趋同化时代到来
    一个睡五分钟等于六个钟头的方法
    张胜(帮别人名字作诗)
    我在阳光下向你表白
    李建逸(帮别人名字作诗)
    夜深,你的手机为谁而开
    杨英(帮别人名字作诗)
  • 原文地址:https://www.cnblogs.com/buguge/p/14607423.html
Copyright © 2020-2023  润新知