适用版本:v3.3.6+;

dependency

<dependency>
   <groupId>com.lc.ibps.components</groupId>
   <artifactId>ibps-component-upload</artifactId>
</dependency>

配置说明

配置文件在application-app.yml

file:
  saveType: ${FILE_SAVETYPE:fastdfs}
  #window系统采用\\,linux采用/
  upload: /tmp/file/provider/boot
  #相对路径,则要把file.upload 去掉
  #file.realpath=/attachFiles/temp
  #####  文件上传的格式路径 ,默认 {uploadType}/{yyyy}/{MM};
  ##### 可以支持{curAccount}、 {curUserName}、{yyyy}、{MM}、{dd},文件夹中间/分割
  formatTpl: "{uploadType}/{yyyy}/{MM}"
  #最大文件上传限制,单位字节. 100M=100*1024*1024(B)=104857600 bytes 
  maxUploadSize: 104857600   

API说明

/**
 * 设置持久化对象
 *
 * @param fileInfoPersistenceService
 */
public void setFileInfoPersistenceService(IFileInfoPersistenceService<T> fileInfoPersistenceService);

/**
 * 
 * 实现类的类型 从枚举SaveType 中获取对应的值 标识每一个上传类型对应的实现类
 *
 * @return
 */
public String getSaveType();

/**
 * 上传文件
 * 
 * @param is
 *            需要上传的文件
 * @param params
 *            参数
 * @return
 * @throws Exception
 */
public T uploadFile(InputStream is, Map<String, Object> params) throws Exception;

/**
 * 
 * 删除单一文件
 * 
 * @param fileId
 *            持久化后的数据库Id
 * @throws Exception
 */
public void deleteFile(String fileId) throws Exception;

/**
 * 
 * 删除文件
 * 
 * @param fileIds
 *            持久化后的数据库Id
 * @throws Exception
 */
public void deleteFile(String[] fileIds) throws Exception;

/**
 * 下载文件
 * 
 * @param fileId
 *            持久化后的数据库Id
 * @return
 */
public T downloadFile(String fileId) throws Exception;

/**
 * 
 * 根据附件id 获取文件的二进制数据
 *
 * @param fileId
 *            持久化后的数据库Id
 * @return
 */
public byte[] getFile(String fileId) throws Exception;

/**
 * 检查文件是否存在,实现秒传
 *
 * @param params
 * @return
 */
public boolean checkMd5(String md5) throws Exception;

/**
 * 
 * 检查分块是否存在
 *
 * @param params
 * @return
 */
public boolean checkChunk(Map<String, Object> params) throws Exception;

/**
 * 合并分块文件
 *
 * @param params
 * @return
 * @throws Exception
 */
public T mergeChunks(Map<String, Object> params) throws Exception;

/**
 * 保存文件
 *
 * @param params
 * @return
 */
public T saveFile(Map<String, Object> params) throws Exception;

/**
 * 检查文件是否存在。可能被删除
 *
 * @param FileInfo
 * @return
 */
public boolean checkFileExists(T fileInfo) throws Exception;

使用示例

/**
 * 
 * 获取上传组件服务
 *
 * @return
 */
    private IUploadService<?> getUploadService() {
       UploadStrategySelector<?> uploadStrategySelector = AppUtil.getBean(UploadStrategySelector.class);
       return uploadStrategySelector.getIUploadService(true);
    }

/**
     * 保存附件
     *
     * @param uploadService
     * @param mail
     * @return
     * @throws Exception
     */
    private String saveFiles(IUploadService<?> uploadService, Mail mail) throws Exception {
        List<MailAttachment> attachments = mail.getMailAttachments();
        if (BeanUtils.isEmpty(attachments))
            return null;
        List<String> fileIds = new ArrayList<String>();
        for (MailAttachment mailAttachment : attachments) {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("originalFilename", mailAttachment.getFileName());
            params.put("uploadType", "mail");
            try {
                FileInfo fileInfo = uploadService.uploadFile(mailAttachment.getInputStream(), params);
                fileIds.add(fileInfo.getId());
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
        }
        return StringCollections.toString(fileIds);
    }

    private void test() {
        IUploadService<?> uploadService = getUploadService();
        Mail mail = new Mail();
        String fileIds = saveFiles(uploadService, mail);
        System.out.println(fileIds);


    }
文档更新时间: 2021-01-04 13:55   作者:Eddy