覆盖 png 文件
Override png file
我想覆盖上传的文件。我试过这段代码:
@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.nio.file.FileAlreadyExistsException: /opt/1/download.png
你知道我每次上传文件时如何覆盖同名的旧文件吗?
StandardOpenOption.CREATE_NEW
的API documentation说:
Create a new file, failing if the file already exists.
将 StandardOpenOption.CREATE
与 StandardOpenOption.TRUNCATE_EXISTING
结合使用:
Path writeTargetPath = Files.write(
Paths.get(directory.getAbsolutePath(), file.getOriginalFilename()).toAbsolutePath(),
file.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
我想覆盖上传的文件。我试过这段代码:
@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.nio.file.FileAlreadyExistsException: /opt/1/download.png
你知道我每次上传文件时如何覆盖同名的旧文件吗?
StandardOpenOption.CREATE_NEW
的API documentation说:
Create a new file, failing if the file already exists.
将 StandardOpenOption.CREATE
与 StandardOpenOption.TRUNCATE_EXISTING
结合使用:
Path writeTargetPath = Files.write(
Paths.get(directory.getAbsolutePath(), file.getOriginalFilename()).toAbsolutePath(),
file.getBytes(), StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);