Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/450.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何解决通过REST服务上传文件时出现的400()错误_Javascript_Java_Rest_Spring Boot_File Upload - Fatal编程技术网

Javascript 如何解决通过REST服务上传文件时出现的400()错误

Javascript 如何解决通过REST服务上传文件时出现的400()错误,javascript,java,rest,spring-boot,file-upload,Javascript,Java,Rest,Spring Boot,File Upload,我有一个程序(SpringBoot),它使用REST服务将文件上传到服务器或任何其他给定位置。但是当我使用下面相同的服务时,发生了错误,下面就是问题所在 通过REST服务上传文件时,我遇到了400()错误,没有任何说明。这个应用程序是SpringBoot应用程序,它使用java脚本前端通过实现的rest服务上传和下载文件 帮助解决这个问题。谢谢你的帮助。谢谢 错误是: 代码如下: JS: document.getElementById('import2')。onclick=function(){

我有一个程序(SpringBoot),它使用REST服务将文件上传到服务器或任何其他给定位置。但是当我使用下面相同的服务时,发生了错误,下面就是问题所在

通过REST服务上传文件时,我遇到了400()错误,没有任何说明。这个应用程序是SpringBoot应用程序,它使用java脚本前端通过实现的rest服务上传和下载文件

帮助解决这个问题。谢谢你的帮助。谢谢

错误是:

代码如下: JS:

document.getElementById('import2')。onclick=function(){
var files=document.getElementById('selectFiles').files;
console.log(文件);

if(files.length我的假设:您的方法
saveUploadedFiles
引发IOException

当您收到400个错误的请求作为响应时,我认为您应该调试您的

}捕获(IOE异常){
e、 printStackTrace();
返回新的响应属性(HttpStatus.BAD_请求);
}


块以找出引起IOException的原因。

我的假设:您的方法
saveUploadedFiles
抛出IOException

当您收到400个错误的请求作为响应时,我认为您应该调试您的

}捕获(IOE异常){
e、 printStackTrace();
返回新的响应属性(HttpStatus.BAD_请求);
}


块以找出IOException的原因。

我在本地环境中与邮递员创建了相同的场景,如下所示;

服务器端有一个小改动(@RequestMapping(value=“/upload”,method=RequestMethod.POST))

@RequestMapping(value=“/upload”,method=RequestMethod.POST)
公共响应性上载文件(@RequestParam(“数据”)多部分文件上载文件){
debug(“单文件上传!”);
if(uploadfile!=null&&uploadfile.isEmpty()){
返回新的响应属性(“请选择一个文件!”,HttpStatus.OK);
}
返回新的响应属性(“已成功上载-”+uploadfile.getOriginalFilename(),new HttpHeaders(),HttpStatus.OK);
}

它是有效的。

我在本地环境中与邮递员创建了相同的场景,如下所示;

服务器端有一个小改动(@RequestMapping(value=“/upload”,method=RequestMethod.POST))

@RequestMapping(value=“/upload”,method=RequestMethod.POST)
公共响应性上载文件(@RequestParam(“数据”)多部分文件上载文件){
debug(“单文件上传!”);
if(uploadfile!=null&&uploadfile.isEmpty()){
返回新的响应属性(“请选择一个文件!”,HttpStatus.OK);
}
返回新的响应属性(“已成功上载-”+uploadfile.getOriginalFilename(),new HttpHeaders(),HttpStatus.OK);
}

它可以工作。

我已经调试并尝试了,请求没有到达方法偶数。然后在这里检查这个答案:我已经调试并尝试了,请求没有到达方法偶数。然后在这里检查这个答案:当使用postman rest客户端时,你是正确的,但是你尝试过ajax调用吗?好的,检查这个:当使用postman rest客户端时,你是正确的,但是你有你试过ajax调用吗?好的,检查一下:
document.getElementById('import2').onclick = function () {

var files = document.getElementById('selectFiles').files;
console.log(files);
if (files.length <= 0) {
    return false;
}

//serverUploaded = true;

var form_data = new FormData(files.item(0));
//from new NLP
$.ajax({
    type: "POST",
    url: "http://localhost:8080/gsta/upload",
    processData: false,
    contentType: false,
    async: false,
    cache: false,
    data: form_data,
    success: function (result) {
        //alert(JSON.stringify(result));
        //$("#out_lexalytics").html(JSON.stringify(result));
        if (result) {
            if(result.statusCode == 200)
            {
                serverUploaded = true;
            }
        }
    }
});
@PostMapping("/upload")
// If not @RestController, uncomment this
@ResponseBody
public ResponseEntity<?> uploadFile(@RequestParam("data") MultipartFile uploadfile) {

    logger.debug("Single file upload!");

    if (uploadfile != null && uploadfile.isEmpty()) {
        return new ResponseEntity("please select a file!", HttpStatus.OK);
    }

    try {

        saveUploadedFiles(Arrays.asList(uploadfile));

    } catch (IOException e) {
        e.printStackTrace();
        return new ResponseEntity<>(HttpStatus.BAD_REQUEST);
    }

    return new ResponseEntity("Successfully uploaded - " + uploadfile.getOriginalFilename(), new HttpHeaders(), HttpStatus.OK);

}
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public ResponseEntity<?> uploadFile(@RequestParam("data") MultipartFile uploadfile) {
    logger.debug("Single file upload!");
    if (uploadfile != null && uploadfile.isEmpty()) {
        return new ResponseEntity("please select a file!", HttpStatus.OK);
    }
    return new ResponseEntity("Successfully uploaded - " + uploadfile.getOriginalFilename(), new HttpHeaders(), HttpStatus.OK);
}