使用Fastdfs,操作附件的上传与下载

Fastdfs的安装,请根据网络上的文章以及参考余庆老师github上的install,自行摸索,本文不在赘述

余庆老师的github地址Mafly的fastdfs安装南:
https://link.jianshu.com?t=https%3A%2F%2Fgithub.com%2Fhappyfish100

下面进入正题:


```java
1.在pom.xml中添加如下依赖(这里没有直接选用余庆老师提供的fastdfs-client-java
)

<dependency>
    <groupId>com.github.tobato</groupId>
    <artifactId>fastdfs-client</artifactId>
    <version>1.25.2-RELEASE</version>
</dependency>

2.添加配置信息application.yml

```xml
fdfs:
  soTimeout: 1500
  connectTimeout: 600
  thumbImage:             #缩略图生成参数
    width: 150
    height: 150
  trackerList:            #TrackerList参数,支持多个
    xx.xx.xxx.xx:22122 #请更改为自己的Tracker服务地址

3.多文件上传,下载

文件上传时,直接调用FastFileStorageClient.uploadFile()方法即可。
但是下载时,如果单单调用FastFileStorageClient.downloadFile()方法,那么下载下来的文件名对用户会很不友好,本例将下载返回原名称。


```java
@RestController
public class FastdfsController {
    @Autowired
    FastFileStorageClient fastFileStorageClient;
    /**
     * 多文件上传
     * @param files
     * @return
     * @throws IOException
     */
    @PostMapping(value = "/upload",produces = "application/json;charset=UTF-8")
    public bool uploadFilesWithFastdfs(@RequestParam("file") MultipartFile[] files)throws IOException{
        List<FileInfo> fileInfoList = new ArrayList<>();
        for(int i = 0; i<files.length;i++){
            MultipartFile multipartFile = files[i];
            String fileName = multipartFile.getOriginalFilename();
            StorePath storePath = fastFileStorageClient.uploadFile(multipartFile.getInputStream(),multipartFile.getSize(),fileName.substring(fileName.lastIndexOf(".")+1),null);
            //真实的情况会将数据信息进行保存,包含的信息(文件名,存储路径,是谁的附件等信息),那么入库的过程我就不说了
        }
        return true;       
    }
    /**
     * 单文件下载
     * @param id
     * @param httpServletResponse
     * @throws MalformedURLException
     */
    @GetMapping(value = "/download/{id}")
    public void downloadFilesWithFastdfs(@PathVariable String id,HttpServletResponse httpServletResponse) throws MalformedURLException {
        //操作数据库,读取文件上传时的信息
        FileInfo fileInfo = service.getFileInfo(id);
        if(fileInfo!=null){
            try {           
                String fileName = URLEncoder.encode(fileInfo.getFileName(),"UTF8");
                String fileUrl = fileInfo.getFileUrl();
                String filepath = fileUrl.substring(fileUrl.lastIndexOf("group1/")+7);
                DownloadByteArray callback = new DownloadByteArray();
                byte[] b = fastFileStorageClient.downloadFile("group1", filepath,callback);
                httpServletResponse.reset();
                httpServletResponse.setContentType("application/x-download");
                httpServletResponse.addHeader("Content-Disposition" ,"attachment;filename=\"" +fileName+ "\"");
                httpServletResponse.getOutputStream().write(b);
                service.updateDownloadCount(id);
                httpServletResponse.getOutputStream().close();


            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

最后,文件删除也很简单
调用fastFileStorageClient.deleteFile()即可。


本文源作者连接:http://www.360doc.com/content/19/0926/22/39194723_863411601.shtml


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值