Table of Contents |
---|
1.
...
이미지, 동영상, 파일 업로드
Warning | ||
---|---|---|
| ||
가이드로 제공되는 아래 코드 중 파일 업로드 부분은 샘플 코드로서 보안 관련 처리가 미흡합니다. 파일 업로드 부분은 프로젝트 내부에서 사용하시는 부분을 그대로 사용하시고 아래 코드를 참고하셔서 연동 부분을 처리해주세요. |
...
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest; import java.io.*; import java.util.*; @Controller public class UploadController { static String IMAGE_UPLOAD_DIR_REL_PATH = "uploads"; @RequestMapping(value = "/uploadFile", method = RequestMethod.POST) @ResponseBody public Map<String, Object> uploadFile(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException { String ROOT_ABS_PATH = request.getSession().getServletContext().getRealPath(""); String UPLOAD_DIR_ABS_PATH = ROOT_ABS_PATH + File.separator + IMAGE_UPLOAD_DIR_REL_PATH; makeDirectory(UPLOAD_DIR_ABS_PATH); String fileName = file.getOriginalFilename(); String ext = ""; String contentType = file.getContentType(); if(contentType != null) { ext = "." + contentType.substring(contentType.lastIndexOf('/') + 1); } else if (fileName.lastIndexOf('.') > 0) { ext = fileName.substring(fileName.lastIndexOf('.')); } if (ext.indexOf(".jpeg") > -1) { // jpg가 더많이쓰여서 jpeg는 jpg로 변환 ext = ".jpg"; } String saveFileName = UUID.randomUUID().toString() + ext; String saveFileAbsPath = UPLOAD_DIR_ABS_PATH + File.separator + saveFileName; writeFile(saveFileAbsPath, file.getBytes()); Map<String, Object> map = new HashMap<String, Object>(); // 브라우저에서 접근가능한 경로를 uploadPath에 담아서 넘겨줍니다. map.put("uploadPath", "uploads/" + saveFileName); return map; } /** * 파일을 씁니다. */ private static void writeFile(String path, byte[] bytes) throws IOException { OutputStream os = null; try { os = new FileOutputStream(path); os.write(bytes); } finally { if (os != null) os.close(); } } /** * 디렉토리가 없는 경우 디렉토리를 생성합니다. */ private static void makeDirectory(String dirPath) { File dir = new File(dirPath); if (!dir.exists()) { dir.mkdir(); } } } |
2.
...
HWP, MS 워드, 엑셀 문서 임포트
Warning | ||
---|---|---|
| ||
가이드로 제공되는 아래 코드 중 파일 업로드 부분은 샘플 코드로서 보안 관련 처리가 미흡합니다. 파일 업로드 부분은 프로젝트 내부에서 사용하시는 부분을 그대로 사용하시고 아래 코드를 참고하셔서 연동 부분을 처리해주세요. |
...