Java實現(xiàn)整合文件上傳到FastDFS的方法詳細(xì)
1.引入fastdfs依賴到pom.xml
<dependency> <groupId>com.github.tobato</groupId> <artifactId>fastdfs-client</artifactId> <version>1.26.5</version> </dependency>
2.上傳代碼如下
上傳純文件流
/** * 文件上傳 * @param file MultipartFile類型 * @return url */ @Override public String fileUpload(MultipartFile file) throws Exception { try { return upload(file); } catch (Exception e) { e.printStackTrace(); } throw new Exception(); }
上傳網(wǎng)絡(luò)資源鏈接:
/** * 文件上傳 * @param urlStr url地址 * @return url */ @Override public String fileUpload(String urlStr) throws Exception { try { //把地址轉(zhuǎn)換成URL對象 URL url = new URL(urlStr); //創(chuàng)建http鏈接 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //設(shè)置超時間為3秒 conn.setConnectTimeout(3*1000); //防止屏蔽程序抓取而返回403錯誤 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)"); //得到輸入流 InputStream inputStream = conn.getInputStream(); //截取鏈接中的文件名 String fileName= urlStr.substring(urlStr.lastIndexOf("/")+1); MultipartFile multipartFile = new MockMultipartFile(fileName,fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); //返回結(jié)果集 return upload(multipartFile); } catch (Exception e) { e.printStackTrace(); } throw new Exception(); }
整體代碼如下:
package com.tfjybj.arpro.crawl.service.impl; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.service.FastFileStorageClient; import com.tfjybj.arpro.crawl.service.FileUploadService; import com.tfjybj.arpro.crawl.util.CommonConfigurationUtil; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.apache.http.entity.ContentType; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mock.web.MockMultipartFile; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; /** * 文件上傳業(yè)務(wù)類 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/2/25 - 20:01 */ @Service @Slf4j public class FileUploadServiceImpl implements FileUploadService { @Autowired private FastFileStorageClient fastFileStorageClient; // 獲取配置文件中的配置IP地址 @Value("${fdfs.realIp}") private String realIp; // 獲取配置文件中的配置分組 @Value("${fdfs.groupName}") private String group; /** * 文件上傳 * @param file MultipartFile類型 * @return url */ @Override public String fileUpload(MultipartFile file) throws Exception { try { return upload(file); } catch (Exception e) { e.printStackTrace(); } throw new Exception(); } /** * 文件上傳 * @param urlStr url地址 * @return url */ @Override public String fileUpload(String urlStr) throws Exception { try { //把地址轉(zhuǎn)換成URL對象 URL url = new URL(urlStr); //創(chuàng)建http鏈接 HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //設(shè)置超時間為3秒 conn.setConnectTimeout(3*1000); //防止屏蔽程序抓取而返回403錯誤 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)"); //得到輸入流 InputStream inputStream = conn.getInputStream(); //截取鏈接中的文件名 String fileName= urlStr.substring(urlStr.lastIndexOf("/")+1); MultipartFile multipartFile = new MockMultipartFile(fileName,fileName, ContentType.APPLICATION_OCTET_STREAM.toString(), inputStream); //返回結(jié)果集 return upload(multipartFile); } catch (Exception e) { e.printStackTrace(); } throw new Exception(); } /** * 文件上傳 * @param file 需要上傳的文件 * @return 上傳后的文件地址 */ public String upload(MultipartFile file) { try { // 1.文件信息校驗 if (file.isEmpty()) { log.debug("需要上傳的文件信息不通過"); return null; } // 2.保存圖片到fastDFS服務(wù)器 //2.1 獲取文件后綴名 String extension = StringUtils.substringAfterLast(file.getOriginalFilename(), "."); //2.2 保存 StorePath storePath = fastFileStorageClient.uploadFile(group, file.getInputStream(), file.getSize(), extension); // 獲取附件的完整地址 String Path = CommonConfigurationUtil.HTTP + CommonConfigurationUtil.ECOLON + CommonConfigurationUtil.DOUBLE_SLASH + realIp + CommonConfigurationUtil.SINGLE_SLASH + storePath.getFullPath(); log.info("文件上傳成功,文件地址:" + Path); return Path; } catch (Exception ex) { ex.printStackTrace(); return null; } } }
3.配置文件如下
# 文件服務(wù)器基礎(chǔ)配置 fdfs: groupName: ar so-timeout: 1500 connect-timeout: 600 tracker-list: d-fastdfs.xxxx.com:22122 replace-ip: source: d-fastdfs.xxxx.com dest: d-fastdfs.xxxx.com realIp: d-fastdfs.xxxx.com
4.上傳效果如下
無論是純文件上傳還是以網(wǎng)絡(luò)資源鏈接的形式上傳都是文件流上傳的形式。
到此這篇關(guān)于Java實現(xiàn)整合文件上傳到FastDFS的方法詳細(xì)的文章就介紹到這了,更多相關(guān)Java文件上傳FastDFS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Windows10系統(tǒng)下JDK1.8的下載安裝及環(huán)境變量配置的教程
這篇文章主要介紹了Windows10系統(tǒng)下JDK1.8的下載安裝及環(huán)境變量配置的教程,本文圖文并茂給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03Java開發(fā)編程到底是用idea好還是eclipse好
這篇文章主要介紹了Java開發(fā)編程到底是用idea好還是eclipse好,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08如何使用Java模擬退火算法優(yōu)化Hash函數(shù)
為了解決局部最優(yōu)解問題,1983年,Kirkpatrick等提出了模擬退火算法(SA)能有效的解決局部最優(yōu)解問題。模擬退火算法包含兩個部分即Metropolis算法和退火過程。Metropolis算法就是如何在局部最優(yōu)解的情況下讓其跳出來,是退火的基礎(chǔ)2021-06-06java 運(yùn)行報錯has been compiled by a more recent version of the J
java 運(yùn)行報錯has been compiled by a more recent version of the Java Runtime (class file version 54.0)2021-04-04