简体   繁体   English

使用angularjs和spring boot上传文件

[英]uploading files using angularjs and spring boot

I have an angularjs application deployed in a server, and a spring boot application deployed in another server.我在服务器上部署了一个angularjs应用程序,在另一台服务器上部署了一个spring boot应用程序。

in my angularjs application I have a form that uploads a file and send it via a rest controller, this controller then saves this file inside a folder in the server where the spring boot application is deployed.在我的 angularjs 应用程序中,我有一个表单可以上传文件并通过 rest 控制器发送它,然后该控制器将此文件保存在部署 spring boot 应用程序的服务器中的文件夹中。

this is my rest controller :这是我的休息控制器:

@CrossOrigin(origins="*")
@RestController
public class Upload {

    @RequestMapping(value="/imageUpload", method = RequestMethod.POST)
    public void UploadFile(MultipartHttpServletRequest request) throws IOException {

        Iterator<String> itr=request.getFileNames();
        MultipartFile file=request.getFile(itr.next());
        String fileName=file.getOriginalFilename();
        File dir = new File("C:\\file");
        if (dir.isDirectory())
        {
            File serverFile = new File(dir,fileName);
            BufferedOutputStream stream = new BufferedOutputStream(
                    new FileOutputStream(serverFile));
            stream.write(file.getBytes());
            stream.close();
        }else {
            System.out.println("not");
        }

    }
}

and this my angularjs controller which sends the file :这是我发送文件的angularjs控制器:

$scope.validerOffre= function(){
    var file = $scope.fileUpload;
    var uploadUrl = "/imageUpload";
    fileUploadService.uploadFileToUrl(file, uploadUrl);
    };

this controller then call the fileUploadService :这个控制器然后调用 fileUploadService :

capValueRecruitApp.service('fileUploadService', function ($http) {
  this.uploadFileToUrl = function(file, uploadUrl){
    var fd = new FormData();
    fd.append('file', file);
    $http.post('http://localhost:8080' + uploadUrl, fd, {
        transformRequest: angular.identity,
        headers: {'Content-Type': undefined}
      })
      .success(function(){
      })
      .error(function(){
      });
  }
});

and in my angularjs application I want to display the files that I uploaded but I cant do that since the files were uploaded in the spring boot application server, which is another server .在我的 angularjs 应用程序中,我想显示我上传的文件,但我不能这样做,因为这些文件是在 Spring Boot 应用程序服务器中上传的,这是另一台服务器。

so my question is how can I save the files that I uploaded in a folder in the angularjs application server instead of the server where the spring boot application is deployed.所以我的问题是如何将我上传的文件保存在 angularjs 应用程序服务器的文件夹中,而不是部署 Spring Boot 应用程序的服务器中。

You basically have two solutions :您基本上有两种解决方案:

1: you create a new @RequestMapping that takes the filename ( or even better an id returned by your fileUpload controller) and that returns the file. 1:你创建一个新的@RequestMapping ,它接受文件名(或者更好的是你的fileUpload控制器返回的id)并返回文件。 something like this :像这样:

@RequestMapping(value="/imageDownload", method = RequestMethod.POST)
public void downloadFile(@RequestParam(value = "fileName", required = true) String fileName,HttpServletResponse response) throws IOException {

    File dir = new File("C:\\file");
    File fileToDownload = new File(dir,fileName);
    if (dir.isDirectory() && fileToDownload.exists()){
        //read fileToDownload and send stream to to response.getOutputStream();

    }else {
        System.out.println("no such file "+ fileToDownload.toString());
        response.sendError(404);
        return;
    }

}

Then you call http://localhost:8080/imageDownload?filename=yourUploadedFile.jpg然后你调用http://localhost:8080/imageDownload?filename=yourUploadedFile.jpg

2: When you save the file in your upload, save it somewhere where your webserver has direct access. 2:当您在上传文件中保存文件时,请将其保存在您的网络服务器可以直接访问的地方。

If our angularjs files are served by your spring application, you could add a new folder in spring.resources.staticLocations in the application.properties file.如果我们的angularjs的文件是由Spring应用程序服务,您可以在添加新文件夹spring.resources.staticLocations在application.properties文件。 (see this : http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content ) (见: http : //docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-spring-mvc-static-content

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

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