我想在一个主根目录下创建目录。我试过这段代码:
private static String UPLOADED_FOLDER = "/opt/";
@PostMapping
public ResponseEntity<StringResponseDTO> uploadData(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes, @RequestParam("id") Integer merchant_id) throws Exception {
InputStream inputStream = file.getInputStream();
String originalName = file.getOriginalFilename();
String name = file.getName();
String contentType = file.getContentType();
long size = file.getSize();
LOG.info("name: " + name);
LOG.info("contentType: " + contentType);
LOG.info("size: " + size);
try {
byte[] bytes = file.getBytes();
File newFile = new File(UPLOADED_FOLDER + merchant_id, file.getOriginalFilename());
LOG.info("New file location: " + newFile.getAbsolutePath()); //Log the path
Files.write(newFile.toPath(), bytes);
} catch (IOException e) {
e.printStackTrace();
}
return ResponseEntity.ok(new StringResponseDTO(originalName));
}
但我得到例外:
2019-08-12 09:53:30,748 INFO [stdout] (default task-79) 09:53:30.747 [default task-79] INFO o.d.a.b.restapi.MerchantController - New file location: /opt/13/Screenshot 2019-08-01 at 14.58.59.png
2019-08-12 09:53:30,749 ERROR [stderr] (default task-79) java.nio.file.NoSuchFileException: /opt/13/Screenshot 2019-08-01 at 14.58.59.png
2019-08-12 09:53:30,750 ERROR [stderr] (default task-79) at java.base/sun.nio.fs.UnixException.translateToIOException(UnixException.java:92)
我需要将数字转换merchant_id成字符串吗?
jeck猫
相关分类