FastDFS数据上传(后台页面调后台接口)

原创
小哥 3年前 (2022-11-08) 阅读数 37 #大杂烩

1、controller:
请记住添加两条评论:@CrossOrigin // 注解方式@RestController

    //注入fastDFSClient,关于fastDFSClient初始化,在本专栏的另一篇文章中编写。
    @Autowired
    FastDFSClient fastDFSClient;
/**
     * 通用接口,dfs文件上传(文件标签和isAuth必修的;isAuth对于文件是否有效识别,定期清除无效文件,有效:0,无效:1; 为空时,默认值为0有效)
     * @param file
     * @param dto
     * @return ResultDto
     */
    @ApiOperation(value="通用接口,dfs文件上传(文件标签和isAuth必修的;isAuth对于文件是否有效识别,定期清除无效文件,有效:0,无效:1; 为空时,默认值为0有效)", notes="null")
    @PostMapping(value = "/dfsupload")
    public ItsSingleResultDto dfsUpload(
            @ApiParam(value = "", required = true) @RequestParam( value="file", required = true) MultipartFile file,
            @ApiParam(value = "文件信息", required = true) @ModelAttribute FileQueryDto dto
            ) {
        ItsSingleResultDto result = new ItsSingleResultDto();//自定义返回值对象
            FileDetailDto newdto = new FileDetailDto();
            String staffCode = this.getStaffCode();//上传文件的人的卡号
            String name = file.getOriginalFilename();
            String suffix = name.substring(name.lastIndexOf(".") + 1, name.length());

            // FileDetailDto backpatha=new FileDetailDto();
            UploadDto uploadDto = new UploadDto();

            java.io.File f = new java.io.File(name);

            byte[] buffer = null;
            try {
                buffer = file.getBytes();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
            uploadDto.setFile(buffer);
            uploadDto.setFileName(name);
            // backpatha=service.dfsupload(uploadDto);//上载到服务器以供实施
            Map metaList = new HashMap();
            String backpath = fastDFSClient.uploadFile(uploadDto.getFile(), uploadDto.getFileName(), metaList);//进行上传
            // backpath = null;
            //执行循环,如果上载失败,循环将再次尝试上载。
            while (StringUtils.isEmpty(backpath)) {
                backpath = fastDFSClient.uploadFile(uploadDto.getFile(), uploadDto.getFileName(), metaList);
                System.out.println("00000+" + backpath);
            }
            System.out.println(backpath);
//-----------------------上传文件后,根据您自己的业务逻辑将记录插入数据库表。以下是所有业务处理。--------------------
            BeanUtils.copyProperties(dto, newdto);//属性对拷
            // newdto.setPath(fileName);
            newdto.setFilename(name);
            newdto.setSize(file.getSize());
            newdto.setType(suffix);
            newdto.setFakepath(backpath);
            newdto.setUploaderno(staffCode);
            FileDto filedto = new FileDto();
            BeanUtils.copyProperties(newdto, filedto);
            // 插入主表
            filedto.setDownloadcount(0);
            // 当前系统时间
            // Date day=new Date();
            Date day = com.dhc.its.utils.GlobalHelper.getDateTime();
            // System.out.println("11111111111+"+day);
            SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
            filedto.setUploadtime(df.format(day));
            if (StringUtils.isEmpty(dto.getIsAuth()) || dto.getIsAuth().equals("")) {
                filedto.setIsauth(Constant.NOT_DELETE);
            } else {
                filedto.setIsauth(dto.getIsAuth());
            }
            // filedto.setUploadtime(day);
            FileDto res = BeanMapper.map(fileAS.save(BeanMapper.map(filedto, File.class)), FileDto.class);
            dto.setFileuid(res.getFileuid());
            res.setFakepath(null);// 屏蔽fakepath
            // FileDetailDto result=BeanMapper.map(file, FileDetailDto.class);
            // 循环插入标签表
            FileTagDto fileTagDto = new FileTagDto();
            if (StringUtils.isEmpty(dto.getFileuid()) && StringUtils.isEmpty(dto.getTagid())) {

            } else {
                String tagarr[] = dto.getTagid().split(",");
                for (int i = 0; i < tagarr.length; i++) {
                    dto.setTagid(tagarr[i]);
                    BeanUtils.copyProperties(dto, fileTagDto);
                    Boolean fileTag = fileTagAS.create(BeanMapper.map(fileTagDto, FileTag.class));
            result.setCode(ResultCodeEnum.SUCCESS.getCode());
            result.setMessage(ResultCodeEnum.SUCCESS.getMessage());
            result.setData(res);

            return result;
    }

2、在FastDFSClient添加上载方法

/**
     * 上传文件
     * 
     * @param file
     *            文件对象
     * @param fileName
     *            文件名
     * @param metaList
     *            文件元数据
     * @return
     */
     @Autowired
    public StorageClient1 storageClient1;//这需要在上面初始化,请查看我的构建。FastDFS的client的博文。
    public String uploadFile(byte[] buff, String fileName, Map metaList) {
        try {
            // byte[] buff = IOUtils.toByteArray(new FileInputStream(file));

            NameValuePair[] nameValuePairs = null;
            if (metaList != null) {
                nameValuePairs = new NameValuePair[metaList.size()];
                int index = 0;
                for (Iterator> iterator = metaList.entrySet().iterator(); iterator
                        .hasNext();) {
                    Map.Entry entry = iterator.next();
                    String name = entry.getKey();
                    String value = entry.getValue();
                    nameValuePairs[index++] = new NameValuePair(name, value);
                }
            }
            return storageClient1.upload_file1(buff, FilenameUtils.getExtension(fileName), nameValuePairs);
            // FileUtils.getExtension(fileName)
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        // return storageClient1.upload_file1(buff,fileName,nameValuePairs);
    }

3,可能与pom依赖:

        
            org.csource
            fastdfs-client-java
            1.27-SNAPSHOT
        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter-test
            1.3.2
            test
        
        
            commons-io
            commons-io
            2.5
        
        
            org.csource
            fastdfs-client-java
            1.27-SNAPSHOT
        
        
               org.apache.pdfbox
               pdfbox
               2.0.13
               
        
            com.itextpdf
            itextpdf
            5.4.3
          
        
           com.lowagie
           itext
           2.1.7
          
         
            com.itextpdf
            itext-asian
            5.2.0
        
        
           io.github.openfeign.form
           feign-form-spring
           3.2.2
          
         
            io.github.openfeign.form
            feign-form
            3.2.2
        
版权声明

所有资源都来源于爬虫采集,如有侵权请联系我们,我们将立即删除

热门