java - 文件未重命名

标签 java spring

我正在尝试实现这个 Spring 端点:

private static String UPLOADED_FOLDER = "/opt/";

@PostMapping(value = "/upload", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<StringResponseDTO> uploadFile(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes, @RequestParam("id") Integer merchant_id) throws Exception {

        InputStream inputStream = file.getInputStream();

        try {
            byte[] bytes = file.getBytes();

            File directory = new File(UPLOADED_FOLDER, merchant_id.toString());
            directory.mkdirs();
            File newFile = new File(directory, file.getOriginalFilename());
            newFile.renameTo(new File("merchant_logo.png"));
            Files.write(newFile.toPath(), bytes);

            redirectAttributes.addFlashAttribute("message",
                    "You successfully uploaded '" + file.getOriginalFilename() + "'");

        } catch (IOException e) {
            e.printStackTrace();
        }

        return ResponseEntity.ok(new StringResponseDTO(originalName));
    }

总体思路是重命名文件并覆盖以前的同名文件。但由于某种原因它不起作用。我得到旧文件内容。知道为什么吗?

最佳答案

我正在使用java 1.8,也许这会对你有所帮助。

    @PostMapping(value = "/upload", produces = { MediaType.APPLICATION_JSON_VALUE })
    public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file,
            RedirectAttributes redirectAttributes, @RequestParam("id") Integer merchantId) throws Exception {
        try {
            File directory = new File(properties.getFileUploadDir(), merchantId.toString());
            directory.mkdirs();
            Path writeTargetPath = Files.write(
                    Paths.get(directory.getAbsolutePath(), file.getOriginalFilename()).toAbsolutePath(),
                    file.getBytes(), StandardOpenOption.CREATE_NEW);
            Path fileToMovePath = Paths.get(properties.getFileUploadDir(), merchantId.toString(), "merchant_logo.png");
            Path movedPath = Files.move(writeTargetPath, fileToMovePath, StandardCopyOption.REPLACE_EXISTING);
            log.info("movedPath: {}", movedPath.toAbsolutePath());

            redirectAttributes.addFlashAttribute("message",
                    "Successfully uploaded '" + file.getOriginalFilename() + "'");
        } catch (IOException e) {
            log.error("IOException: {}", e);
            return ResponseEntity.ok("Upload failed'" + file.getOriginalFilename() + "'");
        }
        return ResponseEntity.ok("Successfully uploaded'" + file.getOriginalFilename() + "'");
    }

关于java - 文件未重命名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57621740/

相关文章:

spring - 在batchUpdate中使用NamedParameterJDBCTemplate时如何获取生成的 key

java - 如何从 Spring Rest 文档中排除一些测试?

java - 这里怎么分析时间复杂度呢?

java - 将电话号码编码为单词并搜索字典

java - JWT token 问题

java - spring mvc 中基于注解的 Controller 的依赖注入(inject)

java - 各种Android OpenGL相关类之间的关系是什么?

java - 如何在 TextView 中设置泰卢固语字体?

java - Spring StoredProcedure 结果集值修剪

spring security OAuth2 - 自定义 ClientDetailsS​​ervice