주의사항
가이드로 제공되는 아래 코드는 샘플 코드로서, 리소스 관리나 보안 관련 처리에 대해서는 미흡할 수 있습니다.
단순 참고용으로만 사용해주세요.
1. Uploading images, video clips and other files
| Code Block | ||||||||
|---|---|---|---|---|---|---|---|---|
| ||||||||
package com.synap.synapeditor; import orgjavax.springframeworkservlet.stereotypeannotation.ControllerWebServlet; import org.springframeworkapache.webcommons.bindfileupload.annotation.RequestMappingFileItem; import org.springframeworkapache.webcommons.bindfileupload.annotationdisk.RequestMethodDiskFileItemFactory; import org.springframeworkapache.webcommons.bindfileupload.annotationservlet.RequestParamServletFileUpload; import orgcom.google.springframework.web.bind.annotation.ResponseBody;gson.Gson; import com.google.gson.JsonObject; import org.springframework.web.multipart.MultipartFile; javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest.HttpServletResponse; import java.io.File; import java.io.IOException; import java.io.*PrintWriter; import java.util.*List; @WebServlet( name = "UploadServlet", urlPatterns = {"/uploadFile"} ) public class UploadServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static final String UPLOAD_DIRECTORY = "upload"; private static final String DEFAULT_FILENAME = "default.file"; private static final int MEMORY_THRESHOLD = 1024 * 1024 * 3; private static final int MAX_FILE_SIZE = 1024 * 1024 * 40; private static final int MAX_REQUEST_SIZE = 1024 * 1024 * 50; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (ServletFileUpload.isMultipartContent(request)) { DiskFileItemFactory factory = new DiskFileItemFactory(); factory.setSizeThreshold(MEMORY_THRESHOLD); factory.setRepository(new File(System.getProperty("java.io.tmpdir"))); ServletFileUpload upload = new ServletFileUpload(factory); upload.setFileSizeMax(MAX_FILE_SIZE); upload.setSizeMax(MAX_REQUEST_SIZE); String uploadPath = getServletContext().getRealPath("") + File.separator + UPLOAD_DIRECTORY; File uploadDir = new File(uploadPath); if (!uploadDir.exists()) { uploadDir.mkdir(); } String fileName = ""; try { List<FileItem> formItems = upload.parseRequest(request); if (formItems != null && formItems.size() > 0) { for (FileItem item : formItems) { if (!item.isFormField()) { fileName = new File(item.getName()).getName(); String filePath = uploadPath + File.separator + fileName; File storeFile = new File(filePath); item.write(storeFile); request.setAttribute("message", "File " + fileName + " has uploaded successfully!"); } } } } catch (Exception ex) { request.setAttribute("message", "There was an error: " + ex.getMessage()); } Gson gson = new Gson(); JsonObject obj = new JsonObject(); obj.addProperty("uploadPath", "/synapeditor/upload/" + fileName); String json = gson.toJson(obj); PrintWriter out = response.getWriter(); response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); out.print(json); out.flush(); } } } |
...