Page tree

Versions Compared

Key

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

...

Code Block
languagejava
themeEmacs
titleupload
linenumberstrue
@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@ResponseBody
public Map<String, Object> uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
	static String ROOT_PATH = "C:\\workspace\\synapEditorsample\\out\\artifacts\\synapEditorsample_war_exploded";
	static String IMAGE_UPLOAD_DIR = "\\uploads";
    String fileName = file.getOriginalFilename();
    	String ext = fileName.substring(fileName.lastIndexOf('.'));
    	String saveFileName = UUID.randomUUID().toString() + ext;

	String    File uploadDir uploadPath = new File(ROOT_PATH + File.separator + IMAGE_UPLOAD_DIR);
	File uploadDir = new File(uploadPath);
	if(!uploadDir.exists()) {
	        uploadDir.mkdirs();
    	}

    	byte[] bytes = file.getBytes();
    	Path path = Paths.get(ROOT_PATHuploadPath + IMAGE_UPLOAD_DIR + File.separator + saveFileName);
    	Files.write(path, bytes);

    	Map<String, Object> map = new HashMap<>();

	// 브라우저에서 접근가능한 경로를 uploadPath에 담아서 넘겨줍니다.
	map.put("uploadPath", "uploads/" + saveFileName);

 

 return map;
}

2. Importing MS Word / LibreOffice documents

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 OUTPUT_DIR = "\\uploads\\output";
	static String RELATIVE_OUTPUT_URL =
"/upload/output";
    String fileName = importFile.getOriginalFilename();

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

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

    	try {
		// 파일 저장         	Files.write(inputFilePath, bytes);
    	} catch (IOException e) {
        	e.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();

	// 경로 생성
	File outputPathworksDir = new File(ROOT_PATH + File.separator + OUTPUT_DIR  + File.separator + yearMonth + File.separator + uuid);
	if(!outputPathworksDir.exists()) {
		outputPath    worksDir.mkdirs();
	}

	// 문서 변환
 	executeConverter(inputFilePath.toAbsolutePath().toString(), outputPathworksDir.getAbsolutePath());

	// 변환이 끝난 원본파일은 삭제한다.
	Files.delete(inputFilePath);

	Integer[] serializedData = serializePbData(OUTPUT_DIRworksDir + File.separator + "\\document.word.pb");

	Files.delete(Paths.get(worksDir + File.separator + "document.word.pb"));

	Map<String, Object> map = new HashMap<>();
    	map.put("serializedData", serializedData);
	// 브라우저에서 접근가능한 경로를 importPath에 담아서 넘겨줍니다.
	map.put("importPath", RELATIVE_OUTPUT_URL);

     "uploads/output/" + yearMonth + "/" + uuid);

	return map;
}


protected static int executeConverter(String inputFilePath, String outputPath) {
	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();
	}
        
	// 변환 명령 구성
	String[] cmd = {SEDOC_CONVERT_DIR, "-f", FONT_DIR, inputFilePath, outputPath, TEMP_DIR};
	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;
	}
}

 
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;
}


private static class TimeoutProcessKiller extends TimerTask {
    private Process p;

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

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