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è)置超時間為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.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)文章
spring帶bean和config如何通過main啟動測試
這篇文章主要介紹了spring帶bean和config,通過main啟動測試,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07
Ribbon單獨(dú)使用,配置自動重試,實(shí)現(xiàn)負(fù)載均衡和高可用方式
這篇文章主要介紹了Ribbon單獨(dú)使用,配置自動重試,實(shí)現(xiàn)負(fù)載均衡和高可用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
springmvc和js前端的數(shù)據(jù)傳遞和接收方式(兩種)
本文介紹了springmvc和js前端的數(shù)據(jù)傳遞和接收方式(兩種),詳細(xì)的介紹了兩種方式,一種是json格式傳遞,另一種是Map傳遞,具有一定的參考價值,有興趣的可以了解一下2017-12-12
IDEA?報Plugin'maven-resources-plugin:'not?found?
如果在使用?IDEA?時遇到?"Plugin?'maven-resources-plugin:'?not?found"?錯誤,可能是由于?Maven?倉庫中未找到所需的?Maven?插件,近小編給大家分享幾種解決方法,感興趣的朋友跟隨小編一起看看吧2023-07-07
IDEA?Ui設(shè)計器JFormDesigner?永久激活插件+注冊機(jī)(親測一直在用)
這篇文章主要介紹了IDEA?Ui設(shè)計器JFormDesigner?永久激活----插件+注冊機(jī)?自己一直在用的版本和注冊機(jī),非常不錯,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08
Java趣味練習(xí)題之輸出兩個日期之間的相隔天數(shù)
本篇文章介紹了我看到的一個趣味小題目,怎么求得兩個日期之間相隔的天數(shù),以及解決該題目的過程及思路,通讀本篇對大家的學(xué)習(xí)或工作具有一定的價值,需要的朋友可以參考下2021-10-10

