SeaweedFS 分布式 上传、下载、删除附件公共接口

这里就直接贴代码吧。

引用jar包:net.anumbrella.seaweedfs.core.FileTemplate;

一、SeaweedFS 上传附件接口

    @Autowired
    private FileTemplate template;
    
    /**
     * @Description: 上传附件接口
     * @Date: 2019/12/19
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传附件接口", notes = "上传附件,上传成功返回附件id")
    public RestResponse uploadFile(@RequestParam(name = "fileId",required = false) String fileId, @RequestParam("excelFile") MultipartFile file) throws IOException {
    try {
            if (file.isEmpty()) {
                return RestResponse.fail().setMsg("上传文件不能为空!");
            }
            if (!StringUtils.isEmpty(fileId)){
                return RestResponse.ok(template.updateFileByStream(fileId,file.getOriginalFilename(),file.getInputStream()));
            }
            ContentType contentType = ContentType.create("application/x-www-form-urlencoded", Consts.ISO_8859_1);
            return RestResponse.ok(template.saveFileByStream(file.getOriginalFilename(), file.getInputStream(),contentType));
        } catch (Exception e) {
            logger.error("上传附件失败", e);
            return RestResponse.fail().setMsg("上传附件服务连接关闭");
        }
    }

 

二、SeaweedFS  下载附件接口

    @Autowired
    private FileTemplate template;    

    /**
     * @Description: 上传附件接口
     * @Date: 2019/12/19
     */
    @GetMapping("/getFileInfo/{fileId}")
    @ApiOperation(value="公共下载附件接口",notes = "公共下载附件接口")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "fileId", value = "附件id", dataType = "String", required = true)})
    public RestResponse getFileInfo(@PathVariable String fileId) throws IOException {
        FileHandleStatus status = template.getFileStatus(fileId);
        String name = new String(status.getFileName().substring(8,status.getFileName().length()-1).getBytes("iso-8859-1"),"utf-8");
        SeaweedResponse response = new SeaweedResponse();
        if (status != null){
            response.setFileId(status.getFileId());
            response.setFileName(name);
            response.setFilePath(template.getFileUrl(fileId));
        }
        return RestResponse.ok(response);
    }

 

ps:

    解决升级seaweed服务器,导致的获取附件名称时get (“Content-Disposition”) 报 NullPointerException 的问题(完善)

/**
     * 获取附件的详情
     * @return
     */
    @GetMapping("/getFileInfo/{fileId}")
    @ApiOperation(value="公共下载附件接口",notes = "下载附件接口")
    @ApiImplicitParams(value = {@ApiImplicitParam(name = "fileId", value = "附件id", dataType = "String", required = true)})
    public RestResponse getFileInfo(@PathVariable String fileId) throws IOException {
        CloseableHttpClient client = null;
        String name = "未命名";
        try {
            String url = getTemplate().getFileUrl(fileId);
            client = HttpClientBuilder.create().build();
            HttpGet get = new HttpGet(url);
            get.addHeader("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36");
            HttpResponse response = client.execute(get);
            String s = response.getFirstHeader("Content-Disposition").getValue();
            name = new String(s.substring(18,s.length()-1).getBytes("iso-8859-1"), "utf-8");
        }
        catch (Exception e){
            reConnect();
            logger.error("公共下载附件出错",e);
        }
        finally {
            if(client != null)
            {
                client.close();
            }
        }

        SeaweedResponse response = new SeaweedResponse();
        response.setFileId(fileId);
        response.setFileName(name);
        response.setFilePath(getTemplate().getFileUrl(fileId));
        return RestResponse.ok(response);
    }

 

三、SeaweedFS 删除附件接口

    @Autowired
    private FileTemplate template;

     /**
     * @Description: 上传附件接口
     * @Date: 2019/12/19
     */
    @PostMapping("/delete")
    @ApiOperation(value="公共删除附件接口",notes = "删除附件接口")
    public RestResponse deleteFile(@RequestParam(name = "fileId") String fileId) throws IOException {
        if (StringUtils.isEmpty(fileId)) {
            return RestResponse.fail().setMsg("fileId 参数不能为空");
        }
        template.deleteFile(fileId);
        return RestResponse.ok("删除成功");
    }

 

我是进阶的球儿,大家一起2019年的爬坑历程。感觉分享很给力的话给个赞,谢谢!!!有问题也可以下方留言或者加本人QQ:313989006 进行沟通。

你可能感兴趣的:(Java,SpringCloud)