回答編集履歴
2
微調整
answer
CHANGED
@@ -99,10 +99,14 @@
|
|
99
99
|
|
100
100
|
@PostMapping("/")
|
101
101
|
public String confirm(@RequestParam("filedata") MultipartFile filedata, Model model) throws IOException {
|
102
|
+
if (filedata.getContentType().startsWith("image/")) {
|
102
|
-
|
103
|
+
model.addAttribute("contentType", filedata.getContentType());
|
104
|
+
} else {
|
105
|
+
model.addAttribute("keiyakuMaster", new KeiyakuMaster());
|
106
|
+
return "index";
|
107
|
+
}
|
103
108
|
model.addAttribute("filename", filedata.getOriginalFilename());
|
104
109
|
model.addAttribute("viewFiledata", Base64.getEncoder().encodeToString(filedata.getBytes()));
|
105
|
-
|
106
110
|
return "index";
|
107
111
|
}
|
108
112
|
|
@@ -133,4 +137,5 @@
|
|
133
137
|
}
|
134
138
|
}
|
135
139
|
|
140
|
+
|
136
141
|
```
|
1
確認画面の追加
answer
CHANGED
@@ -35,13 +35,27 @@
|
|
35
35
|
<title>Image upload</title>
|
36
36
|
</head>
|
37
37
|
<body>
|
38
|
+
<th:block th:if="${#strings.isEmpty(filename)}">
|
38
|
-
|
39
|
+
<form th:action="@{/}" th:object="${keiyakuMaster}" method="post" enctype="multipart/form-data">
|
39
|
-
|
40
|
+
<input type="file" th:field="*{filedata}" accept="image/png, image/jpeg"><br>
|
40
|
-
|
41
|
+
<br>
|
41
|
-
|
42
|
+
<button>アップロードの確認へ</button>
|
42
|
-
|
43
|
+
</form>
|
44
|
+
</th:block>
|
45
|
+
<th:block th:unless="${#strings.isEmpty(filename)}">
|
46
|
+
<p>以下の画像を、アップロードしてもよろしいですか?</p>
|
47
|
+
<form th:action="@{/save}" method="post" enctype="multipart/form-data">
|
48
|
+
<button>画像のアップロード</button><br>
|
49
|
+
<br>
|
50
|
+
<input type="hidden" name="contentType" th:value="${contentType}">
|
51
|
+
<input type="hidden" name="filename" th:value="${filename}">
|
52
|
+
<input type="hidden" name="viewFiledata" th:value="${viewFiledata}">
|
53
|
+
<img th:src="@{'data:' + ${contentType} + ';base64,'+${viewFiledata}}" alt="">
|
54
|
+
</form>
|
55
|
+
</th:block>
|
43
56
|
</body>
|
44
57
|
</html>
|
58
|
+
|
45
59
|
```
|
46
60
|
|
47
61
|
result.html
|
@@ -55,7 +69,7 @@
|
|
55
69
|
<title>Image upload</title>
|
56
70
|
</head>
|
57
71
|
<body>
|
58
|
-
<p>以下の画像が、アップロードされ
|
72
|
+
<p style="color:red">以下の画像が、アップロードされました</p>
|
59
73
|
<img th:if="!${#strings.isEmpty(filedata)}" th:src="@{'data:' + ${contentType} + ';base64,'+${filedata}}" alt="">
|
60
74
|
</body>
|
61
75
|
</html>
|
@@ -84,30 +98,39 @@
|
|
84
98
|
}
|
85
99
|
|
86
100
|
@PostMapping("/")
|
87
|
-
public String
|
101
|
+
public String confirm(@RequestParam("filedata") MultipartFile filedata, Model model) throws IOException {
|
102
|
+
model.addAttribute("contentType", filedata.getContentType());
|
103
|
+
model.addAttribute("filename", filedata.getOriginalFilename());
|
104
|
+
model.addAttribute("viewFiledata", Base64.getEncoder().encodeToString(filedata.getBytes()));
|
105
|
+
|
106
|
+
return "index";
|
107
|
+
}
|
108
|
+
|
109
|
+
@PostMapping("/save")
|
110
|
+
public String save(
|
111
|
+
@RequestParam("contentType") String contentType,
|
112
|
+
@RequestParam("filename") String filename,
|
113
|
+
@RequestParam("viewFiledata") String viewFiledata) throws IOException {
|
114
|
+
|
88
115
|
var keiyakuMaster = new KeiyakuMaster();
|
89
|
-
if (
|
116
|
+
if (contentType.startsWith("image/")) {
|
90
|
-
keiyakuMaster.setContentType(
|
117
|
+
keiyakuMaster.setContentType(contentType);
|
91
118
|
} else {
|
92
119
|
keiyakuMaster.setContentType("");
|
93
120
|
}
|
94
|
-
keiyakuMaster.setFilename(filedata.getOriginalFilename());
|
95
|
-
keiyakuMaster.
|
121
|
+
keiyakuMaster.setFilename(filename);
|
96
|
-
|
122
|
+
keiyakuMaster.setFiledata(Base64.getDecoder().decode(viewFiledata));
|
97
123
|
keiyakuMasterRepository.save(keiyakuMaster);
|
98
|
-
|
99
124
|
return "redirect:/" + keiyakuMaster.getId();
|
100
125
|
}
|
101
126
|
|
102
127
|
@GetMapping("/{id}")
|
103
128
|
public String result(@PathVariable Long id, Model model) {
|
104
129
|
var keiyakuMaster = keiyakuMasterRepository.findById(id).get();
|
105
|
-
|
106
130
|
model.addAttribute("contentType", keiyakuMaster.getContentType());
|
107
|
-
model.addAttribute("filename", keiyakuMaster.getFilename());
|
108
131
|
model.addAttribute("filedata", Base64.getEncoder().encodeToString(keiyakuMaster.getFiledata()));
|
109
|
-
|
110
132
|
return "result";
|
111
133
|
}
|
112
134
|
}
|
135
|
+
|
113
136
|
```
|