diff --git a/src/main/java/com/example/demo/controller/UserProfileController.java b/src/main/java/com/example/demo/controller/UserProfileController.java index 3497a02..1d2830a 100644 --- a/src/main/java/com/example/demo/controller/UserProfileController.java +++ b/src/main/java/com/example/demo/controller/UserProfileController.java @@ -123,9 +123,69 @@ public class UserProfileController { } @PutMapping("/user/{id}") - public void putUserProfile(@PathVariable String id, @RequestParam String name, - @RequestParam String phone, @RequestParam String address, @RequestParam String filepath) { - mapper.updateUserProfile(id, name, phone, address, filepath); + public void putUserProfile(@PathVariable("id") String id, @RequestParam("name") String name, + @RequestParam("phone") String phone, @RequestParam("address") String address, + @RequestPart MultipartFile upfile) { + + String uuid = UUID.randomUUID().toString(); + String fileName = upfile.getOriginalFilename(); + String fileExtName = fileName.substring(fileName.lastIndexOf("."), fileName.length()); + String contentType = upfile.getContentType(); + long fileLength = upfile.getSize(); + + System.out.println("fileLength : " + fileLength); + System.out.println("fileName : " + fileName); + System.out.println("fileExtName : " + fileExtName); + System.out.println("contentType : " + contentType); + + byte[] data = null; + try { + data = upfile.getBytes(); + } catch (IOException e) { + e.printStackTrace(); + } + + String baseFolder = SERVER_URL_IMG; + String baseMiddleFolder = "/img"; + String saveFolder = baseFolder + baseMiddleFolder; + String dbSavePath = baseMiddleFolder + "/" + uuid + fileExtName; + String saveFullFilePath = saveFolder + "/" + uuid + fileExtName; + + // 입력된 내용 파일로 쓰기 + File folder = new File(saveFolder); + // 해당 디렉토리가 없을경우 디렉토리를 생성합니다. + if (!folder.exists()) { + try { + folder.mkdir(); // 폴더 생성합니다. + System.out.println(saveFolder + " folder create OK"); + } catch (Exception e) { + e.getStackTrace(); + } + } else { + System.out.println("A folder has already been created"); + } + + File file = new File(saveFullFilePath); + System.out.println("saved file : " + saveFullFilePath); + FileOutputStream fos = null; + + try { + fos = new FileOutputStream(file); + fos.write(data); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + if (fos != null) + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + mapper.updateUserProfile(id, name, phone, address, dbSavePath); } @DeleteMapping("/user/{id}") diff --git a/src/main/webapp/WEB-INF/views/add.jsp b/src/main/webapp/WEB-INF/views/add.jsp index b715f57..8d66bf1 100644 --- a/src/main/webapp/WEB-INF/views/add.jsp +++ b/src/main/webapp/WEB-INF/views/add.jsp @@ -17,24 +17,19 @@ formData.append("name", $('#name').val()); formData.append("phone", $('#phone').val()); formData.append("address", $('#address').val()); - formData.append("upfile", $("input[name='upfile']")[0].files[0]); - /* formData.append("upfile", $('#upfile').files[0]); */ + formData.append("upfile", $("input[name='upfile']")[0].files[0]); for (var pair of formData.entries()) { console.log(pair[0]+ ', ' + pair[1]); - } - - + } $.ajax({ type: "POST", - url: '/profile/user/10', - /* data: $('#theForm').serialize(), */ + url: '/profile/user/10', data: formData, contentType: false, processData: false, - success: function(data){ - //location.href="listUser.do"; + success: function(data){ alert("신규등록 OK"); }, error: function(error, status, msg) { @@ -42,6 +37,35 @@ } }); }); + + // + $('#edit').on('click', function() { + + var formData = new FormData(); + formData.append("name", $('#ename').val()); + formData.append("phone", $('#ephone').val()); + formData.append("address", $('#eaddress').val()); + formData.append("upfile", $("input[name='eupfile']")[0].files[0]); + + for (var pair of formData.entries()) { + console.log(pair[0]+ ', ' + pair[1]); + } + $.ajax({ + type: "PUT", + url: '/profile/user/1', + data: formData, + contentType: false, + processData: false, + + success: function(data){ + alert("수정등록 OK"); + }, + error: function(error, status, msg) { + alert("Error " + msg); + } + }); + }); + }); @@ -68,11 +92,18 @@ --> 제품명
- 가격
- 별
- 이미지 - + 가격
+ 별
+ 이미지 + +





+ + 제품명
+ 가격
+ 별
+ 이미지 + - + \ No newline at end of file