在Java中使用Linux路径时POST请求失败



我的Spring应用程序中有以下@Service类:

@Service
public class FileService {

//@Autowired
//CustomerDetailsRepository customerDetailsRepository;
//private static final String FILE_DIRECTORY = "D:\temp\xmlFromAdmin";
private static final String FILE_DIRECTORY = "//opt//xmlFromAdmin";
static String fileName = "";

@Autowired
CustomerDetailsRepository customerDetailsRepository;

//private static CustomerDetails customerDetails;

//private static final String TRANSFORMED_FILE_DIRECTORY = "D:\temp\valuesToFrontend";
private static final String TRANSFORMED_FILE_DIRECTORY = "//opt//valuesToFrontend";

public void storeFile(MultipartFile file) throws Exception {
fileName = file.getOriginalFilename();

Path filePath = Paths.get(FILE_DIRECTORY + "/" + fileName);
File fullFilePath = new File(FILE_DIRECTORY + "/" + fileName);

String transformedFileName = TRANSFORMED_FILE_DIRECTORY+"/"+fileName;

Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);

Thread.sleep(1000);

FileDecryption fileDecryption = new FileDecryption();
fileDecryption.transformDocument(fullFilePath,transformedFileName);

在前端,我从用户那里获取一个XML文件,对其进行转换,并将值提供给前端。当我在本地Windows计算机上运行此代码时,此代码运行良好,带有注释掉的路径。然而,当我将代码放入Linux时,文件上传失败了。Linux中的路径也被指定。上传时我收到错误500,即使URL是正确的。这里的错误是什么?这是angular中服务文件中的POST请求:

uploadLicenseFile(fileName: string, file: File): Observable<any> {
let headers = new HttpHeaders({
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, PATCH, OPTIONS',
//'Access-Control-Allow-Headers': 'X-Requested-With, content-type, Authorization'
'Access-Control-Allow-Headers': 'Content-Type,Accept,X-Access-Token,X-Key,Authorization,X-Requested-With,Origin,Access-Control-Allow-Origin,Access-Control-Allow-Credentials,content-type=multipart/*'
})
let options = {headers:headers, observer: 'response'};
const formData: FormData = new FormData();
formData.append('fileName', fileName);
formData.append('file', file);
//return this.http.post(this.url+'/fileUpload/upload', formData,options)
const req = new HttpRequest('POST', this.url+'/fileUpload/upload', formData, {
reportProgress: true,
responseType: 'json'
});
return this.http.request(req);
}

问题出在权限上。上传的文件是由tomcat用户上传的,而我创建的目录是由root用户上传的。截至目前已跳过目录创建。

最新更新