1.引入fastdfs依赖到pom.xml
1 2 3 4 5 |
< dependency > < groupId >com.github.tobato</ groupId > < artifactId >fastdfs-client</ artifactId > < version >1.26.5</ version > </ dependency > |
2.上传代码如下
上传纯文件流
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/** * 文件上传 * @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(); } |
上传网络资源链接:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
/** * 文件上传 * @param urlStr url地址 * @return url */ @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();
} |
整体代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
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测试数据mons.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;
/** * 文件上传业务类 * * @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 { //把地址转换成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();
}
/** * 文件上传 * @param file 需要上传的文件 * @return 上传后的文件地址 */ 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.配置文件如下
1 2 3 4 5 6 7 8 9 10 |
# 文件服务器基础配置 fdfs: groupName: ar so-timeout: 1500 connect-timeout: 600 tracker-list: d-fastdfs.xxxx测试数据: 22122 replace-ip: source: d-fastdfs.xxxx测试数据 dest: d-fastdfs.xxxx测试数据 realIp: d-fastdfs.xxxx测试数据 |
4.上传效果如下
无论是纯文件上传还是以网络资源链接的形式上传都是文件流上传的形式。
到此这篇关于Java实现整合文件上传到FastDFS的方法详细的文章就介绍到这了,更多相关Java文件上传FastDFS内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!
原文链接:https://blog.csdn.net/weixin_44684272/article/details/123147405
查看更多关于Java实现整合文件上传到FastDFS的方法详细的详细内容...