Search in sources :

Example 1 with FileNameLengthLimitExceededException

use of com.ruoyi.common.exception.file.FileNameLengthLimitExceededException in project wumei-smart by kerwincui.

the class FileUploadUtils method upload.

/**
 * 文件上传
 *
 * @param baseDir 相对应用的基目录
 * @param file 上传的文件
 * @param allowedExtension 上传文件类型
 * @return 返回上传成功的文件名
 * @throws FileSizeLimitExceededException 如果超出最大大小
 * @throws FileNameLengthLimitExceededException 文件名太长
 * @throws IOException 比如读写文件出错时
 * @throws InvalidExtensionException 文件校验异常
 */
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, InvalidExtensionException {
    int fileNamelength = file.getOriginalFilename().length();
    if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
        throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
    }
    assertAllowed(file, allowedExtension);
    String fileName = extractFilename(file);
    File desc = getAbsoluteFile(baseDir, fileName);
    file.transferTo(desc);
    String pathFileName = getPathFileName(baseDir, fileName);
    return pathFileName;
}
Also used : FileNameLengthLimitExceededException(com.ruoyi.common.exception.file.FileNameLengthLimitExceededException) MultipartFile(org.springframework.web.multipart.MultipartFile) File(java.io.File)

Example 2 with FileNameLengthLimitExceededException

use of com.ruoyi.common.exception.file.FileNameLengthLimitExceededException in project hocassian-media-matrix by hokaso.

the class FileUploadUtils method upload.

/**
 * 文件上传
 *
 * @param baseDir 相对应用的基目录
 * @param file 上传的文件
 * @param allowedExtension 上传文件类型
 * @return 返回上传成功的文件名
 * @throws FileSizeLimitExceededException 如果超出最大大小
 * @throws FileNameLengthLimitExceededException 文件名太长
 * @throws IOException 比如读写文件出错时
 * @throws InvalidExtensionException 文件校验异常
 */
public static final String upload(String baseDir, MultipartFile file, String[] allowedExtension) throws FileSizeLimitExceededException, IOException, FileNameLengthLimitExceededException, InvalidExtensionException {
    int fileNamelength = file.getOriginalFilename().length();
    if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
        throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
    }
    assertAllowed(file, allowedExtension);
    String fileName = extractFilename(file);
    File desc = getAbsoluteFile(baseDir, fileName);
    file.transferTo(desc);
    String pathFileName = getPathFileName(baseDir, fileName);
    return pathFileName;
}
Also used : FileNameLengthLimitExceededException(com.ruoyi.common.exception.file.FileNameLengthLimitExceededException) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile)

Example 3 with FileNameLengthLimitExceededException

use of com.ruoyi.common.exception.file.FileNameLengthLimitExceededException in project wumei-smart by kerwincui.

the class ToolController method uploadFile.

/**
 * 文件上传
 */
@PostMapping("/upload")
@ApiOperation("文件上传")
public AjaxResult uploadFile(MultipartFile file) throws Exception {
    try {
        String filePath = RuoYiConfig.getProfile();
        // 文件名长度限制
        int fileNamelength = file.getOriginalFilename().length();
        if (fileNamelength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
            throw new FileNameLengthLimitExceededException(FileUploadUtils.DEFAULT_FILE_NAME_LENGTH);
        }
        // 文件类型限制
        // assertAllowed(file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION);
        // 获取文件名和文件类型
        String fileName = file.getOriginalFilename();
        String extension = getExtension(file);
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MMdd-HHmmss");
        fileName = "/iot/" + getLoginUser().getUserId().toString() + "/" + df.format(new Date()) + "." + extension;
        // 创建目录
        File desc = new File(filePath + File.separator + fileName);
        if (!desc.exists()) {
            if (!desc.getParentFile().exists()) {
                desc.getParentFile().mkdirs();
            }
        }
        // 存储文件
        file.transferTo(desc);
        String url = "/profile" + fileName;
        AjaxResult ajax = AjaxResult.success();
        ajax.put("fileName", url);
        ajax.put("url", url);
        return ajax;
    } catch (Exception e) {
        return AjaxResult.error(e.getMessage());
    }
}
Also used : AjaxResult(com.ruoyi.common.core.domain.AjaxResult) FileNameLengthLimitExceededException(com.ruoyi.common.exception.file.FileNameLengthLimitExceededException) SimpleDateFormat(java.text.SimpleDateFormat) File(java.io.File) MultipartFile(org.springframework.web.multipart.MultipartFile) Date(java.util.Date) FileNameLengthLimitExceededException(com.ruoyi.common.exception.file.FileNameLengthLimitExceededException) IOException(java.io.IOException) ApiOperation(io.swagger.annotations.ApiOperation)

Aggregations

FileNameLengthLimitExceededException (com.ruoyi.common.exception.file.FileNameLengthLimitExceededException)3 File (java.io.File)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 AjaxResult (com.ruoyi.common.core.domain.AjaxResult)1 ApiOperation (io.swagger.annotations.ApiOperation)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Date (java.util.Date)1