在Java中使用Linux路径时POST请求失败
我的Spring应用程序中有以下@Service类:在Java中使用Linux路径时POST请求失败,java,angular,linux,uri,Java,Angular,Linux,Uri,我的Spring应用程序中有以下@Service类: @Service public class FileService { //@Autowired //CustomerDetailsRepository customerDetailsRepository; //private static final String FILE_DIRECTORY = "D:\\temp\\xmlFromAdmin"; private static
@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是正确的。这里的错误是什么?这是服务文件中的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);
}
uploadLicenseFile(文件名:string,文件:file):可观察{
let headers=新的HttpHeaders({
“访问控制允许来源”:“*”,
“访问控制允许方法”:“获取、发布、放置、删除、修补、选项”,
//“访问控制允许标头”:“X-Requested-With,内容类型,授权”
“访问控制允许标头”:“内容类型,接受,X-Access-Token,X-Key,授权,X-Requested-With,来源,访问控制允许来源,访问控制允许凭据,内容类型=多部分/*”
})
let options={headers:headers,observer:'response'};
const formData:formData=new formData();
append('fileName',fileName);
formData.append('file',file);
//返回this.http.post(this.url+'/fileUpload/upload',formData,options)
const req=new HttpRequest('POST',this.url+'/fileUpload/upload',formData{
报告进展:是的,
responseType:'json'
});
返回这个.http.request(req);
}
对于linux来说,这不是应该的吗
Path filePath = Paths.get(FILE_DIRECTORY + "\" + fileName);
File fullFilePath = new File(FILE_DIRECTORY + "\" + fileName);
在这两种情况下(linux、windows)都可以使用path.get
问题在于许可。上传的文件是由tomcat用户创建的,而我创建的目录是由root用户创建的。现在已经绕过了目录创建。问题:为什么要从Angular发送POST请求中的
访问控制允许-*
标题?我刚刚收到一些CORS错误。我不知道是否需要它,但在Windows上它工作得很好。我明白了。你不需要送他们。当在服务器端启用CORS时,它们来自服务器端。这篇文章在没有它们的情况下应该可以工作。问题是,如果它在Windows上工作,为什么在Linux上会出错。我正在使用tomcat和war文件。是否因为文件路径未正确指定?请检查您是否有权访问/opt
路径上的rw
权限。我尝试的rhis无效。所有的道路都是一样的。如果我也使用任何其他路径,则会出现此错误。前端的形式是/usr/local/apache/webapps文件夹中的ROOT/folder,其所有者是ROOT。war文件后端代码位于同一目录中,但其所有者是tomcatLinux,它使用/
作为目录分隔符。只有Windows使用反斜杠。但是,正确的方法是将多个参数传递给path.get,这是正确的,可以保证在所有平台上工作。
Path filePath = Paths.get(FILE_DIRECTORY + "\" + fileName);
File fullFilePath = new File(FILE_DIRECTORY + "\" + fileName);
Path filePath = Paths.get(FILE_DIRECTORY, fileName);
File fullFilePath = new File(Paths.get(FILE_DIRECTORY, fileName).toUri());