2017-06-29 9 views
0

私は、Springを使ってイメージを受け入れて操作するWebサービスを作成するプロジェクトを用意しています。Spring Frameworkの画像ファイル処理の好ましい方法は何ですか?

私はSpring向けのXMLとJSONレスポンスのためのRESTful APIのコンセプトを知っています.JavaScriptライブラリを使ったJavaオブジェクトとのバインディングですが、私は同じ種類のものを探していましたが、画像のような他のコンテンツタイプを探していました。

画像をアップロードして取得するための以下の機能がありますが、将来私はそれを操作できるようにBufferedImageのようなImage POJOをバインドするために@RequestBodyオブジェクトが必要であるかどうかはわかりません。

// 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を読んだことがありますが、それが私のアプリケーションに役立つかどうかは不明です。

ありがとうございました!

あなたの考えを教えてください。

答えて

1

アップロードに必要なのは、通常のアップロードファイルです。

@PostMapping("/upload") // //new annotation since 4.3 
public String singleFileUpload(@RequestParam("file") MultipartFile file, 
           RedirectAttributes redirectAttributes) { 

the example

からコードので、あなただけのあなただけの

@GetMapping(value = "/image") 
public @ResponseBody byte[] getImage() throws IOException { 
    InputStream in = getClass() 
     .getResourceAsStream("/com/baeldung/produceimage/image.jpg"); 
    return IOUtils.toByteArray(in); 
} 
画像ファイルのバイトを書き込む必要がありますダウンロードについては、「マルチパート/フォームのデータ」とPOSTによって

をファイルを送信

コードのthe example

+1

@RequestMapping(value e = "/ upload"、method = RequestMethod.POST)の代わりに@PostMappingを使用しますか? – bpunzalan

+0

私はそう思っていますが、試してみることができます。私はあなたのconfigs、春のバージョンなどについては何も知らない。 – StanislavL

+0

ありがとう:)私はこれをチェックしようとします – bpunzalan

関連する問題