From 9e4597890b14e2abd315db1983f95b5a708ed08c Mon Sep 17 00:00:00 2001 From: lunaticbum <> Date: Tue, 8 Oct 2024 14:43:49 +0900 Subject: [PATCH] .... --- .../back/lun/controllers/BlogController.kt | 64 ++++++++++++----- .../back/lun/model/ResponceResult.kt | 7 +- .../templates/content/blog/write.html | 69 ++++++++++++------- 3 files changed, 97 insertions(+), 43 deletions(-) diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt index 8582740..240e628 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/controllers/BlogController.kt @@ -2,10 +2,16 @@ package kr.lunaticbum.back.lun.controllers import jakarta.servlet.http.HttpServletRequest import jakarta.servlet.http.HttpServletResponse +import kr.lunaticbum.back.lun.model.FileSaveResult +import kr.lunaticbum.back.lun.model.ResponceResult import kr.lunaticbum.back.lun.utils.LogService import kr.lunaticbum.back.lun.utils.getFileExtension import org.springframework.beans.factory.annotation.Autowired import org.springframework.beans.factory.annotation.Value +import org.springframework.core.io.Resource +import org.springframework.core.io.UrlResource +import org.springframework.http.MediaType +import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.* import org.springframework.web.multipart.MultipartFile import org.springframework.web.servlet.ModelAndView @@ -40,47 +46,63 @@ class BlogController() { private val resourceHandler: String? = null + @ResponseBody + @GetMapping("/post/images/{fileName}") + fun getImage(@PathVariable fileName : String) : Resource { + val imgUploadPath = ("file:" +uploadPath + File.separator + fileName) + return UrlResource.from(imgUploadPath) + } + @PostMapping("/post/imageUpload") - fun postImage(@RequestPart("file") upload: MultipartFile, res: HttpServletResponse, req: HttpServletRequest) { + fun postImage(@RequestPart("file") upload: MultipartFile, res: HttpServletResponse, req: HttpServletRequest) : ResponseEntity { + var lResultCode = 0 + var lResultMsg = "Suscces" var out: OutputStream? = null var printWriter: PrintWriter? = null + var targetFile : File? = null logService.log("imgUploadPath ${upload.originalFilename}") res.characterEncoding = "utf-8" res.contentType = "text/html;charset=utf-8" - + var uuid = UUID.randomUUID() + val extension: String = getFileExtension(upload.originalFilename) ?: "" try { - val uuid = UUID.randomUUID() - logService.log("imgUploadPath ${uuid.toString()}") - val extension: String = getFileExtension(upload.originalFilename) ?: "" + logService.log("imgUploadPath ${uuid.toString()}") val bytes = upload.bytes var f = File(uploadPath) +// logService.log("imgUploadPath ${f.parentFile.parentFile.parentFile.parentFile.absoluteFile}") +// logService.log("imgUploadPath ${f.parentFile.parentFile.parentFile.absoluteFile}") +// logService.log("imgUploadPath ${f.parentFile.parentFile.absoluteFile}") +// logService.log("imgUploadPath ${f.parentFile.absoluteFile}") logService.log("imgUploadPath ${f.exists()}") + logService.log("imgUploadPath ${f.absolutePath}") if (f.exists() == false) f.mkdirs() // 실제 이미지 저장 경로 val imgUploadPath = (uploadPath + File.separator + uuid).toString() + "." + extension logService.log("imgUploadPath $imgUploadPath") - File(imgUploadPath).parentFile.mkdirs() + targetFile = File(imgUploadPath) + if(targetFile.parentFile.exists() == false)targetFile.parentFile.mkdirs() + // 이미지 저장 out = FileOutputStream(imgUploadPath) out.write(bytes) out.flush() // ckEditor 로 전송 - printWriter = res.writer - val callback = req.getParameter("CKEditorFuncNum") - val fileUrl = "/blog/post/image/$uuid.$extension" +// printWriter = res.writer +// val callback = req.getParameter("CKEditorFuncNum") +// val fileUrl = "/blog/post/image/$uuid.$extension" - printWriter.println( - ("") - ) - - printWriter.flush() +// printWriter.println( +// ("") +// ) +// +// printWriter.flush() logService.log("imgUploadPath $imgUploadPath") logService.log("imgUploadPath ${File(imgUploadPath).exists()}") } catch (e: IOException) { @@ -88,11 +110,17 @@ class BlogController() { } finally { try { out?.close() - printWriter?.close() } catch (e: IOException) { e.printStackTrace() } } + val responce = ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(FileSaveResult().apply { + + this.resultCode = lResultCode + this.resultMsg = lResultMsg + this.fileName = "$uuid.$extension" + }) + return responce } } \ No newline at end of file diff --git a/src/main/kotlin/kr/lunaticbum/back/lun/model/ResponceResult.kt b/src/main/kotlin/kr/lunaticbum/back/lun/model/ResponceResult.kt index 8d8d866..3f44e71 100644 --- a/src/main/kotlin/kr/lunaticbum/back/lun/model/ResponceResult.kt +++ b/src/main/kotlin/kr/lunaticbum/back/lun/model/ResponceResult.kt @@ -4,7 +4,12 @@ import lombok.Getter @Getter -class ResponceResult { +open class ResponceResult { var resultCode: Int = 0 var resultMsg: String? = null } + +@Getter +class FileSaveResult : ResponceResult() { + var fileName : String? = null +} \ No newline at end of file diff --git a/src/main/resources/templates/content/blog/write.html b/src/main/resources/templates/content/blog/write.html index d43c656..b39d2d1 100644 --- a/src/main/resources/templates/content/blog/write.html +++ b/src/main/resources/templates/content/blog/write.html @@ -5,6 +5,8 @@ layout:decorate="~{layout/default_layout}"> + + @@ -13,32 +15,50 @@ + Spring Boot @@ -75,12 +91,17 @@ -
-
- -
- +
+ +
+
+ +
+
+ +
+
\ No newline at end of file