通过REST POST通过JavaSpring引导以流形式接收图像
我正在使用SpringBoot用Java创建一个项目。 重点是接收转换为流的图像,我的代码将该图像转换为pdf文件,并将该pdf作为流发送回。 尽管进行了分析,我还是无法通过开头,接收到流 在这里,您将看到我对正在运行的项目的postman调用的一个片段 我的控制器如下所示:通过REST POST通过JavaSpring引导以流形式接收图像,java,rest,spring-boot,Java,Rest,Spring Boot,我正在使用SpringBoot用Java创建一个项目。 重点是接收转换为流的图像,我的代码将该图像转换为pdf文件,并将该pdf作为流发送回。 尽管进行了分析,我还是无法通过开头,接收到流 在这里,您将看到我对正在运行的项目的postman调用的一个片段 我的控制器如下所示: @RestController public class Controller { @PostMapping(value = "/convert/{format}", consumes = "applicatio
@RestController
public class Controller {
@PostMapping(value = "/convert/{format}", consumes = "application/octet-stream", produces = "application/octet-stream")
@ResponseBody
public void convert(RequestEntity<InputStream> entity, HttpServletResponse response, @PathVariable String format, @RequestParam Map<String, String> params) throws IOException {
if ("pdf".equalsIgnoreCase(format)) {
PDFConverter cnv = new PDFConverter();
/*cnv.convert(entity.getBody(), response.getOutputStream(), params);*/
response.setContentType("application/octet-stream");
response.getOutputStream().println("hello binary");
} else {
// handle other formats
throw new IllegalArgumentException("illegal format: " + format);
}
}
}
在这种情况下我应该忽略什么?正如错误消息已经告诉您的那样,您的内容类型无效。您期望的内容类型与发送的内容类型不同。可能是将字符集定义附加到请求中的问题 我认为您正在使用commons fileupload的流式API。如果spring.http.multipart.enabled=true,这将不起作用,因为请求正在预处理中。您是否可以尝试将spring.http.multipart.enabled设置为false,并更改consumes={MediaType.multipart\u FORM\u DATA\u VALUE},我在使用RequestEntity的控制器中找到了解决方案,这导致了错误。在将其更改为HttpServletRequest之后,它工作了
@RestController
public class Controller {
@RequestMapping(value="/convert/{format}", method=RequestMethod.POST)
public @ResponseBody void convert(HttpServletRequest request, HttpServletResponse response, @PathVariable String format, @RequestParam Map<String, String> params) {
try{
if ("pdf".equalsIgnoreCase(format)) {
PDFConverter cnv = new PDFConverter();
response.setContentType("application/pdf");
cnv.convert(request.getInputStream(), response.getOutputStream(), params);
} else {
// handle other formats
throw new IllegalArgumentException("illegal format: " + format);
}
} catch (IllegalArgumentException | IOException e) {
e.printStackTrace();
}
}
}
我不是故意附加字符集的,这是默认设置吗?当我取消选择标题中设置的内容类型时,它会给我以下错误消息:消息:无效mime类型\false\:不包含“/”,为了消除邮差给事情带来的阴影,你能用curl试试你的请求吗?很好!我已经用以下命令尝试过了:curl-F file=@streamFile.stream;type=application/octet stream localhost:8080/convert/pdf但这给了我相同的结果我实际上使用webMethods Integration Server在我的Java项目中调用POST方法,这将通过流发送。问题在于我是否想在postman/curl中模拟这个流,因为localhost与webMethods运行在不同的服务器上。它不会识别我放在二进制字段中的流的内容类型。我在使用RequestEntity的控制器中找到了解决方案,这导致了错误。在将其更改为HttpServletRequest之后,它工作了