繁体   English   中英

我无法使用Java Spring Boot和Angular 7在远程服务器(Apache2)上上传文件。它在localhost上运行良好

[英]I can't upload files on remote server (Apache2) with Java Spring Boot and Angular 7. It works well on localhost

我无法在远程服务器(apache2)上传文件(图像)。 我使用java spring boot for back和Angular 7 for front)。 在localhost:4200它运作良好。 在我从chrome浏览器控制台获得的远程服务器上:

POST http://www.xxx.tech:8081/images/upload 400

错误:“{”timestamp“:”2019-05-10T09:39:38.162 + 0000“,”status“:400,”错误“:”错误请求“,”消息“:”找不到文件“,”路径“ : “/图片/上传”}”
headers:HttpHeaders {normalizedNames:Map(0),lazyUpdate:null,lazyInit:ƒ}
消息:“ http://www.xxx.tech:8081/images/upload:400 OK的Http失败响应”
名称:“HttpErrorResponse”
好的:假的
状态:400
statusText:“确定”
网址:“ http://www.xxx.tech:8081/images/upload

目录文件夹已存在于VPS服务器上。 如何使它工作? 在我的controller.java中,我试图替换

File tmp = new File("../front/assets/img/").getCanonicalFile();

File tmp = new File("./front/assets/img/").getCanonicalFile();

File tmp = new File("/home/alexandra/www/front/assets/img/").getCanonicalFile();

但它仍然显示相同的错误消息

JAVA:ImageController.java

@PostMapping(value="images/upload")
    public String uploadImage( @RequestParam("file") MultipartFile transferedFile) throws Exception{
        try {
            //FOR LOCALHOST (works)
            //File tmp = new File("../FRONT- 
            //Alexandra/src/assets/img/").getCanonicalFile();

            //FOR REMOTE SERVER (don't work)
            File tmp = new File("../front/assets/img/").getCanonicalFile();

            String destination = tmp.getPath() + "/" + transferedFile.getOriginalFilename();

            File data = new File(destination);
            transferedFile.transferTo(data);
            Image image = new Image(transferedFile.getOriginalFilename(), destination);
            imageRepository.save(image);
            return destination;

            }catch( Exception param_exception) { 
                throw new ResponseStatusException(
                    HttpStatus.BAD_REQUEST,
                    "No file found");
            }
    }

Angular:mycomponent.component.ts

public apiUrl: string = environment.ApiUrl;
...

public uploadImaeg(): void {

      this.imgesUploaded.map(image => {

         if (image != null && image != undefined) {
            this.imagesService.addImage(image).subscribe();
         }  
      })     
   }

images.service.ts

public addImage(param_file: File): Observable<Object> {
        const headers: HttpHeaders = new HttpHeaders();
        const data: FormData = new FormData();

        data.append("file", param_file, param_file.name);
        headers.append("Content-Type", "multipart/form-data");
        const Obs: Observable<boolean> = this.serviceHttp.post(
            this.apiUrl + "images/upload", data, { headers: headers}
        ).pipe(
            map(
                (param_response: boolean) => {
                    return param_response;
                }
            )
        );
        return Obs;
    }

environment.prod.ts

export const environment = {
  production: true, 
  ApiUrl: 'http://'+document.location.hostname +':8081/'
};

我没有看到你实际上创建了一个存储目录。 您可能希望添加显式目录创建,因为您的bean初始化方法类似于:

private final Path rootLocation = Paths.get("upload");
@PostConstruct
public void init() {
  try {
    Files.createDirectories(rootLocation);
  }
  catch (IOException e) {
     throw new StorageException("Could not initialize storage", e);
  }

}

@AlexGera你让我明白了问题所在:

我尝试了这样的代码:

private final Path rootLocation = Paths.get("images/upload");
    @PostConstruct
    public void init() {
      try {
        Files.createDirectories(rootLocation);
      }
      catch (IOException e) {
         throw new RuntimeException("Could not initialize storage", e);
      }
    }

    @PostMapping(value="images/upload")
    public String uploadImage( @RequestParam("file") MultipartFile transferedFile) throws Exception{
        try {

            File tmp = new File("/home/alexandra/www/front/assets/img/").getCanonicalFile();

            String destination = tmp.getPath() + "/" + transferedFile.getOriginalFilename();

            File data = new File(destination);
            transferedFile.transferTo(data);
            Image image = new Image(transferedFile.getOriginalFilename(), destination);
            imageRepository.save(image);
            return destination;

            }catch( Exception param_exception) { 
                throw new ResponseStatusException(
                    HttpStatus.BAD_REQUEST,
                    "No file found");
            }
    }

它没有解决问题,但它在后备文件夹中创建了一个文件夹:image / upload。 并且,此文件夹的权限不是root root,就像它位于filezilla服务器目录中的所有文件夹中一样。 这是亚历山德拉亚历山德拉

所以,我更改了文件夹资产和文件夹img的权限(也许只有img,最后一个文件夹,应该可以工作,我不知道),因为我的路径是

/home/alexandra/www/front/assets/img/

我这样做了:

cd /home/alexandra/www/front
sudo chown alexandra:alexandra assets
cd assets
sudo chown alexandra:alexandra img
sudo service myserver stop
sudo service myserver start
sudo systemctl restart apache2

它奏效了

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM