Spring Framework的图像文件处理的首选方法是什么?(What is the preferred way of Image file handling for Spring Framewo

编程入门 行业动态 更新时间:2024-10-27 10:30:09
Spring Framework的图像文件处理的首选方法是什么?(What is the preferred way of Image file handling for Spring Framework?)

我有一个项目来创建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

更多推荐

本文发布于:2023-08-01 11:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1357904.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像文件   首选   方法   Framework   Spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!