Spring boot 收到java.nio.file.AccessDeniedException时出错
我正在尝试将图像存储在文件夹中。尝试这样做时,我收到了错误消息-java.nio.file.AccessDeniedException:“storagepath”。我已授予该文件夹的读写权限,但这并没有解决问题。 我一整天都在头痛,但找不到解决办法。请帮忙!! 控制器代码Spring boot 收到java.nio.file.AccessDeniedException时出错,spring-boot,Spring Boot,我正在尝试将图像存储在文件夹中。尝试这样做时,我收到了错误消息-java.nio.file.AccessDeniedException:“storagepath”。我已授予该文件夹的读写权限,但这并没有解决问题。 我一整天都在头痛,但找不到解决办法。请帮忙!! 控制器代码 @PostMapping("/product/postadd") public void addAElectronicsProduct(@RequestPart(value="produc
@PostMapping("/product/postadd")
public void addAElectronicsProduct(@RequestPart(value="product") ElectronicSellDAO Camera ,@RequestPart(value="imgs",required=true) List<MultipartFile> images) {
System.out.println(Camera.getEmail()+" "+Camera.getBrand());
System.out.println(images.size());
File file = new File(OUT_PATH);
// check if the file exists
boolean exists = file.exists(); if(exists ==
true) { // printing the permissions associated with the file
System.out.println("Executable: " + file.canExecute());
System.out.println("Readable: " + file.canRead());
System.out.println("Writable: "+ file.canWrite()); } else {
System.out.println("File not found."); }
//System.out.println(Camera.getProductId());
Path path1=null;
Path path2=null;
Path path3=null;
try {
MultipartFile img1=images.get(0);
byte[] bytes1=img1.getBytes();
path1=Paths.get(OUT_PATH,img1.getOriginalFilename());
Files.write(path1, bytes1);
System.out.println(path1.toString());
Camera.setImg1(img1.getOriginalFilename());
if(images.size()>1) {
MultipartFile img2=images.get(1);
byte[] bytes2=img2.getBytes();
path2=Paths.get(OUT_PATH,img2.getOriginalFilename());
Files.write(path2, bytes2);
Camera.setImg2(img2.getOriginalFilename());
System.out.println(path2.toString());
}
if(images.size()>2) {
MultipartFile img3=images.get(2);
byte[] bytes3=img3.getBytes();
path3=Paths.get(OUT_PATH,img3.getOriginalFilename());
Files.write(path3, bytes3);
Camera.setImg3(img3.getOriginalFilename());
System.out.println(path3.toString());
}
electronicsService.addAElectronicsProduct(Camera);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
您错过了共享类的导入语句。
我简化了您的代码示例,并通过适当的导入完成了它。
然后在我的Windows10硬盘上写入一个可比较的路径就没有问题了 以下是它应该如何工作:
package de.bsi.restdemo;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ItemController {
@GetMapping("/product")
public void addAElectronicsProduct() throws IOException {
final String OUT_PATH = "C:\\Users\\geheim\\Desktop\\test";
File file = new File(OUT_PATH);
System.out.println("Executable: " + file.canExecute());
System.out.println("Readable: " + file.canRead());
System.out.println("Writable: " + file.canWrite());
byte[] bytes1 = new byte[10];
Path path1 = Paths.get(OUT_PATH, "Screenshot (160).png");
Files.write(path1, bytes1);
System.out.println(path1.toString());
}
}
然后我将此记录到控制台:
Executable: true
Readable: true
Writable: true
C:\Users\geheim\Desktop\test\Screenshot (160).png
因此,请仔细检查您的导入语句,问题可能就在那里。使代码更简单,这样就不会与其他问题混淆 在我的德语博客中,我有一些关于Spring Boot的帖子:
谢谢您尝试Elmar。我在下面分享我的导入,它们与你的匹配。但是,它仍然抛出了这个错误<代码>导入java.io.File;导入java.io.IOException;导入java.nio.file.Files;导入java.nio.file.Path;导入java.nio.file.path;导入java.util.List为了进一步调查,我会更改文件系统中的路径,看看那里发生了什么。
Executable: true
Readable: true
Writable: true
C:\Users\geheim\Desktop\test\Screenshot (160).png