这篇文章主要介绍Java怎么实现整合文件上传到FastDFS,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
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@Slf4jpublic 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”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注编程网行业资讯频道!