
1.1. 클라이언트
2. html
<input type="file" style="display: none" id="image" accept="image/png"/>
- input type = "file" 로 파일을 받는다 { accept="image/png" }
3. javaScript
$("#image").change(function () {
var ext = $(this).val().split('.').pop().toLowerCase();
ext != "png"
var fileImg = this.files[0];
fileUpload(fileImg);
})
- ext 는 파일의 확장자이다.
- ext != "png" 로 비교 가능
- 선택한 파일은 files[0] 데이터를 가공시킨다.
4. ajax
function fileUpload(file) {
var form_data = new FormData();
form_data.append('file', file);
form_data.append('subPath', "images");
$.ajax({
data: form_data,
type: "POST",
url: '/common/fileUpload',
cache: false,
contentType: false,
enctype: 'multipart/form-data',
processData: false,
success: function (url) {
imgUrl = url;
},
error: function (e) {
}
});
}
- cache: false, contentType: false, enctype: 'multipart/form-data', processData: false, 설정을 꼭 해주고 파일을 넘겨준다.
4.1. 서버
5. controller
@ResponseBody
@RequestMapping("/common/fileUpload")
public String fileUpload(@RequestParam("file") MultipartFile file,
@RequestParam("subPath") String subPath){
return commonService.fileUpload(file, subPath);
}
- MultipartFile로 file을 바인딩한다.
6. service
- !file.exists() 폴더가 없으면 file.mkdirs(); 생성된다.
- new File("").getAbsolutePath() 프로젝트 제일 상단 폴더 까지 표시된다.
- rtnVal변수는 클라이언트 img태그에 src에 들어가게된다.
public String fileUpload(MultipartFile attcFile, String subPath) {
String rtnVal = "";
try {
String attcFileNm = "";
String attcFileOriNm = "";
String attcFileOriExt = "";
String storedFileName = "";
attcFileNm = UUID.randomUUID().toString().replaceAll("-", "");
attcFileOriNm = attcFile.getOriginalFilename();
attcFileOriExt = attcFileOriNm.substring(attcFileOriNm.lastIndexOf("."));
storedFileName = attcFileNm + attcFileOriExt;
String fullVal = new File("").getAbsolutePath() + "\\src" +"\\main"+ "\\resources" + "\\static\\";
rtnVal = fullVal + subPath + "\\" + storedFileName;
File file = new File(rtnVal);
if (!file.exists()) {
file.mkdirs();
}
attcFile.transferTo(file);
rtnVal = subPath + "/" + storedFileName;
} catch (Exception e) {
rtnVal = "";
}
return rtnVal;
}
'몰아 넣기' 카테고리의 다른 글
[java/spring]엑셀 업로드 예제(Excel upload) (0) | 2022.07.10 |
---|---|
[java/spring] jwt 예제 (JWT example) (0) | 2022.07.10 |
[java/spring] UUID를 사용해서 파일이름 랜덤으로 만들기 또는 이미지 확장자 체크하기 (0) | 2022.07.10 |
[java/spring/api] 스프링부트 구글 로그인 인가코드 또는 엑세스토큰을 이용하여 구현하기 (0) | 2022.07.03 |
[그 외] 프로젝트 폴더 구조 (0) | 2022.06.29 |