1. summernote ์๋ํฐ ํ์ผ ๋ชจ์ ํด๋ฆญ
2. ์ฝ๋ฐฑ ํจ์ ํธ์ถ
์ฌ๋จธ ๋ ธํธ๋ callbacksํจ์๋ฅผ ์ง์ํ๋๋ฐ 'onImageUpload'ํจ์๋ '์ด๋ฏธ์ง๋ฅผ ์ ๋ก๋ํ์ ๋' ๋์ํ๋ ํจ์์ด๋ค.
ํ์ผ ์ฒจ๋ถ์์ ๋ค์ค ์ ํ ํ ์ ๋ก๋ํ ๋๋ฅผ ์ํด for๋ฌธ์ผ๋ก ์ฒ๋ฆฌํ๋ค.
uploadSummernoteImageFile ์๋ฐ์คํฌ๋ฆฝํธ ํจ์๋ฅผ ํตํด ajax๋ก ์๋ฒ์์ ํ์ผ ์ ๋ก๋๋ฅผ ์งํํ๋ค.
// summernote ๋ถ๋ถ
function textEdit(){
jsonArray = [];
$('#summernote').summernote({
height: 500, // ์๋ํฐ ๋์ด
minHeight: null, // ์ต์ ๋์ด
maxHeight: null, // ์ต๋ ๋์ด
focus: true, // ์๋ํฐ ๋ก๋ฉํ ํฌ์ปค์ค๋ฅผ ๋ง์ถ์ง ์ฌ๋ถ
lang: "ko-KR", // ํ๊ธ ์ค์
toolbar: [
// [groupName, [list of button]]
['fontname', ['fontname']],
['fontsize', ['fontsize']],
['style', ['bold', 'italic', 'underline','strikethrough', 'clear']],
['color', ['forecolor','color']],
['table', ['table']],
['para', ['ul', 'ol', 'paragraph']],
['height', ['height']],
['insert',['picture','link','video']],
['view', ['fullscreen', 'help']]
],
fontNames: ['Arial', 'Arial Black', 'Comic Sans MS', 'Courier New','๋ง์ ๊ณ ๋','๊ถ์','๊ตด๋ฆผ์ฒด','๊ตด๋ฆผ','๋์์ฒด','๋ฐํ์ฒด'],
fontSizes: ['8','9','10','11','12','14','16','18','20','22','24','28','30','36','50','72'],
callbacks: {
onImageUpload : function(files, editor, welEditable){
// ํ์ผ ์
๋ก๋(๋ค์ค์
๋ก๋๋ฅผ ์ํด ๋ฐ๋ณต๋ฌธ ์ฌ์ฉ)
for (var i = files.length - 1; i >= 0; i--) {
uploadSummernoteImageFile(files[i],
this);
}
}
}
});
$('#summernote').summernote('fontSize', '24');
function uploadSummernoteImageFile(file, el) {
var data = new FormData();
data.append("file",file);
$.ajax({
url: '/../summer_image.do',
type: "POST",
enctype: 'multipart/form-data',
data: data,
cache: false,
contentType : false,
processData : false,
success : function(data) {
var json = JSON.parse(data);
$(el).summernote('editor.insertImage',json["url"]);
jsonArray.push(json["url"]);
jsonFn(jsonArray);
},
error : function(e) {
console.log(e);
}
});
}
},
function jsonFn(jsonArray){
console.log(jsonArray);
},
3.pom.xml์ถ๊ฐ // globals.properties ์์ฑ
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
<!-- json ๋ณํ -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
// ์์๋ก ๋ง๋๋ ํด๋
Globals.tempDir = C:/temp/
// ์์ํด๋์์ copyํด์ ๋ฃ๋ ํด๋
Globals.copyDir = C:/summer/copy/
4. ajax urlํธ์ถ
summer_image.do (ํ์ผ ์ ๋ก๋-์ธ๋ถ ๊ฒฝ๋ก)
@RequestMapping(value="/summer_image.do", produces = "application/json; charset=utf8")
@ResponseBody
public String uploadSummernoteImageFile(@RequestParam("file") MultipartFile multipartFile, HttpServletRequest request ) throws IOException {
JsonObject json = new JsonObject();
String fileRoot = EgovProperties.getProperty("Globals.tempDir");
String originalFileName = multipartFile.getOriginalFilename(); //์ค๋ฆฌ์ง๋ ํ์ผ๋ช
String extension = originalFileName.substring(originalFileName.lastIndexOf(".")); //ํ์ผ ํ์ฅ์
String savedFileName = UUID.randomUUID() + extension; //์ ์ฅ๋ ํ์ผ ๋ช
File targetFile = new File(fileRoot + savedFileName);
try {
// ํ์ผ ์ ์ฅ
InputStream fileStream = multipartFile.getInputStream();
FileUtils.copyInputStreamToFile(fileStream, targetFile);
// ํ์ผ์ ์ด๊ธฐ์ํ์ฌ common/getImg.do ํธ์ถ / ํ๋ผ๋ฏธํฐ๋ก savedFileName ๋ณด๋.
json.addProperty("url", "common/getImg.do?savedFileName="+savedFileName);
json.addProperty("responseCode", "success");
} catch (IOException e) {
FileUtils.deleteQuietly(targetFile);
json.addProperty("responseCode", "error");
e.printStackTrace();
}
String jsonvalue = json.toString();
return jsonvalue;
}
5. ์ธ๋ถ ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๋ ์์ธํ ๋ฐฉ๋ฒ์ ์๋ ์ฐธ๊ณ
6. tempํ์ผ์ ์ ๋ก๋ url ๋ฆฌํด (์ด๋ฏธ์ง ๋ฏธ๋ฆฌ๋ณด๊ธฐ ๋๋)
(summer_image.do -> json.addProperty("url",....);
json.addProperty("url", "common/getImg.do?savedFileName="+savedFileName);
@RequestMapping(value="/common/getImg.do" , method=RequestMethod.GET)
public void getImg(@RequestParam(value="savedFileName") String savedFileName, HttpServletResponse response) throws Exception{
String filePath;
String DIR = EgovProperties.getProperty("Globals.tempDir");
filePath = DIR +savedFileName;
fileutils.getImage(filePath, response);
}
@RequestMapping(value="/common/getImgCopy.do" , method=RequestMethod.GET)
public void getImgCopy(@RequestParam(value="savedFileName") String savedFileName, HttpServletResponse response) throws Exception{
String filePath;
String DIR = EgovProperties.getProperty("Globals.copyDir");
filePath = DIR +savedFileName;
fileutils.getImage(filePath, response);
}
7. url๋ฆฌํด์ด ์ฑ๊ณต
=> summernoteํ๋ฉด์ ์ถ๊ฐํ ์ด๋ฏธ์ง๊ฐ ์๋ํฐ์ ๋ณด์ฌ์ง๋ค. = temp ํด๋์ ์ด๋ฏธ ์ ์ฅ ๋์๋ค๋ ์๋ฏธ.
๊ฐ๋ฐ์ ๋ชจ๋ ํค๊ณ ์ถ๊ฐ๋ ์ด๋ฏธ์ง๋ฅผ ํด๋ฆญํ๋ฉด src์
<img src="common/getImg.do?savedFileName=bc395afe-2324-438d-ae68-1a0a75d0a431.png" style="width: 1920px;">
์ด๋ฐ์์ผ๋ก ์ฝ์ .
์ด์ ์ ๋ชฉ๊ณผ + summernote์๋ํฐ์ ์ฌ์ง๊น์ง,๋ด์ฉ๊น์ง ์ถ๊ฐ ํ ๊ฒฝ์ฐ tempํด๋๊ฐ ์๋ copyํด๋์ ์ ์ฅ์ ํด์ผํ๋ค. (๋ณต์ฌ๊ฐ๋ )
8. ์ ์ฅ ๋ฒํผ ํด๋ฆญ ์ controllerํธ์ถ
1) ์๋ํฐ์ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐ๋ง ํด๋ ์ธ๋ถ์ ์ ์ฅ์ด ๋๋ค.. temp์ ๊ณ์ ์์ด๋๊ฒ..
2) ์๋ํฐ์ ์ด๋ฏธ์ง๋ฅผ ์ถ๊ฐํ ๋ ๊ฐ์ ธ์จ ํ์ผ ๋ฆฌ์คํธ์์ common/getImg.do?savedFileName= ๋ ์ ๊ฑฐํ๊ณ
ํ์ผ ์ด๋ฆ๊ฐ๋ง ๊ฐ์ ธ์จ๋ค.
3) ์ ์ฅ ๋ฒํผ ์ด๋ฒคํธ์ formData๋ฅผ ์ ์ธํ๊ณ append ์ํจ๋ค.
for(var i = 0; i<jsonArray.length; i++){
var str = jsonArray[i];
// str์ ๊ฐ : common/getImg.do?savedFileName=bc395afe-2324-438d-ae68-1a0a75d0a431.png
// '='๋ฅผ ๊ธฐ์ค์ผ๋ก ์๋ฅธ๋ค.
var result = str.toString().split('=');
formData.append('file[]',result[1]);
// result[1] : bc395afe-2324-438d-ae68-1a0a75d0a431.png
}
9. boardWriteSummerCopy ๋ฉ์๋ ํธ์ถ (tempํด๋์ ์๋ ํ์ผ์ copy ํด๋๋ก ๋ณต์ฌ)
1) @RequestParam(value="file[]") List<String> summerfile๋ก ๋ฐ์์ค๊ณ
2) DB์๋ htmlํ๊ทธ๋ก ๋ค์ด๊ฐ๊ธฐ ๋๋ฌธ์ .replaceAll("getImg","getImgCopy");์ ํตํด ์นํ
@RequestMapping(value = "/๋ฑ๋ก.do")
public Map<String, Object> ๋ฑ๋ก(MultipartHttpServletRequest multipartRequest, HttpServletResponse response,
@RequestParam Map<String,String> boardVO, @SessionAttribute("LoginResultVO") LoginVO loginVO,
@RequestParam(value="file[]") List<String> summerfile) throws Exception {
summerCopy(summerfile);
// db์ ๋ค์ด๊ฐ์๋ editordata ๊ฐ
//<p><span style="font-size: 24px;">d</span><img src="common/getImg.do?savedFileName=bc395afe-2324-438d-ae68-1a0a75d0a431.png" style="width: 1903px;"><br></p>
String editordata = boardVO.get("editordata").replaceAll("getImg","getImgCopy");
//replaceAll์ ํตํด getImg๋ฅผ -> getImgCopy๋ก ๋ค ๋ณ๊ฒฝํ๋ค.
// ๊ฒฐ๊ณผ
//<p><span style="font-size: 24px;">d</span><img src="common/getImgCopy.do?savedFileName=bc395afe-2324-438d-ae68-1a0a75d0a431.png" style="width: 1903px;"><br></p>
summerCopy
public Map<String, Object> summerCopy(
@RequestParam(value="file[]") List<String> fileList) throws Exception {
Map<String, Object> result = new HashMap<String, Object>();
//์๋ณธ ํ์ผ๊ฒฝ๋ก
for(int i=0;i<fileList.size();i++){
String oriFilePath = EgovProperties.getProperty("Globals.tempDir")+fileList.get(i);
logger.debug("oriFilePath: {}", oriFilePath);
//๋ณต์ฌ๋ ํ์ผ๊ฒฝ๋ก
String copyFilePath = EgovProperties.getProperty("Globals.copyDir")+fileList.get(i);
logger.debug("copyFilePath: {}", copyFilePath);
try {
FileInputStream fis = new FileInputStream(oriFilePath); //์ฝ์ํ์ผ
FileOutputStream fos = new FileOutputStream(copyFilePath); //๋ณต์ฌํ ํ์ผ
int data = 0;
while((data=fis.read())!=-1) {
fos.write(data);
}
fis.close();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
result.put("SUCCESS", true);
logger.debug("result: {}", result);
return result;
}
4) DB ์ด์ด์ ์นํํ ์ด๋ฆ์ผ๋ก ์ ๋ค์ด๊ฐ๋์ง ํ์ธ
<p><span style="font-size: 24px;">d</span><img src="common/getImgCopy.do?savedFileName=bc395afe-2324-438d-ae68-1a0a75d0a431.png" style="width: 1903px;"><br></p>
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
POI monitorjbl xlsx-streamer ๋ฉ๋ชจ๋ฆฌ ์ด์ ํด๊ฒฐ (2) | 2024.06.07 |
---|---|
POI SAX XSSFReader ๋์ฉ๋ ์์ ํ์ผ ์ฝ๊ธฐ OOME(Out of Memory Error) ๋ฐฉ์ง (0) | 2024.05.23 |
@RequestParam ๊ฐ์ด Null / ์์ธ ์ฒ๋ฆฌ (required=false) (0) | 2022.02.16 |
[Spring] WebSocket sockJS ์ค์๊ฐ ์๋ฆผ ๊ตฌํํ๊ธฐ (3) (1) | 2022.01.10 |
[Spring] ํ์ผ ๋ค์ด๋ก๋ ๊ตฌํ (0) | 2021.12.15 |