teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

1

ああああああああああああああああああ

2022/05/17 11:11

投稿

wassan_nikoniko
wassan_nikoniko

スコア9

title CHANGED
@@ -1,1 +1,1 @@
1
- springboot + mybatis で画像をDBへアップロードする方法
1
+ ああああああああああああああああああ
body CHANGED
@@ -1,202 +1,1 @@
1
- ### 前提・実現したいこと
2
- htmlから受け取った値をjavaに保存して、DBへアップロードしたいです。
3
-
4
- ### 発生している問題・エラーメッセージ
5
- DBのblob型にbase64化された値が入っていてほしいのに、エンコードがうまくいっていない?可能性のある値が出てしまっています。開始文字が0x2fだったりするのでおそらくエンコードに失敗しているのかも...(pngやjpegなどを保存したいのでiVBORや/9j/4といった値が入っているはず?)
6
-
7
- ### 該当のソースコード
8
- コントローラクラス
9
- ```java
10
- @PostMapping(value="/product-registration")
11
- public String postproductregistration(@ModelAttribute ProductInfo productInfo) throws IOException {
12
- System.out.println(productInfo);
13
- productInfo.setProductImg(Base64.encodeBase64(productInfo.getMultiPartFile().getBytes()));//ここでエンコード
14
- System.out.println(productInfo.getProductImg());
15
- productService.insertProductInfo(productInfo);
16
- return "redirect:/product-list";
17
- }
18
- ```
19
- エンティティクラス
20
- ```java
21
- package cooking.entity;
22
-
23
- import java.io.Serializable;
24
- import java.math.BigDecimal;
25
-
26
- import org.springframework.web.multipart.MultipartFile;
27
-
28
- import lombok.Data;
29
- /**
30
- * DBの値を表すクラス エンティティ
31
- * @author 81806
32
- * @version 1.0.0
33
- */
34
- @Data
35
- public class ProductInfo implements Serializable {
36
-
37
- /** シリアルバージョンUID. */
38
- private static final long serialVersionUID = -2921497769927755763L;
39
-
40
- /** 商品ID*/
41
- private int productID;
42
-
43
- /** 商品名 */
44
- private String productName;
45
-
46
- /** ジャンル */
47
- private String genre;
48
-
49
- /** メーカー */
50
- private String maker;
51
-
52
- /** 商品価格 */
53
- private BigDecimal sellingPrice;
54
-
55
- /** 商品概要 */
56
- private String productDetail;
57
-
58
- /** 商品画像 */
59
- private byte[] productImg;
60
-
61
- /** 削除フラグ */
62
- private String DeleteFlg;
63
-
64
- /** 登録日時 */
65
- private String InsertDate;
66
-
67
- /** 更新日時 */
68
- private String UpdateDate;
69
-
70
- /** 商品画像 入力時 */
71
- private MultipartFile multiPartFile;
1
+ ああああああああああああああああああああああああああああああああああああ
72
-
73
- /**商品画像 表示時 */
74
- private String ProductImgOut;
75
- }
76
- ```
77
- 登録画面
78
- ```html
79
- <!DOCTYPE html>
80
- <html xmlns:th="http://www.thymeleaf.org">
81
- <head>
82
- <meta charset="utf-8">
83
- <title></title>
84
- <link rel="stylesheet" href="css/update.css">
85
- </head>
86
- <body>
87
- <h2 class="title1">商品情報管理システム</h2>
88
- <h2 class="title2">商品情報登録</h2>
89
- <label>商品名の入力は必須です</label><br>
90
- <form action = "/product-registration" method="post" th:object="${productInfo}" enctype="multipart/form-data">
91
- <input class="submit" type="submit" name="button">登録</button>
92
- <button type="button" name="button" onclick="location.href='./product-list'">戻る</button>
93
- <div class="inputBlock">
94
- <div class="title">ジャンル:</div>
95
- <div class="input">
96
- <select th:field="*{genre}">
97
- <option th:value="1">1</option>
98
- <option th:value="2">2</option>
99
- <option th:value="3">3</option>
100
- </select>
101
- </div>
102
- </div>
103
- <div class="inputBlock">
104
- <div class="title">メーカー:</div>
105
- <div class="input"><input type="text" size="21" th:field="*{maker}"></div>
106
- </div>
107
- <div class="inputBlock">
108
- <div class="title">商品名:</div>
109
- <div class="input"><input type="text" size="21" th:field="*{productName}"></div>
110
- </div>
111
- <div class="inputBlock">
112
- <div class="title">販売価格:</div>
113
- <div class="input"><input type="text" size="21" th:field="*{sellingPrice}"></div>
114
- </div>
115
- <div class="inputBlock">
116
- <div class="title">商品説明:</div>
117
- <div class="input"><textarea name="name" th:field="*{productDetail}"></textarea></div>
118
- </div>
119
- <div class="inputBlock">
120
- <div class="title">商品画像:</div>
121
- <div class="input"><input type="file" size="21" th:field="*{multiPartFile}"></div>
122
- </div>
123
- </form>
124
- </body>
125
- </html>
126
-
127
- ```
128
- Mapperクラス
129
- ```
130
- <?xml version="1.0" encoding="UTF-8"?>
131
- <!DOCTYPE mapper PUBLIC
132
- "-//mybatis.org//DTD Mapper 3.0//EN"
133
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
134
- <mapper namespace="cooking.repository.ProductMapper">
135
- <select id="getProductInfoList" resultType="cooking.entity.ProductInfo">
136
- SELECT
137
- ProductID,
138
- Genre,
139
- Maker,
140
- ProductName,
141
- SellingPrice,
142
- ProductImg
143
-
144
- FROM
145
- ProductInfo
146
-
147
- WHERE
148
- DeleteFlg = '0'
149
-
150
- ORDER BY
151
- ProductID ASC
152
- </select>
153
-
154
-
155
- <select id="getProductInfoCount" resultType="int">
156
- select count(*) from ProductInfo
157
- </select>
158
-
159
- <insert id="insertProductInfo" parameterType="cooking.entity.ProductInfo">
160
- INSERT INTO ProductInfo (
161
- ProductName,
162
- Genre,
163
- Maker,
164
- SellingPrice,
165
- ProductDetail,
166
- ProductImg,
167
- InsertDate,
168
- UpdateDate)
169
-
170
- VALUES (
171
- #{productName},
172
- #{genre},
173
- #{maker},
174
- #{sellingPrice},
175
- #{productDetail},
176
- #{productImg},
177
- CURRENT_TIMESTAMP(),
178
- CURRENT_TIMESTAMP())
179
- </insert>
180
- </mapper>
181
-
182
- ```
183
- ![イメージ説明](8730801f3b1f799c4816692cfe78f36d.png)
184
-
185
- ###Mysql構成
186
- +---------------+---------------+------+-----+---------+----------------+
187
- | Field | Type | Null | Key | Default | Extra |
188
- +---------------+---------------+------+-----+---------+----------------+
189
- | ProductID | int | NO | PRI | NULL | auto_increment |
190
- | ProductName | varchar(25) | NO | | NULL | |
191
- | Genre | char(2) | NO | | NULL | |
192
- | Maker | varchar(20) | NO | | NULL | |
193
- | SellingPrice | decimal(11,2) | NO | | NULL | |
194
- | ProductDetail | varchar(200) | YES | | NULL | |
195
- | ProductImg | mediumblob | YES | | NULL | |
196
- | DeleteFlg | char(1) | NO | | 0 | |
197
- | InsertDate | timestamp(3) | NO | | NULL | |
198
- | UpdateDate | timestamp(3) | NO | | NULL | |
199
- +---------------+---------------+------+-----+---------+----------------+
200
-
201
- ### 試したこと
202
- getencoderなどjavaのバージョンが違うものでのエンコードも試しましたが、うまくいきませんでした。何卒ご教授いただければ幸いです。