我有一个项目来创建Web服务,接受和操作图像作为使用Spring的响应。
我知道Spring的RESTful API的概念,专门用于XML和JSON响应,使用Jackson库与java对象的绑定,但我正在寻找相同类型的东西,但对于其他内容类型,如图像。
我有以下功能来上传和获取图像,但我不确定需要什么样的@RequestBody对象来绑定像BufferedImage这样的Image POJO,以便将来可以操作它。
// Upload the image from a browser through AJAX with URI "../upload" @RequestMapping(value="/upload", method=RequestMethod.POST, consumes={"image/png", "image/jpeg"}) protected void upload(@RequestBody ???){ // upload the image in a webserver as an Image POJO to make some image manipulation in the future. } // Fetches the image from a webserver through GET request with URI "../fetch/{image}" @RequestMapping(value="/fetch/{image}", method=RequestMethod.GET) protected @ResponseBody String fetch(@PathVariable("image") String imageName){ // fetch image from a webserver as a String with the path of the image location to be display by img html tag. }有了这个,我正在寻找一种更优选的方式来为Spring做图像文件处理,并提供更简洁的解释。
我也读过有关BufferedImageHttpMessageConverter但不太确定它是否对我的应用程序有用。
谢谢!
请让我知道你的想法。
I have a project to create web services that would accept and manipulate images as a response using Spring.
I know the concept of RESTful APIs for Spring specifically for XML and JSON responses, the bindings with the java objects using the Jackson library, but I was looking for the same kind of thing but for other content-types like images.
I have the below functionality to upload and fetch the image but I am not sure on what @RequestBody object is needed to bind Image POJOs like BufferedImage so that I could manipulate it in the future.
// Upload the image from a browser through AJAX with URI "../upload" @RequestMapping(value="/upload", method=RequestMethod.POST, consumes={"image/png", "image/jpeg"}) protected void upload(@RequestBody ???){ // upload the image in a webserver as an Image POJO to make some image manipulation in the future. } // Fetches the image from a webserver through GET request with URI "../fetch/{image}" @RequestMapping(value="/fetch/{image}", method=RequestMethod.GET) protected @ResponseBody String fetch(@PathVariable("image") String imageName){ // fetch image from a webserver as a String with the path of the image location to be display by img html tag. }With this, I was looking for a more preferred way of doing the image file handling for Spring with a more concise explanation.
I have also read about BufferedImageHttpMessageConverter but not quite sure if it is useful to my application.
Thank you!
Please let me know your thoughts.
最满意答案
上传所需的只是通常的上传文件。
@PostMapping("/upload") // //new annotation since 4.3 public String singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {示例中的代码
所以你只需通过POST发送文件“multipart / form-data”
要下载,您应该只写入图像文件字节
@GetMapping(value = "/image") public @ResponseBody byte[] getImage() throws IOException { InputStream in = getClass() .getResourceAsStream("/com/baeldung/produceimage/image.jpg"); return IOUtils.toByteArray(in); }示例中的代码
All you need for uploading is usual upload file.
@PostMapping("/upload") // //new annotation since 4.3 public String singleFileUpload(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {The code from the example
So you just send the file by POST with "multipart/form-data"
For downloading you should just write the image file bytes
@GetMapping(value = "/image") public @ResponseBody byte[] getImage() throws IOException { InputStream in = getClass() .getResourceAsStream("/com/baeldung/produceimage/image.jpg"); return IOUtils.toByteArray(in); }The code from the example
更多推荐
发布评论