美文网首页
文件上传transferTo路径问题

文件上传transferTo路径问题

作者: cjlynn | 来源:发表于2021-07-05 15:54 被阅读0次

file.transferTo(desc);文件找不到路径
将目的文件地址 File desc = new File(uploadDir + File.separator + fileName);
改为 File desc = new File(new File(uploadDir).getAbsolutePath() + File.separator + fileName);

private static final File getAbsoluteFile(String uploadDir, String fileName) throws IOException
    {
        File desc = new File(new File(uploadDir).getAbsolutePath() + File.separator + fileName);

        if (!desc.exists())
        {
            if (!desc.getParentFile().exists())
            {
                desc.getParentFile().mkdirs();
            }
        }
        return desc;
    }
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;
    }

相关文章

网友评论

      本文标题:文件上传transferTo路径问题

      本文链接:https://www.haomeiwen.com/subject/lwhiultx.html