zoukankan      html  css  js  c++  java
  • 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 "";
            }
        }
  • 相关阅读:
    综合练习:词频统计
    Dart SDK 2.0安装问题
    The DartEditor executable launcher was unable to locate its companion shared library.
    pycharm中如何正确配置pyqt5
    发现黑苹果带双显示器无法启动的原因
    Pycharm中用鼠标改变字体大小
    失望的visual studio for mac
    laravel 函数测试 --- Route::has()
    laragon 之Nginx
    laragon 之xdebug
  • 原文地址:https://www.cnblogs.com/buguge/p/14607423.html
Copyright © 2011-2022 走看看