뷰에서 url에 데이터를 담을때 자바스크립트 함수encodeURIComponent(),
서버에서 꺼낼때 자바 함수 URLDecoder.decode(fileName, "UTF-8");
한글, 특수문자를 처리해야할때 사용
화면 >>>
const showUploadedFile = function(uploadResultArr){
var html = '';
$(uploadResultArr).each(function(i, obj){
if(!obj.image){
var fileCallPath = encodeURIComponent(obj.uploadPath+"/"+obj.uuid+"_"+obj.fileName);
html += '<li><div><a href="/upload/download?fileName='+fileCallPath+'"><img src="/resources/img/attachFile.jpg">' + obj.fileName + '</a><span data-file="'+fileCallPath+'" data-type="file">x</span><div></li>';
}else{
var fileCallPath = encodeURIComponent(obj.uploadPath+"/s_"+obj.uuid+"_"+obj.fileName);
html +='<li><div><a href="/upload/download?fileName='+fileCallPath+'"><img src="/upload/display?fileName='+fileCallPath+'"></a><span data-file="'+fileCallPath+'" data-type="image">x</span><div></li>';
}
});
$('.uploadResult ul').append(html);
}
서버 >>>
@PostMapping("/deleteFile")
@ResponseBody
public ResponseEntity<String> deleteFile(String fileName, String type){
File file;
try {
String decodeUrl = URLDecoder.decode(fileName, "UTF-8");
file = new File("c:\\upload\\"+ URLDecoder.decode(fileName, "UTF-8"));
file.delete(); // 파일 삭제!
if(type.equals("image")) { //이미지 파일이면 섬네일도 삭제
String largeFileName = file.getAbsolutePath();
largeFileName = file.getAbsolutePath().replace("s_", "");
file = new File(largeFileName);
file.delete();
}
}catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<String>(HttpStatus.NOT_FOUND);
}
return new ResponseEntity<String>("deleted", HttpStatus.OK);
}