Page tree

Versions Compared

Key

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

...

Code Block
languagejava
themeEmacs
titleimport
linenumberstrue
@RequestMapping(value = "/importDoc", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> importDoc(@RequestParam("file") MultipartFile importFile) throws IOException {
	static String ROOT_PATH = "C:\\workspace\\synapEditorsample\\out\\artifacts\\synapEditorsample_war_exploded";
	static String DOC_UPLOAD_DIR = "\\uploads\\docs";
	static String WORKSOUTPUT_DIR = "\\uploads\\tmpoutput";
	static String RELATIVE_OUTPUT_URL = "/upload/output";
    String fileName = importFile.getOriginalFilename();

    File uploadDir = new File(ROOT_PATH + DOC_UPLOAD_DIR);
    if(!uploadDir.exists()) {
        uploadDir.mkdirs();
    }

    byte[] bytes = new byte[0];
    try {
        bytes = importFile.getBytes();
    } catch (IOException e) {
        e.printStackTrace();
    }
	
	// 파일 저장경로 설정
    Path pathinputFilePath = Paths.get(ROOT_PATH + DOC_UPLOAD_DIR + "\\"File.separator + fileName);

    try {
		// 파일 저장
        Files.write(pathinputFilePath, bytes);
    } catch (IOException e) {
        e.printStackTrace();
    }


	// 월별로 파일 변환
File	Calendar worksDircal = new File(ROOT_PATH + WORKS_DIR);
    if(!worksDir.exists()) {
Calendar.getInstance();
    String yearMonth = String.format("%04d%02d", cal.get(Calendar.YEAR), cal.get(Calendar.MONTH)+1);
            
    // 파일별로 변환결과를 저장할 경로 생성
    String uuid = worksDir.mkdirsUUID.randomUUID().toString();

	// 경로 생성
}	File outputPath = new File(ROOT_PATH + executeConverter(DOC_UPLOADOUTPUT_DIR  + File.separator + fileName, ROOT_PATH + WORKS_DIRyearMonth + File.separator + uuid);
	if(!outputPath.exists()) {
		outputPath.mkdirs();
	}

    executeConverter(inputFilePath.toAbsolutePath().toString(), outputPath.getAbsolutePath());
    Integer[] serializedData = serializePbData(WORKSOUTPUT_DIR + "\\document.word.pb");

    Map<String, Object> map = new HashMap<>();
    map.put("serializedData", serializedData);
    map.put("importPath", WORKSRELATIVE_OUTPUT_DIRURL);

    return map;
}
 

public	protected static voidint executeConverter(String inputFilePath, String outputFilePathoutputPath) {
	    String SEDOC_CONVERT_DIR = "C:\\sedocConverter\\sedocConverter.exe";
    	String FONT_DIR = "C:\\sedocConverter\\sedocConverter\\fonts";
	    String TEMP_DIR = "C:\\sedocConverter\\sedocConverter\\tmp";

        File tempDir = new File(TEMP_DIR);
        if(!tempDir.exists()) {
            tempDir.mkdirs();
        }

        File fontDir = new File(FONT_DIR);
        if(!fontDir.exists()) {
            fontDir.mkdirs();
    }    }
 Runtime rt = Runtime.getRuntime();    
Process process;      try {// 변환 명령 구성
     process = rt.exec(new String[] cmd = {SEDOC_CONVERT_DIR, "-f", FONT_DIR, inputFilePath, outputFilePathoutputPath, TEMP_DIR};
        try {
            Timer t = new Timer();
            Process proc = Runtime.getRuntime().exec(cmd);

            TimerTask killer = new TimeoutProcessKiller(proc);
            t.schedule(killer, 20000); // 20초  process.waitFor()(변환이 20초 안에 완료되지 않으면 프로세스 종료)

            int exitValue = proc.waitFor();
            killer.cancel();

            return exitValue;
        } catch (Exception e) {
            e.printStackTrace();
            return -1;
        }
    }

 
public Integer[] serializePbData(String pbFilePath) throws IOException {
    List<Integer> serializedData = new ArrayList<Integer>();
    FileInputStream fis = new FileInputStream(pbFilePath);

    Integer[] data = null;

    fis.skip(16);

    InflaterInputStream ifis = new InflaterInputStream(fis);

    byte[] buffer = new byte[1024];

    int len = -1;
    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()]);

    ifis.close();
    fis.close();

    return data;
}

...