赞
踩
<dependency>
<groupId>com.github.tobato</groupId>
<artifactId>fastdfs-client</artifactId>
<version>1.27.2</version>
</dependency>
uploadFile(String groupName, InputStream inputStream, long fileSize, String fileExtName)
返回值:一个 StorePath 对象,其中封装文件的路径和 store 节点的地址。
uploadSlaveFile(String groupName, String masterFilename, InputStream inputStream, long fileSize, String prefixName, String fileExtName)
返回值:一个 StorePath 对象,表示成功上传后从文件存储的路径信息。
getMetadata(String groupName, String path)
返回值:Set 集合,其中包含多组 metadata 信息。每一条 metadata 是 k-v 形式的键值对。如果没有找到对应的 metadata 信息,则返回一个空的集合。
overwriteMetadata(String groupName, String path, Set
mergeMetadata(String groupName, String path, Set
queryFileInfo(String groupName, String path)
返回值:一个 FileInfo 对象,包含了文件的元数据信息。
deleteFile(String groupName, String path)
downloadFile(String groupName, String path, DownloadCallback callback)
返回值:一个泛型值对象 T,表示经过回调方法处理后的结果。
downloadFile(String groupName, String path, long fileOffset, long fileSize, DownloadCallback callback)
返回值:一个泛型值对象 T,表示经过回调方法处理后的结果。
fdfs:
# 超时时间
connect_timeout: 5000
# 读取时间
so_timeout: 30000
# 服务地址列表
tracker-list: 192.168.29.31:22122
# 解决限制文件大小
spring:
servlet:
multipart:
max-request-size: 1TB
max-file-size: 1TB
package com.zjl.util; import com.github.tobato.fastdfs.domain.fdfs.StorePath; import com.github.tobato.fastdfs.domain.proto.storage.DownloadByteArray; import com.github.tobato.fastdfs.service.FastFileStorageClient; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.IOException; import java.nio.charset.StandardCharsets; /** * @author: zjl * @datetime: 2024/4/7 * @desc: */ @Component public class FastdfsUtil { @Resource private FastFileStorageClient storageClient; /** * 上传 * * @param file * @return */ public String upload(MultipartFile file) { // 获取文件名 String filename = file.getOriginalFilename(); // 得到文件扩展名 String extName = filename.substring(filename.lastIndexOf(".") + 1); StorePath storePath = null; try { // 上传 storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(), extName, null); // 这个getFullPath是fastdfs返回的id,可通过这个实现图片浏览、视频播放、文件下载等操作 return storePath.getFullPath(); } catch (IOException e) { throw new RuntimeException(e); } } /** * 下载文件(写二进制流) * @param path * @return */ public ResponseEntity<byte[]> download(String fileName ,String path,HttpServletRequest request) throws Exception { StorePath storePath = StorePath.parseFromUrl(path); String substring = path.substring(path.lastIndexOf(".")); byte[] data = storageClient.downloadFile(storePath.getGroup(), storePath.getPath(), new DownloadByteArray()); fileName = this.getFilename(request,fileName); HttpHeaders httpHeaders = new HttpHeaders(); // 设置下载响应类型以及文件名 httpHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); httpHeaders.setContentDispositionFormData("attachment",fileName+substring ); return new ResponseEntity<>(data,httpHeaders, HttpStatus.OK); } //解决中文下载问题 public String getFilename(HttpServletRequest request, String filename) throws Exception { //ie浏览器的编码格式 String[] IEBrowserWords = {"MSIE","Trident","Edge"}; String userAgent = request.getHeader("User-Agent"); for (String ieBrowserWord : IEBrowserWords) { if(userAgent.contains(ieBrowserWord)){ return URLEncoder.encode(filename,"UTF-8"); } } //其他浏览器就采用这种编码格式 return new String(filename.getBytes("UTF-8"),"ISO-8859-1"); } }
package com.zjl.controller; import com.zjl.util.FastdfsUtil; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; /** * @author: zjl * @datetime: 2024/4/7 * @desc: */ @RestController public class MyFileController { @Resource private FastdfsUtil fastdfsUtil; @PostMapping("/upload") public String upload(MultipartFile file){ return fastdfsUtil.upload(file); } }
直接访问这个地址:http://192.168.29.31:8888/group1/M00/00/00/wKgdH2YSNj2ABnBCAAEQeS5bVeM384.jpg
@GetMapping("/download")
public ResponseEntity<byte[]> download(HttpServletRequest request) throws Exception {
//假设这是从其他业务层获取的文件路径
String filePath = "group1/M00/00/00/wKgdH2YSNj2ABnBCAAEQeS5bVeM384.jpg";
String fileName = "Spring权限管理";
return fastdfsUtil.download(fileName,filePath,request);
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。