简体   繁体   English

无法将文件从Angular客户端上传到Spring Java服务器:说400错误

[英]Unable to upload file from Angular client to Spring Java server: Says 400 error

When I am uploading a file from Postman rest client to the server(spring application deployed on a remote machine), I am able to upload the file without any issue. 当我将文件从Postman rest客户端上传到服务器(部署在远程计算机上的spring应用程序)时,我可以上传文件而没有任何问题。

But when I try to write a rest client in angular.js, and send over the request, I get 400 Bad Request Error. 但是,当我尝试在angular.js中编写一个rest客户端并发送请求时,出现400错误请求错误。 I know it's because of some syntax issue between what is send from client and what server is expecting. 我知道这是由于客户端发送的内容与服务器期望的内容之间存在语法问题。

Server side code: 服务器端代码:

@CrossOrigin(origins = "*") @RequestMapping(value="/upload", method=RequestMethod.POST, consumes = {"multipart/form-data"}) @CrossOrigin(origins =“ *”)@RequestMapping(value =“ / upload”,method = RequestMethod.POST,消耗= {“ multipart / form-data”})

public @ResponseBody String handleFileUpload(@RequestParam("files") MultipartFile file){ . public @ResponseBody字符串handleFileUpload(@RequestParam(“ files”)MultipartFile file){。 . . . } }

Client side code: 客户端代码:

$scope.uploadFiles = function () {
    alert("inside");

    var request = {
        method: 'POST',
        url: 'http://IP ADDRESS and PORT NUMBER/upload',
        data: formdata,
        headers: {
            'Content-Type': undefined
        }
    };


    $http(request)
        .success(function (response) {
            alert("success: "+response);

        })
        .error(function (err) {alert("error: "+err);
        });
}

I have uploaded the code here for file upload using Spring and AngularJS: 我已经在此处上传了使用Spring和AngularJS上传文件的代码:

https://gist.github.com/abdulrafique/9219f7164fdf5dc6dfa8da110be6a04e https://gist.github.com/abdulrafique/9219f7164fdf5dc6dfa8da110be6a04e

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

相关问题 无法使用jersey 2上传文件。错误400 - Unable to upload file with jersey 2. Error 400 使用ajax和spring portlet将文件从客户端上传到服务器端 - Upload a file from client side to server side with ajax and spring portlet 如何处理从Java客户端上传服务器端HTTP文件 - How to handle Server side HTTP file upload from Java client Spring + Angular 无法两次上传同一个文件 - Spring + Angular Unable to upload same file twice Java客户端如何将文件上传到远程HDFS服务器或从远程HDFS服务器下载文件? - How a Java client upload/download a file to/from remote HDFS server? Spring AngularJS上传文件功能-400错误错误的请求 - Spring AngularJS upload file functionality - 400 error Bad request Spring Boot + Angular文件上传无法两次上传同一文件 - Spring Boot + Angular file upload unable to upload same file twice 无法在Spring Java中上传带有特殊字符的文件 - unable to upload file with special character in Spring java 从C客户端到Java服务器的Http Post字符串给出错误400错误请求 - Http Post string from C client to Java server gives error 400 Bad Request 使用Spring和Rest Web服务将文件从服务器发送到Client Java - Send file from Server to Client java with Spring and Rest web service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM