Search in sources :

Example 1 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseExcelFileController method excelPreviewSave.

/**
 * 预览后提交保存
 *
 * @param params 请求参数;必需包含预览返回的文件uuid
 * @return
 * @throws Exception
 */
public JsonResult<Map<String, Object>> excelPreviewSave(Map<String, Object> params) throws Exception {
    String uuid = params.get("uuid").toString();
    if (V.isEmpty(uuid)) {
        throw new BusinessException("未知的预览保存");
    }
    UploadFile uploadFile = uploadFileService.getEntity(uuid);
    uploadFile.setDescription(params.compute("description", (k, v) -> S.defaultIfEmpty(S.valueOf(v), "Excel预览后导入数据")).toString());
    return importData(uploadFile, fileStorageService.getFile(uploadFile.getStoragePath()), params);
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) UploadFile(com.diboot.file.entity.UploadFile)

Example 2 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseFileController method saveFile.

/**
 * 保存文件
 *
 * @param inputStream
 * @param fileName
 * @return
 * @throws Exception
 */
protected UploadFile saveFile(InputStream inputStream, String fileName) throws Exception {
    UploadFileResult uploadFileResult = fileStorageService.upload(inputStream, fileName);
    String accessUrl = buildAccessUrl(uploadFileResult.getFilename());
    UploadFile uploadFile = new UploadFile();
    uploadFile.setUuid(uploadFileResult.getUuid()).setFileName(uploadFileResult.getOriginalFilename()).setFileType(uploadFileResult.getExt()).setStoragePath(uploadFileResult.getStorageFullPath()).setAccessUrl(accessUrl);
    // 返回uploadFile对象
    return uploadFile;
}
Also used : UploadFile(com.diboot.file.entity.UploadFile) UploadFileResult(com.diboot.file.dto.UploadFileResult)

Example 3 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseFileController method uploadFile.

/**
 * 直接上传文件
 * @param uploadFileFormDTO
 * @return
 * @throws Exception
 */
public JsonResult uploadFile(UploadFileFormDTO uploadFileFormDTO) throws Exception {
    if (uploadFileFormDTO == null || uploadFileFormDTO.getFile() == null) {
        throw new BusinessException(Status.FAIL_INVALID_PARAM, "未获取待处理的文件!");
    }
    MultipartFile file = uploadFileFormDTO.getFile();
    String originFileName = file.getOriginalFilename();
    if (V.isEmpty(originFileName) || !isValidFileExt(file)) {
        log.debug("非法的文件类型: " + originFileName);
        throw new BusinessException(Status.FAIL_VALIDATION, "请上传合法的文件格式!");
    }
    // 保存文件
    UploadFile uploadFile = saveFile(uploadFileFormDTO);
    // 保存上传记录
    createUploadFile(uploadFile);
    // 返回结果
    return JsonResult.OK(new HashMap(16) {

        {
            put("uuid", uploadFile.getUuid());
            put("accessUrl", uploadFile.getAccessUrl());
            put("fileName", uploadFile.getFileName());
        }
    });
}
Also used : BusinessException(com.diboot.core.exception.BusinessException) MultipartFile(org.springframework.web.multipart.MultipartFile) UploadFile(com.diboot.file.entity.UploadFile) HashMap(java.util.HashMap)

Example 4 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseExcelFileController method importData.

/**
 * 导入数据
 *
 * @param uploadFile  上传文件对象
 * @param inputStream excel文件输入流
 * @param params      请求参数
 * @return
 * @throws Exception
 */
private JsonResult<Map<String, Object>> importData(UploadFile uploadFile, InputStream inputStream, Map<String, Object> params) throws Exception {
    ReadExcelListener<?> listener = getExcelDataListener();
    listener.setUploadFileUuid(uploadFile.getUuid());
    listener.setRequestParams(params);
    // 读excel
    readExcelFile(inputStream, uploadFile.getFileType(), listener);
    uploadFile.setDataCount(listener.getProperCount());
    uploadFileService.updateEntity(uploadFile);
    String errorDataFilePath = listener.getErrorDataFilePath();
    if (errorDataFilePath == null) {
        return JsonResult.OK();
    }
    String errorDataFileName = uploadFile.getFileName().replaceFirst("\\.\\w+$", "_错误数据.xlsx");
    UploadFile errorFile;
    if (FileHelper.isLocalStorage()) {
        String errorDataFileUidName = S.substringAfterLast(errorDataFilePath, "/");
        String errorDataFileUid = S.substringBefore(errorDataFileUidName, ".");
        errorFile = new UploadFile().setUuid(errorDataFileUid).setFileType("excel").setFileName(errorDataFileName).setStoragePath(errorDataFilePath).setAccessUrl(buildAccessUrl(errorDataFileUidName));
    } else {
        errorFile = super.saveFile(new FileInputStream(errorDataFilePath), errorDataFileName);
        FileHelper.deleteFile(errorDataFilePath);
    }
    errorFile.setDataCount(listener.getErrorCount()).setRelObjType(uploadFile.getRelObjType()).setDescription(uploadFile.getFileName() + " - 错误数据");
    // 创建异常数据记录
    uploadFileService.createEntity(errorFile);
    return JsonResult.OK(new HashMap<String, Object>() {

        {
            put("totalCount", listener.getTotalCount());
            put("errorUrl", errorFile.getAccessUrl());
            put("errorCount", listener.getErrorCount());
            put("errorMsgs", listener.getErrorMsgs());
        }
    });
}
Also used : UploadFile(com.diboot.file.entity.UploadFile) FileInputStream(java.io.FileInputStream)

Example 5 with UploadFile

use of com.diboot.file.entity.UploadFile in project diboot by dibo-software.

the class BaseExcelFileController method uploadExcelFile.

/**
 * 直接上传excel
 *
 * @param file        excel文件
 * @param entityClass 对应实体class
 * @param params      请求参数
 * @return
 * @throws Exception
 */
public JsonResult<Map<String, Object>> uploadExcelFile(MultipartFile file, Class<?> entityClass, Map<String, Object> params) throws Exception {
    checkIsExcel(file);
    // 保存文件
    UploadFile uploadFile = super.saveFile(file, entityClass);
    uploadFile.setDescription(params.compute("description", (k, v) -> S.defaultIfEmpty(S.valueOf(v), "Excel导入数据")).toString());
    uploadFileService.createEntity(uploadFile);
    return importData(uploadFile, file.getInputStream(), params);
}
Also used : UploadFile(com.diboot.file.entity.UploadFile)

Aggregations

UploadFile (com.diboot.file.entity.UploadFile)10 BusinessException (com.diboot.core.exception.BusinessException)3 UploadFileResult (com.diboot.file.dto.UploadFileResult)3 HashMap (java.util.HashMap)3 ApplicationTest (com.diboot.file.example.ApplicationTest)1 Department (com.diboot.file.example.custom.Department)1 FileInputStream (java.io.FileInputStream)1 Test (org.junit.Test)1 Transactional (org.springframework.transaction.annotation.Transactional)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1