【问题标题】:How to change originalFilename of MultipartFile如何更改 MultipartFile 的 originalFilename
【发布时间】:2016-03-10 10:45:24
【问题描述】:

我在服务器端有一个 MultipartFile 文件。我想更改这个文件的原始文件名,但该类只支持getOriginalFilename()。

谁能帮我解决这个问题? PS:是上传的图片文件。

非常感谢。

【问题讨论】:

  • 是否要为上传文件指定一个新名称并保存?
  • 您不能更改MultipleFile 的原始名称。但是你可以用另一个名字保存这个文件,什么时候将File传递给transferTo()方法。

标签: java multipartform-data


【解决方案1】:

您可以使用 MockMultipartFile 类更改名称。 例如,为多部分文件添加时间戳。

MultipartFile multipartFile = new  MockMultipartFile(FilenameUtils.getBaseName(oldMultipartFile.getOriginalFilename()).concat(new SimpleDateFormat("yyyyMMddHHmm").format(new Date())) + "." + FilenameUtils.getExtension(oldMultipartFile.getOriginalFilename()), oldMultipartFile.getInputStream());

然后使用新名称的 multipartFile 或者您可以像这样在保存之前重命名文件

  String currentDate = new SimpleDateFormat("yyyyMMddHHmm").format(new Date());

  file.getOriginalFilename().replace(file.getOriginalFilename(), FilenameUtils.getBaseName(file.getOriginalFilename()).concat(currentDate) + "." + FilenameUtils.getExtension(file.getOriginalFilename())).toLowerCase())

【讨论】:

  • 第二个答案不正确。我们不能这样直接替换原来的名字。我们需要将替换的结果存储在一个变量中。
  • 谢谢兄弟。你的回答很有帮助。
【解决方案2】:

只需通过更新它来创建一个新的 MultipartFile 实例

原始文件名

与具有相同现有文件字段的新另一个文件。

只需调用下面的方法为

getNewFile("newFileName", currentFile)

当前文件是现有文件的 MultipartFile 对象。

private MultipartFile getNewFile(String fileName, MultipartFile currentFile){
        return new MultipartFile() {
            @Override
            public String getName() {
                return currentFile.getName();
            }

            @Override
            public String getOriginalFilename() {
                return fileName;
            }

            @Override
            public String getContentType() {
                return currentFile.getContentType();
            }

            @Override
            public boolean isEmpty() {
                return currentFile.isEmpty();
            }

            @Override
            public long getSize() {
                return currentFile.getSize();
            }

            @Override
            public byte[] getBytes() throws IOException {
                return currentFile.getBytes();
            }

            @Override
            public InputStream getInputStream() throws IOException {
                return currentFile.getInputStream();
            }

            @Override
            public void transferTo(File file) throws IOException, IllegalStateException {

            }
        };
    }

【讨论】:

    【解决方案3】:
    public String storeFile(MultipartFile file) {
            String fileName = StringUtils.cleanPath(file.getOriginalFilename());
    //this will provide the complete filename, let "file.txt";
            File fileSaved = fileRepository.save(new File());
    //let's save that on a repo, not file, just some reference, easy to handle
            fileName = fileName.substring(0, fileName.length() - 4) + fileSaved.getFileId() + fileName.substring(fileName.length() - 4);
    //change the file name a bit, just put the id from the repo, it will be unique by default, right ? and what's that 4 - ".txt" has 4 alphabets, currently i neeed to handle only txt, so I hard-coded this
            Path targetLocation = this.fileStorageLocation.resolve(fileName);
            Files.copy(file.getInputStream(), targetLocation, StandardCopyOption.REPLACE_EXISTING);
    //it will be never replaced, even filenames are same,id will be unique
    }
    

    【讨论】:

      猜你喜欢
      • 2013-09-23
      • 2020-05-29
      • 2019-03-28
      • 1970-01-01
      • 1970-01-01
      • 2020-10-17
      • 1970-01-01
      • 1970-01-01
      • 2019-08-03
      相关资源
      最近更新 更多