이미지 업로드 관련

This commit is contained in:
Kana
2024-01-17 12:12:13 +09:00
parent d51668f3ec
commit 51d58cac5d
5 changed files with 123 additions and 12 deletions

View File

@@ -0,0 +1,69 @@
package com.no1.wms.mypage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import com.no1.wms.excel.EgovWebUtil;
@Service
public class ImgService {
@Autowired
private ServletContext servletContext;
public void imgFileUplode(HttpServletRequest request, MultipartFile imageFile, String fileName) {
String storePathString = "";
try {
storePathString = servletContext.getRealPath("/img/mypage/profile/");
System.out.println("storePathString : " + storePathString);
//String path = ResourceUtils.getFile("classpath:static/img/mypage/profile/").toPath().toString();
//String storePathString = path;
//System.out.println("storePathString : " + storePathString);
} catch (Exception e) {
e.printStackTrace();
}
File saveFolder = new File(EgovWebUtil.filePathBlackList(storePathString));
//폴더 없으면 생성
if (!saveFolder.exists() || saveFolder.isFile()) {
saveFolder.mkdirs();
}
// 원본 파일 이름 가져오기
String originalFileName = imageFile.getOriginalFilename();
// 확장자 추출
int index = originalFileName.lastIndexOf(".");
String fileExt = originalFileName.substring(index + 1);
String newFileName = fileName;
// 저장될 파일 경로 설정
//String filePath = storePathString + File.separator + newFileName + "." + fileExt;
String filePath = storePathString + newFileName + "." + fileExt;
try {
// 동일한 파일명이 존재하는지 확인하고 있다면 기존 파일 삭제
File existingFile = new File(filePath);
if (existingFile.exists()) {
existingFile.delete();
}
// 이미지를 지정된 경로에 저장
imageFile.transferTo(new File(EgovWebUtil.filePathBlackList(filePath)));
System.out.println("Image file saved at: " + filePath);
} catch (IOException e) {
e.printStackTrace();
}
}
}

View File

@@ -6,10 +6,13 @@ import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.no1.wms.account.AccountDto;
import com.no1.wms.account.AccountService;
@@ -20,7 +23,8 @@ public class MypageController {
@Autowired
AccountService accountService;
@Autowired
ImgService imgService;
@GetMapping("/mypage")
public String mypage(Model m, HttpServletRequest request) {
@@ -46,6 +50,17 @@ public class MypageController {
}
}
@PostMapping("/mypage/uplodeImg")
public String imgFileUplode(HttpServletRequest request, MultipartFile file) {
System.out.println(file);
HttpSession session = request.getSession();
AccountDto dto = (AccountDto) session.getAttribute("userData");
String fileName = dto.getId();
System.out.println(fileName);
imgService.imgFileUplode(request, file, fileName);
return "redirect:/mypage";
}
}

View File

@@ -16,16 +16,35 @@
<div class="container-fluid">
<div class="row">
<div class="col-md-4" style="text-align: center;">
<img alt="Default Image" src="/resources/static/img/mypage/defaultimg.png" width="200" />
<img alt="Default Image" src="/main/webapp/img/mypage/profile/defaultimg.png" width="200" />
<!-- <img alt="Default Image" src="/resources/static/img/mypage/defaultimg.png" width="200" /> -->
<hr>
<p><b>
<span style="color: #6E6ED7; font-size: 20px;">${list.positionDto.name }&nbsp;&nbsp; </span>
<span style="font-size: 20px;">${list.name}</span>
</b></p>
<br>
<!-- -->
<form method="post" action="/mypage/uplodeImg" enctype="multipart/form-data" class="form-horizontal">
<div class="col-centered">
<button type="button" class="btn btn-danger" onclick="">프로필 사진 수정</button>
<table id="datatable-scroller" class="table table-bordered tbl_Form">
<tbody>
<tr>
<th class="active" style="text-align:right"><label class="control-label" for="">프로필사진 업로드</label></th>
<td>
<input type="file" name="file" id="file" accept=".jpg, .jpeg, .png" />
</td>
</tr>
</tbody>
</table>
</div>
<div class="col-centered">
<button type="submit" class="btn btn-danger" id="uploadImg">프로필 사진 수정</button>
</div>
</form><!-- uplodeImgForm -->
</div>
@@ -172,6 +191,14 @@
//
});//submitBtn
});//ready
function _onSubmit(){
if($("#file").val() == ""){
alert("이미지 파일을 업로드해주세요.");
$("#file").focus();
return false;
}
return true;
}
</script>

Binary file not shown.

After

Width:  |  Height:  |  Size: 764 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 58 KiB