1.引入fastdfs依赖到pom.xml
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.26.5</version>
</dependency>
2.上传代码如下
上传纯文件流
@Override
public String fileUpload(MultipartFile file) throws Exception {
try {
return upload(file);
} catch (Exception e) {
e.printStackTrace();
}
throw new Exception();
}
上传网络资源链接:
@Override
public String fileUpload(String urlStr) throws Exception {
try {
//把地址转换成URL对象
URL url = new URL(urlStr);
//创建http链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为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);
//返回结果集
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;
@Service
@Slf4j
public class FileUploadServiceImpl implements FileUploadService {
@Autowired
private FastFileStorageClient fastFileStorageClient;
// 获取配置文件中的配置IP地址
@Value("${fdfs.realIp}")
private String realIp;
// 获取配置文件中的配置分组
@Value("${fdfs.groupName}")
private String group;
@Override
public String fileUpload(MultipartFile file) throws Exception {
try {
return upload(file);
} catch (Exception e) {
e.printStackTrace();
}
throw new Exception();
}
@Override
public String fileUpload(String urlStr) throws Exception {
try {
//把地址转换成URL对象
URL url = new URL(urlStr);
//创建http链接
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
//设置超时间为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);
//返回结果集
return upload(multipartFile);
} catch (Exception e) {
e.printStackTrace();
}
throw new Exception();
}
public String upload(MultipartFile file) {
try {
// 1.文件信息校验
if (file.isEmpty()) {
log.debug("需要上传的文件信息不通过");
return null;
}
// 2.保存图片到fastDFS服务器
//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.配置文件如下
# 文件服务器基础配置
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.上传效果如下
无论是纯文件上传还是以网络资源链接的形式上传都是文件流上传的形式。
到此这篇关于Java实现整合文件上传到FastDFS的方法详细的文章就介绍到这了,更多相关Java文件上传FastDFS内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!