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 "";
            }
        }
  • 相关阅读:
    UVa 11300 Spreading the Wealth(有钱同使)
    hihoCoder 1385 : A Simple Job(简单工作)
    hihoCoder 1383 : The Book List(书目表)
    HDU 5724 Chess(国际象棋)
    Sobytiynyy Proyekt Casino Gym
    Course recommendation Gym
    Poor Folk Gym
    How far away? (HDU
    BAPC 2016 ----Brexit (BFS + vector)
    Simpsons’ Hidden Talents(扩展KMP)
  • 原文地址:https://www.cnblogs.com/buguge/p/14607423.html
Copyright © 2011-2022 走看看