Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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
Java 尝试在SpringRESTAPI中上载文件时获取415不支持的媒体类型_Java_Spring_File Upload - Fatal编程技术网

Java 尝试在SpringRESTAPI中上载文件时获取415不支持的媒体类型

Java 尝试在SpringRESTAPI中上载文件时获取415不支持的媒体类型,java,spring,file-upload,Java,Spring,File Upload,我已经做了一个服务,它接受一个文件并试图存储在本地文件系统中 但是当我试图用一个html文件向这个服务发出请求时 我已经在谷歌上搜索了这个错误,但很多人告诉我这个错误是由于服务器配置错误造成的,所以请告诉我服务器上的任何更改 我的服务器java代码: @RequestMapping(value = "/upload",headers = "Content-Type=multipart/form-data", method = RequestMethod.POST) @ResponseBo

我已经做了一个服务,它接受一个文件并试图存储在本地文件系统中 但是当我试图用一个html文件向这个服务发出请求时

我已经在谷歌上搜索了这个错误,但很多人告诉我这个错误是由于服务器配置错误造成的,所以请告诉我服务器上的任何更改

我的服务器java代码:

@RequestMapping(value = "/upload",headers = "Content-Type=multipart/form-data", method = RequestMethod.POST)
    @ResponseBody
    public String upload(@RequestBody MultipartFile file)
    {
        name = "/path";

        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(name)));
                stream.write(bytes);
                stream.close();
                System.out.println(file);
             //s   return "You successfully uploaded " + name + "!";
            } catch (Exception e) {
                return "You failed to upload " + name + " => " + e.getMessage();
            }
        } else {
            return "You failed to upload " + name + " because the file was empty.";
        }
        }
        return "";
    }
我的html文件:

<html>
<body>
    <form method="POST" enctype="multipart/form-data"
        action="http://localhost:8080/upload">
        File to upload: <input type="file" class = "file" name="file"><br /> <br /> <input type="submit"
            value="Upload"> Press here to upload the file!
    </form>
</body>
</html>

要上载的文件:

按此处上载文件!
我认为@RequestBody上传文件是错误的。我是这样做的:

UserController.java:

@RequestMapping(value = "/upload/avatar/{userId}", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> uploadAvatar(@PathVariable("userId") Long userId, MultipartHttpServletRequest request) throws IOException {
    String fileName = request.getFileNames().next();
    userService.addAvatar(userId, fileName, request.getFile(fileName));
    return new ResponseEntity<>(HttpStatus.OK);
}

文件是我保存上传文件信息的实体

为什么我们不能从@Request Body获取它通常我们获取所有其他类型的数据
@Transactional
public void addAvatar(final Long userId, final String fileName, final MultipartFile avatar) throws IOException {
    byte[] bytes = IOUtils.toByteArray(avatar.getInputStream());
    long size = avatar.getSize();
    File file = new File(bytes, size, avatar.getContentType(), fileName);

    final User user = userRepository.findOne(agencyId);
    user.setAvatar(file);
    userRepository.save(user);
}