如下代码,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 ""; } }