在java中使用linux路径时,post请求失败

imzjd6km  于 2021-06-27  发布在  Java
关注(0)|答案(2)|浏览(402)

我的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上传时,即使网址是正确的。这里的错误是什么?以下是服务文件中的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);
  }
xytpbqjk

xytpbqjk1#

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

对于linux,这不应该是吗

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

path.get可以在两种情况下(linux、windows)使用,如下所示

Path filePath = Paths.get(FILE_DIRECTORY, fileName);
File fullFilePath = new File(Paths.get(FILE_DIRECTORY, fileName).toUri());
kkih6yb8

kkih6yb82#

问题是许可。上载的文件是由tomcat用户创建的,而我创建的目录是由root用户创建的。目前已绕过目录创建。

相关问题