1. Uploading images, video clips and other files
upload
@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; File uploadDir = new File(ROOT_PATH + IMAGE_UPLOAD_DIR); if(!uploadDir.exists()) { uploadDir.mkdirs(); } byte[] bytes = file.getBytes(); Path path = Paths.get(ROOT_PATH + IMAGE_UPLOAD_DIR + File.separator + saveFileName); Files.write(path, bytes); Map<String, Object> map = new HashMap<>(); map.put("uploadPath", "uploads/" + saveFileName); return map; }
2. Importing MS Word / LibreOffice documents
import
@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 = 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 inputFilePath = Paths.get(ROOT_PATH + 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 outputPath = new File(ROOT_PATH + OUTPUT_DIR + File.separator + yearMonth + File.separator + uuid); if(!outputPath.exists()) { outputPath.mkdirs(); } executeConverter(inputFilePath.toAbsolutePath().toString(), outputPath.getAbsolutePath()); Integer[] serializedData = serializePbData(OUTPUT_DIR + "\\document.word.pb"); Map<String, Object> map = new HashMap<>(); map.put("serializedData", serializedData); map.put("importPath", RELATIVE_OUTPUT_URL); 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; }