Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

주의사항

가이드로 제공되는 아래 코드는 샘플 코드로서, 리소스 관리나 보안 관련 처리에 대해서는 미흡할 수 있습니다.

단순 참고용으로만 사용해주세요.


1. Uploading images, video clips and other files

Code Block
languagejava
themeEmacs
titleupload
linenumberstrue
package com.synap.synapeditor;


import javaxorg.servletspringframework.annotationstereotype.WebServletController;
import org.apachespringframework.web.commonsbind.fileuploadannotation.FileItemRequestMapping;
import org.apachespringframework.commonsweb.fileuploadbind.diskannotation.DiskFileItemFactoryRequestMethod;
import org.apachespringframework.commonsweb.fileuploadbind.servletannotation.ServletFileUploadRequestParam;

import comorg.googlespringframework.gson.Gson;
import com.google.gson.JsonObject;
web.bind.annotation.ResponseBody;
import javaxorg.servlet.ServletException;
import javax.servlet.http.HttpServlet;springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.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();   
        }
    }
}

...