buguge - Keep it simple,stupid

知识就是力量,但更重要的,是运用知识的能力why buguge?

导航

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 "";
        }
    }

 

posted on 2021-04-01 17:43  buguge  阅读(621)  评论(0编辑  收藏  举报