質問編集履歴

1

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

2022/05/17 11:11

投稿

wassan_nikoniko
wassan_nikoniko

スコア9

test CHANGED
@@ -1 +1 @@
1
- springboot + mybatis で画像をDBへアップロードする方法
1
+ ああああああああああああああああああ
test CHANGED
@@ -1,403 +1 @@
1
- ### 前提・実現したいこと
2
-
3
- htmlから受け取った値をjavaに保存して、DBへアップロードしたいです。
4
-
5
-
6
-
7
- ### 発生している問題・エラーメッセージ
8
-
9
- DBのblob型にbase64化された値が入っていてほしいのに、エンコードがうまくいっていない?可能性のある値が出てしまっています。開始文字が0x2fだったりするのでおそらくエンコードに失敗しているのかも...(pngやjpegなどを保存したいのでiVBORや/9j/4といった値が入っているはず?)
10
-
11
-
12
-
13
- ### 該当のソースコード
14
-
15
- コントローラクラス
16
-
17
- ```java
18
-
19
- @PostMapping(value="/product-registration")
20
-
21
- public String postproductregistration(@ModelAttribute ProductInfo productInfo) throws IOException {
22
-
23
- System.out.println(productInfo);
24
-
25
- productInfo.setProductImg(Base64.encodeBase64(productInfo.getMultiPartFile().getBytes()));//ここでエンコード
26
-
27
- System.out.println(productInfo.getProductImg());
28
-
29
- productService.insertProductInfo(productInfo);
30
-
31
- return "redirect:/product-list";
32
-
33
- }
34
-
35
- ```
36
-
37
- エンティティクラス
38
-
39
- ```java
40
-
41
- package cooking.entity;
42
-
43
-
44
-
45
- import java.io.Serializable;
46
-
47
- import java.math.BigDecimal;
48
-
49
-
50
-
51
- import org.springframework.web.multipart.MultipartFile;
52
-
53
-
54
-
55
- import lombok.Data;
56
-
57
- /**
58
-
59
- * DBの値を表すクラス エンティティ
60
-
61
- * @author 81806
62
-
63
- * @version 1.0.0
64
-
65
- */
66
-
67
- @Data
68
-
69
- public class ProductInfo implements Serializable {
70
-
71
-
72
-
73
- /** シリアルバージョンUID. */
74
-
75
- private static final long serialVersionUID = -2921497769927755763L;
76
-
77
-
78
-
79
- /** 商品ID*/
80
-
81
- private int productID;
82
-
83
-
84
-
85
- /** 商品名 */
86
-
87
- private String productName;
88
-
89
-
90
-
91
- /** ジャンル */
92
-
93
- private String genre;
94
-
95
-
96
-
97
- /** メーカー */
98
-
99
- private String maker;
100
-
101
-
102
-
103
- /** 商品価格 */
104
-
105
- private BigDecimal sellingPrice;
106
-
107
-
108
-
109
- /** 商品概要 */
110
-
111
- private String productDetail;
112
-
113
-
114
-
115
- /** 商品画像 */
116
-
117
- private byte[] productImg;
118
-
119
-
120
-
121
- /** 削除フラグ */
122
-
123
- private String DeleteFlg;
124
-
125
-
126
-
127
- /** 登録日時 */
128
-
129
- private String InsertDate;
130
-
131
-
132
-
133
- /** 更新日時 */
134
-
135
- private String UpdateDate;
136
-
137
-
138
-
139
- /** 商品画像 入力時 */
140
-
141
- private MultipartFile multiPartFile;
1
+ ああああああああああああああああああああああああああああああああああああ
142
-
143
-
144
-
145
- /**商品画像 表示時 */
146
-
147
- private String ProductImgOut;
148
-
149
- }
150
-
151
- ```
152
-
153
- 登録画面
154
-
155
- ```html
156
-
157
- <!DOCTYPE html>
158
-
159
- <html xmlns:th="http://www.thymeleaf.org">
160
-
161
- <head>
162
-
163
- <meta charset="utf-8">
164
-
165
- <title></title>
166
-
167
- <link rel="stylesheet" href="css/update.css">
168
-
169
- </head>
170
-
171
- <body>
172
-
173
- <h2 class="title1">商品情報管理システム</h2>
174
-
175
- <h2 class="title2">商品情報登録</h2>
176
-
177
- <label>商品名の入力は必須です</label><br>
178
-
179
- <form action = "/product-registration" method="post" th:object="${productInfo}" enctype="multipart/form-data">
180
-
181
- <input class="submit" type="submit" name="button">登録</button>
182
-
183
- <button type="button" name="button" onclick="location.href='./product-list'">戻る</button>
184
-
185
- <div class="inputBlock">
186
-
187
- <div class="title">ジャンル:</div>
188
-
189
- <div class="input">
190
-
191
- <select th:field="*{genre}">
192
-
193
- <option th:value="1">1</option>
194
-
195
- <option th:value="2">2</option>
196
-
197
- <option th:value="3">3</option>
198
-
199
- </select>
200
-
201
- </div>
202
-
203
- </div>
204
-
205
- <div class="inputBlock">
206
-
207
- <div class="title">メーカー:</div>
208
-
209
- <div class="input"><input type="text" size="21" th:field="*{maker}"></div>
210
-
211
- </div>
212
-
213
- <div class="inputBlock">
214
-
215
- <div class="title">商品名:</div>
216
-
217
- <div class="input"><input type="text" size="21" th:field="*{productName}"></div>
218
-
219
- </div>
220
-
221
- <div class="inputBlock">
222
-
223
- <div class="title">販売価格:</div>
224
-
225
- <div class="input"><input type="text" size="21" th:field="*{sellingPrice}"></div>
226
-
227
- </div>
228
-
229
- <div class="inputBlock">
230
-
231
- <div class="title">商品説明:</div>
232
-
233
- <div class="input"><textarea name="name" th:field="*{productDetail}"></textarea></div>
234
-
235
- </div>
236
-
237
- <div class="inputBlock">
238
-
239
- <div class="title">商品画像:</div>
240
-
241
- <div class="input"><input type="file" size="21" th:field="*{multiPartFile}"></div>
242
-
243
- </div>
244
-
245
- </form>
246
-
247
- </body>
248
-
249
- </html>
250
-
251
-
252
-
253
- ```
254
-
255
- Mapperクラス
256
-
257
- ```
258
-
259
- <?xml version="1.0" encoding="UTF-8"?>
260
-
261
- <!DOCTYPE mapper PUBLIC
262
-
263
- "-//mybatis.org//DTD Mapper 3.0//EN"
264
-
265
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
266
-
267
- <mapper namespace="cooking.repository.ProductMapper">
268
-
269
- <select id="getProductInfoList" resultType="cooking.entity.ProductInfo">
270
-
271
- SELECT
272
-
273
- ProductID,
274
-
275
- Genre,
276
-
277
- Maker,
278
-
279
- ProductName,
280
-
281
- SellingPrice,
282
-
283
- ProductImg
284
-
285
-
286
-
287
- FROM
288
-
289
- ProductInfo
290
-
291
-
292
-
293
- WHERE
294
-
295
- DeleteFlg = '0'
296
-
297
-
298
-
299
- ORDER BY
300
-
301
- ProductID ASC
302
-
303
- </select>
304
-
305
-
306
-
307
-
308
-
309
- <select id="getProductInfoCount" resultType="int">
310
-
311
- select count(*) from ProductInfo
312
-
313
- </select>
314
-
315
-
316
-
317
- <insert id="insertProductInfo" parameterType="cooking.entity.ProductInfo">
318
-
319
- INSERT INTO ProductInfo (
320
-
321
- ProductName,
322
-
323
- Genre,
324
-
325
- Maker,
326
-
327
- SellingPrice,
328
-
329
- ProductDetail,
330
-
331
- ProductImg,
332
-
333
- InsertDate,
334
-
335
- UpdateDate)
336
-
337
-
338
-
339
- VALUES (
340
-
341
- #{productName},
342
-
343
- #{genre},
344
-
345
- #{maker},
346
-
347
- #{sellingPrice},
348
-
349
- #{productDetail},
350
-
351
- #{productImg},
352
-
353
- CURRENT_TIMESTAMP(),
354
-
355
- CURRENT_TIMESTAMP())
356
-
357
- </insert>
358
-
359
- </mapper>
360
-
361
-
362
-
363
- ```
364
-
365
- ![イメージ説明](8730801f3b1f799c4816692cfe78f36d.png)
366
-
367
-
368
-
369
- ###Mysql構成
370
-
371
- +---------------+---------------+------+-----+---------+----------------+
372
-
373
- | Field | Type | Null | Key | Default | Extra |
374
-
375
- +---------------+---------------+------+-----+---------+----------------+
376
-
377
- | ProductID | int | NO | PRI | NULL | auto_increment |
378
-
379
- | ProductName | varchar(25) | NO | | NULL | |
380
-
381
- | Genre | char(2) | NO | | NULL | |
382
-
383
- | Maker | varchar(20) | NO | | NULL | |
384
-
385
- | SellingPrice | decimal(11,2) | NO | | NULL | |
386
-
387
- | ProductDetail | varchar(200) | YES | | NULL | |
388
-
389
- | ProductImg | mediumblob | YES | | NULL | |
390
-
391
- | DeleteFlg | char(1) | NO | | 0 | |
392
-
393
- | InsertDate | timestamp(3) | NO | | NULL | |
394
-
395
- | UpdateDate | timestamp(3) | NO | | NULL | |
396
-
397
- +---------------+---------------+------+-----+---------+----------------+
398
-
399
-
400
-
401
- ### 試したこと
402
-
403
- getencoderなどjavaのバージョンが違うものでのエンコードも試しましたが、うまくいきませんでした。何卒ご教授いただければ幸いです。