質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

Q&A

解決済

1回答

4172閲覧

DBにある画像を画面に出力したい

Yoshi--

総合スコア62

PostgreSQL

PostgreSQLはオープンソースのオブジェクトリレーショナルデータベース管理システムです。 Oracle Databaseで使われるPL/SQLを参考に実装されたビルトイン言語で、Windows、 Mac、Linux、UNIX、MSなどいくつものプラットフォームに対応しています。

Java

Javaは、1995年にサン・マイクロシステムズが開発したプログラミング言語です。表記法はC言語に似ていますが、既存のプログラミング言語の短所を踏まえていちから設計されており、最初からオブジェクト指向性を備えてデザインされています。セキュリティ面が強力であることや、ネットワーク環境での利用に向いていることが特徴です。Javaで作られたソフトウェアは基本的にいかなるプラットフォームでも作動します。

HTML

HTMLとは、ウェブ上の文書を記述・作成するためのマークアップ言語のことです。文章の中に記述することで、文書の論理構造などを設定することができます。ハイパーリンクを設定できるハイパーテキストであり、画像・リスト・表などのデータファイルをリンクする情報に結びつけて情報を整理します。現在あるネットワーク上のほとんどのウェブページはHTMLで作成されています。

Spring Boot

Spring Bootは、Javaのフレームワークの一つ。Springプロジェクトが提供する様々なフレームワークを統合した、アプリケーションを高速で開発するために設計されたフレームワークです。

0グッド

0クリップ

投稿2017/07/14 02:58

編集2017/07/14 03:00

コントローラー

java

1 2 3package com.example.konkatsu.web; 4 5import java.io.IOException; 6import java.util.List; 7 8import org.springframework.beans.BeanUtils; 9import org.springframework.beans.factory.annotation.Autowired; 10import org.springframework.security.core.annotation.AuthenticationPrincipal; 11import org.springframework.stereotype.Controller; 12import org.springframework.ui.Model; 13import org.springframework.validation.BindingResult; 14import org.springframework.validation.annotation.Validated; 15import org.springframework.web.bind.annotation.GetMapping; 16import org.springframework.web.bind.annotation.ModelAttribute; 17import org.springframework.web.bind.annotation.PostMapping; 18import org.springframework.web.bind.annotation.RequestMapping; 19import org.springframework.web.bind.annotation.RequestParam; 20import org.springframework.web.multipart.MultipartFile; 21 22import com.example.konkatsu.domain.Profile; 23import com.example.konkatsu.service.LoginUserDetails; 24import com.example.konkatsu.service.ProfileService; 25 26 27@Controller 28@RequestMapping("konkatsu") //「URL」の接頭辞をkonkatsuに設定(このクラスで呼ばれたpostなどは/konkatsu/(path)となる) 29 30public class KonkatsuController { 31 @Autowired 32 ProfileService profileService; 33 34 @ModelAttribute //@ModelAttributeを付けたメソッド内でProfileFormを初期化 35 //@ModelAttributeがついたメソッドは、@PostMappingでマッピングされたメソッドの前に実行され 36 //「返り値」は自動で「Model」に追加される(この例だと「list」や「create」メソッドが呼ばれる前に「model.addAttribute(new ProfileForm())」相当の処理が行われる) 37 ProfileForm setUpForm() { 38 return new ProfileForm(); 39 } 40 41 42 43 @GetMapping //pathの指定がない場合は URL は/konkatsu 44 public String myPage(@AuthenticationPrincipal LoginUserDetails userDetails) { 45 return "konkatsu/myPage"; 46 } 47 48 49 @GetMapping(path = "profileForm") //pathの指定がない場合は URL は/konkatsu 50 String list(Model model){ //SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 51 List<Profile> profile = profileService.findAll(); 52 model.addAttribute("profile", profile); //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトを指定 53 return "konkatsu/profileForm"; //遷移する画面の名前 54 } 55 56 57 58 //th:action="@{/konkatsu/createProfile}" の処理 59 @PostMapping(path = "createProfile") //URLが /konkatsu/createProfile となる 60 public String create(@Validated ProfileForm form, BindingResult bindingResult, Model model, 61 // 送信されたフォームの入力チェックを行う為に@Validatedアノテーションを付ける。 62 // これによりProfileFormに設定したBean Validationアノテーションが評価され、結果が隣の引数のBindingResultに格納される 63 @AuthenticationPrincipal LoginUserDetails userDetails) throws IOException{ 64 //@AuthenticationPrincipalをつけることでログイン中の[LoginUserDetails]オブジェクトを取得できる 65 66 System.out.println(form.getId()); 67 System.out.println(form.getName()); 68 System.out.println(form.getGenderId()); 69 System.out.println(form.getBirthday()); 70 System.out.println(form.getHeight()); 71 System.out.println(form.getOccupationId()); 72 System.out.println(form.getIncome()); 73 System.out.println(form.getText()); 74 System.out.println(form.getFile()); 75 76 if (bindingResult.hasErrors()){ //入力チェックの結果を確認し、エラーがある場合は一覧画面表示に戻る 77 System.out.println("エラー"); 78 return list(model); 79 } 80 81 82 Profile profile = new Profile(); 83 profile.setId(form.getId()); 84 profile.setName(form.getName()); 85 profile.setGenderId(form.getGenderId()); 86 profile.setBirthday(form.getBirthday()); 87 profile.setHeight(form.getHeight()); 88 profile.setOccupationId(form.getOccupationId()); 89 profile.setIncome(form.getIncome()); 90 profile.setText(form.getText()); 91 MultipartFile uploaded = form.getFile(); 92 byte[] image = uploaded.getBytes(); //アップロードファイルをbyte配列で取得 93 profile.setImage(image); 94 95 BeanUtils.copyProperties(form, profile);//ProfileFormをProfileにコピーする 96 profileService.create(profile, userDetails.getUser()); //ProfileをDBに追加する (ログインユーザー情報も) 97 98 return "/konkatsu/profileConfirm"; 99 100 } 101 102 @GetMapping(path = "manList") //pathの指定がない場合は URL は/konkatsu 103 String manList(@RequestParam Integer id, Model model){ //htmlからidとして送られた値を@RequestParamにて取得SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 104 List<Profile> profile = profileService.findUser(id); 105 model.addAttribute("profile", profile); //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトを指定 106 return "konkatsu/profileList"; //遷移する画面の名前 107 } 108 109 @GetMapping(path = "myProfile") //pathの指定がない場合は URL は/konkatsu 110 String MyList(@RequestParam Integer id, Model model){ //SpringMVCでは画面に値を渡す為にModelオブジェクトを使用 111 Profile profile = profileService.findOne(id); 112 model.addAttribute("profile", profile); //第一引数はThymeleafで取り出す時に使う名前、第二引数はThymeleafに渡したいオブジェクトを指定 113 return "konkatsu/myProfile"; //遷移する画面の名前 114 } 115 116}

html

1<!DOCTYPE html> 2<html xmlns:th="http://www.thymeleaf.org"> 3<head> 4 <meta charset="utf-8"/> 5 <title>婚活サイト</title> 6 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" 7 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap.min.css}"/> 8 <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" 9 th:href="@{/webjars/bootstrap/3.3.7/css/bootstrap-theme.min.css}"/> 10</head> 11 12<body th:with="user=${#authentication.principal.user}"> 13 <!-- authenticationで認証ユーザー情報にアクセスできる 、principalプロパティでUserDetailsオブジェクトにアクセスできるので、user.mailでログインユーザー名が分かる--> 14 15 <h2> 16 <span th:text="${user.mail}">メアド</span> 17 さん、Myプロフィール 18 </h2> 19 20 21 <table class="table table-striped table-bordered table-condensed"> 22 23 <tr> 24 <th>名前</th> 25 <th>性別</th> 26 <th>誕生日</th> 27 <th>身長(cm)</th> 28 <th>職業</th> 29 <th>年収(万円)</th> 30 <th>自己紹介</th> 31 <th>メールアドレス</th> 32 <th>画像</th> 33 </tr> 34 35 36 <tr> 37 38 <td th:text="${profile.name}">山田</td> 39 <td th:text="${profile.gender.gender}">山田</td> 40 <td th:text="${profile.birthday}"></td> 41 <td th:text="${profile.height}"></td> 42 <td th:text="${profile.occupation.occupationName}"></td> 43 <td th:text="${profile.income}"></td> 44 <td th:text="${profile.text}"></td> 45 <td th:text="${profile.user.mail}">duke</td> <!-- 「profile」オブジェクトがもつ「user」の「mail」を表示 --> 46 47---------------------------------- 48 <td> <img th:src="${profile.image}"/></td> 49----------------------------------- 50 </tr> 51 </table> 52 53 54 55 56 <form th:action="@{/konkatsu/manList}" method="get"> 57 <input type="hidden" name="id" th:value="${profile.genderId}" /> 58 <button type="submit" class="btn btn-lg btn-primary btn-block">登録ユーザー一覧を見る</button> 59 </form> 60 61 62 <form th:action="@{/logout}" method="post"> 63 <input type="submit" class="btn btn-lg btn-primary btn-block" value="ログアウト" /> 64 </form> 65 66</body> 67</html>

イメージ説明

MultipartFileで取得した画像を
byte[] image = uploaded.getBytes();
でバイト配列にしDBに保存しました

そのDBにある画像データを画面上に表示したいです

<td> <img th:src="${profile.image}"/></td>

とコントローラーを書き加えると思うのですが

Base64形式にして出力する方法があると思いますが
具体的にどこを直せば良いのでしょうか??

追加した方が良いクラスがありましたら
教えてください!!!!

お願いいたします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

画像ファイルをBASE64形式でエンコードしていないようですし、画像を表示するimgタグもBASE64でエンコードされた画像を出力できるように記述していないので、表示されません。

例えば、

java

1Resource resource = resourceLoader.getResource("classpath:sample.png"); 2byte[] image = FileUtils.readFileToByteArray(resource.getFile()); 3 4String encodedImage = Base64.getEncoder().encodeToString(image); 5 6mnv.addObject("image", encodedImage);

と、BASE64化した画像(対象画像はpng形式を想定)を image の名前でModelAndViewへ格納した場合、

html

1<img src="sample.jpg" th:src="${'data:image/png;base64,' + image}"/>

とすれば表示はされるでしょう。

ただしBASE64形式で表示する画像ファイルについてはレスポンスするHTMLに含まれるため、サイズが数キロバイト程度のものに留めておくべきでしょうか。


参考サイト:
小さい画像をbase64をかけてhtml内に埋め込み
Java 8 より導入された java.util.Base64 の利用デモ。

投稿2017/07/14 04:55

A-pZ

総合スコア12011

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Yoshi--

2017/07/14 06:43

A-pZさんありがとうございます! 参考にさせていただきます。ありがとうございます!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問