Java實(shí)現(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è)置超時(shí)間為3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403錯(cuò)誤
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è)置超時(shí)間為3秒
conn.setConnectTimeout(3*1000);
//防止屏蔽程序抓取而返回403錯(cuò)誤
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.文件信息校驗(yàn)
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.com4.上傳效果如下

無論是純文件上傳還是以網(wǎng)絡(luò)資源鏈接的形式上傳都是文件流上傳的形式。
到此這篇關(guān)于Java實(shí)現(xiàn)整合文件上傳到FastDFS的方法詳細(xì)的文章就介紹到這了,更多相關(guān)Java文件上傳FastDFS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Java實(shí)現(xiàn)圖片上傳至FastDFS入門教程
- Java 客戶端操作 FastDFS 實(shí)現(xiàn)文件上傳下載替換刪除功能
- Java fastdfs客戶端實(shí)現(xiàn)上傳下載文件
- Java使用OSS實(shí)現(xiàn)上傳文件功能
- Java下載https文件并上傳阿里云oss服務(wù)器
- Java微信小程序oss圖片上傳的實(shí)現(xiàn)方法
- java實(shí)現(xiàn)上傳文件到oss(阿里云)功能示例
- java獲取網(wǎng)絡(luò)圖片上傳到OSS的方法
- Java實(shí)現(xiàn)Fast DFS、服務(wù)器、OSS上傳功能
相關(guān)文章
Windows10系統(tǒng)下JDK1.8的下載安裝及環(huán)境變量配置的教程
這篇文章主要介紹了Windows10系統(tǒng)下JDK1.8的下載安裝及環(huán)境變量配置的教程,本文圖文并茂給大家介紹的非常詳細(xì),對大家的工作或?qū)W習(xí)具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03
如何使用JAVA實(shí)現(xiàn)數(shù)字水印
本文介紹了如何使用JAVA實(shí)現(xiàn)數(shù)字水印,主要用到了java.awt包中的AlphaComposite類,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-07-07
Java開發(fā)編程到底是用idea好還是eclipse好
這篇文章主要介紹了Java開發(fā)編程到底是用idea好還是eclipse好,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
如何使用Java模擬退火算法優(yōu)化Hash函數(shù)
為了解決局部最優(yōu)解問題,1983年,Kirkpatrick等提出了模擬退火算法(SA)能有效的解決局部最優(yōu)解問題。模擬退火算法包含兩個(gè)部分即Metropolis算法和退火過程。Metropolis算法就是如何在局部最優(yōu)解的情況下讓其跳出來,是退火的基礎(chǔ)2021-06-06
java 運(yùn)行報(bào)錯(cuò)has been compiled by a more recent version of the J
java 運(yùn)行報(bào)錯(cuò)has been compiled by a more recent version of the Java Runtime (class file version 54.0)2021-04-04
J2ee 高并發(fā)情況下監(jiān)聽器實(shí)例詳解
這篇文章主要介紹了J2ee 高并發(fā)情況下監(jiān)聽器實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02

