+-
HTTP状态406 –不可接受[使用spring 4.3.x Java 8从后端流式传输大量数据]
我正在使用 Spring MVC 4.3.X, Java 8,Tomcat 7

码:

@Controller
public class StreamRecordsController {

    @RequestMapping(value = "/streamrecords", method = RequestMethod.GET, consumes = MediaType.ALL_VALUE, 
            produces = "application/octet-stream")
    @ResponseBody
    public ResponseEntity<StreamingResponseBody> export() throws FileNotFoundException {
        File file = new File("C:\\Users\\Ankur\\sample.pdf");
        StreamingResponseBody responseBody = outputStream -> {
            Files.copy(file.toPath(), outputStream);
        };
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=generic_file_name.pdf")
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .body(responseBody);
    }
}

例外:

enter image description here

邮递员快照

enter image description here
题:

我在这里想念什么?

最佳答案
406不可接受

由请求标识的资源仅能够生成响应实体,该响应实体具有根据请求中发送的接受标头不可接受的内容特征.

尝试在控制器方法中使用Produces批注:

@Produces({MediaType.APPLICATION_JSON})
点击查看更多相关文章

转载注明原文:HTTP状态406 –不可接受[使用spring 4.3.x Java 8从后端流式传输大量数据] - 乐贴网