Page tree
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 2 Next »

1. Servlet 설정

참고

Servlet 3.0 이상에서는 @WebServlet 어노테이션을 사용하셔도 됩니다.


web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>synapeditor</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
  	<servlet-name>ImportServlet</servlet-name>
  	<servlet-class>com.synap.synapeditor.ImportServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>ImportServlet</servlet-name>
  	<url-pattern>/importDoc</url-pattern>
  </servlet-mapping>
  <servlet>
  	<servlet-name>UploadServlet</servlet-name>
  	<servlet-class>com.synap.synapeditor.UploadServlet</servlet-class>
  </servlet>
  <servlet-mapping>
  	<servlet-name>UploadServlet</servlet-name>
  	<url-pattern>/uploadFile</url-pattern>
  </servlet-mapping>
</web-app>


2. 이미지, 동영상 파일 업로드

주의사항

가이드로 제공되는 아래 코드 중 파일 업로드 부분은 샘플 코드로서 보안 관련 처리가 미흡합니다.

파일 업로드 부분은 프로젝트 내부에서 사용하시는 부분을 그대로 사용하시고 아래 코드를 참고하셔서 연동 부분을 처리해주세요. 

upload
import com.google.gson.GsonBuilder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

public class UploadServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static final String UPLOAD_DIR_REL_PATH = "uploads";

	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.setHeaderEncoding("UTF-8");
		    upload.setFileSizeMax(MAX_FILE_SIZE);
		    upload.setSizeMax(MAX_REQUEST_SIZE);
		
		    String ROOT_ABS_PATH = request.getSession().getServletContext().getRealPath("");
		    String UPLOAD_DIR_ABS_PATH = ROOT_ABS_PATH + File.separator + UPLOAD_DIR_REL_PATH;
		
		    makeDirectory(UPLOAD_DIR_ABS_PATH);
		
		    String storeFileName = "";
		    try {
		        List<FileItem> formItems = upload.parseRequest(request);
		
		        if (formItems != null && formItems.size() > 0) {
		            for (FileItem item : formItems) {
		                if (!item.isFormField()) {
		                    String ext = item.getName().substring(item.getName().lastIndexOf('.'));
		                    storeFileName = UUID.randomUUID().toString() + ext;
		                    String storeFileAbsPath = UPLOAD_DIR_ABS_PATH + File.separator + storeFileName;
		
		                    item.write(new File(storeFileAbsPath)); // 파일 저장
		                }
		            }
		        }
		    } catch (Exception ex) {
		        ex.printStackTrace();
		    }
		
		    Map map = new HashMap();
		    map.put("uploadPath", "uploads/" + storeFileName);
		
		    PrintWriter out = response.getWriter();
		    response.setContentType("application/json");
		    response.setCharacterEncoding("UTF-8");
		
		    out.print(new GsonBuilder().create().toJson(map));
		    out.flush();
		}
    }


	/**
	 * 디렉토리가 없는 경우 디렉토리를 생성합니다.
	 */
	private static void makeDirectory(String dirPath) {
	    File dir = new File(dirPath);
	    if (!dir.exists()) {
	        dir.mkdir();
	    }
	}
}

3. HWP, Word, Excel 문서 임포트

주의사항

가이드로 제공되는 아래 코드 중 파일 업로드 부분은 샘플 코드로서 보안 관련 처리가 미흡합니다.

파일 업로드 부분은 프로젝트 내부에서 사용하시는 부분을 그대로 사용하시고 아래 코드를 참고하셔서 연동 부분을 처리해주세요. 

import
import com.google.gson.GsonBuilder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.UUID;
import java.util.zip.InflaterInputStream;

public class ImportServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private static final String UPLOAD_DIR_REL_PATH = "import";

	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.setHeaderEncoding("UTF-8");
		    upload.setFileSizeMax(MAX_FILE_SIZE);
		    upload.setSizeMax(MAX_REQUEST_SIZE);
		
		    String ROOT_ABS_PATH = request.getSession().getServletContext().getRealPath("");
		    String UPLOAD_DIR_ABS_PATH = ROOT_ABS_PATH + File.separator + UPLOAD_DIR_REL_PATH;
		
		    makeDirectory(UPLOAD_DIR_ABS_PATH);
		
		    String storeFileAbsPath = "";
		    try {
		        List<FileItem> formItems = upload.parseRequest(request);
		
		        if (formItems != null && formItems.size() > 0) {
		            for (FileItem item : formItems) {
		                if (!item.isFormField()) {
							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 storeFileName = UUID.randomUUID().toString() + ext;
		                    storeFileAbsPath = UPLOAD_DIR_ABS_PATH + File.separator + storeFileName;
		                    System.out.println(storeFileAbsPath);
		
		                    item.write(new File(storeFileAbsPath)); // 파일 저장
		                }
		            }
		        }
		    } catch (Exception ex) {
		        ex.printStackTrace();
		    }
		
		    // 파일별로 변환결과를 저장할 경로 생성
		    Calendar cal = Calendar.getInstance();
		    String yearMonth = String.format("%04d%02d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1);
		    String uuid = UUID.randomUUID().toString();
		    String outputDirAbsPath = UPLOAD_DIR_ABS_PATH + File.separator + yearMonth + File.separator + uuid;
		
		    makeDirectory(outputDirAbsPath);
		
		    // 변환 실행
		    executeConverter(storeFileAbsPath, outputDirAbsPath);
		
		    // 변환 후 원본 삭제
		    deleteFile(storeFileAbsPath);
		
		    // 변환된 pb파일을 읽어서 serialzie
		    // v2.3.0 부터 파일명이 document.word.pb에서 document.pb로 변경됨
		    String pbAbsPath = outputDirAbsPath + File.separator + "document.pb";
		    Integer[] serializedData = serializePbData(pbAbsPath);
		
		    // pb파일 삭제
		    deleteFile(pbAbsPath);
		
		    Map map = new HashMap();
		    map.put("serializedData", serializedData);
		    map.put("importPath", UPLOAD_DIR_REL_PATH + "/" + yearMonth + "/" + uuid);
		
		    PrintWriter out = response.getWriter();
		    response.setContentType("application/json");
		    response.setCharacterEncoding("UTF-8");
		
		    out.print(new GsonBuilder().create().toJson(map));
		    out.flush();
		}
    }
    
	/**
	 * 문서 변환 모듈을 실행합니다.
	 */
    protected static int executeConverter(String inputFilePath, String outputPath) {
        String SEDOC_CONVERTER_DIR_ABS_PATH = "변환모듈이 존재하는 디렉토리의 절대경로";
		String FONT_DIR_ABS_PATH = SEDOC_CONVERTER_DIR_ABS_PATH + File.separator + "fonts";
		String TEMP_DIR_ABS_PATH = SEDOC_CONVERTER_DIR_ABS_PATH + File.separator + "temp";
		String SEDOC_CONVERTER_ABS_PATH = SEDOC_CONVERTER_DIR_ABS_PATH + File.separator + "sedocConverter_exe";
		// String SEDOC_CONVERTER_ABS_PATH = SEDOC_CONVERTER_DIR_ABS_PATH + File.separator + "sedocConverter.exe"; // window 서버용
		
		makeDirectory(TEMP_DIR_ABS_PATH);
		makeDirectory(FONT_DIR_ABS_PATH);
		
		// 변환 명령 구성
		String[] cmd = {SEDOC_CONVERTER_ABS_PATH, "-f", FONT_DIR_ABS_PATH, inputFilePath, outputPath, TEMP_DIR_ABS_PATH};
		
		try {
		    Timer t = new Timer();
		    Process proc = Runtime.getRuntime().exec(cmd);
		
		    TimerTask killer = new TimeoutProcessKiller(proc);
		    t.schedule(killer, 20000); // 20초 (변환이 20초 안에 완료되지 않으면 프로세스 종료)
		
		    int exitValue = proc.waitFor();
		    killer.cancel();
		
		    return exitValue;
		} catch (Exception e) {
		    e.printStackTrace();
		    return -1;
		}
    }

	/**
	 * 문서 모듈 실행 후 변환된 결과를 Serialize 합니다.
	 */
    protected static Integer[] serializePbData(String pbFilePath) throws IOException {
        List<Integer> serializedData = new ArrayList<Integer>();
	    FileInputStream fis = null;
	    InflaterInputStream ifis = null;
	    Integer[] data = null;
	
	    try {
	        fis = new FileInputStream(pbFilePath);
	        fis.skip(16);
	
	        ifis = new InflaterInputStream(fis);
	        byte[] buffer = new byte[1024];
	
	        int len;
	        while ((len = ifis.read(buffer)) != -1) {
	            for (int i = 0; i < len; i++) {
	                serializedData.add(buffer[i] & 0xFF);
	            }
	        }
	
	        data = serializedData.toArray(new Integer[serializedData.size()]);
	    } finally {
	        if (ifis != null) ifis.close();
	        if (fis != null) fis.close();
	    }
	
	    return data;
    }


	/**
	 * 파일을 삭제합니다.
	 */
	private static void deleteFile(String path) {
		File file = new File(path);
	    if (file.exists()) {
			file.delete();
		}
	}
	
	/**
	 * 디렉토리가 없는 경우 디렉토리를 생성합니다.
	 */
	private static void makeDirectory(String dirPath) {
	    File dir = new File(dirPath);
	    if (!dir.exists()) {
	        dir.mkdir();
	    }
	}
    
    private static class TimeoutProcessKiller extends TimerTask {
        private Process p;

        public TimeoutProcessKiller(Process p) {
            this.p = p;
        }

        @Override
        public void run() {
            p.destroy();
        }
    }
}
  • No labels