@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 WORKS_DIR = "\\uploads\\tmp";
String fileName = importFile.getOriginalFilename();
File uploadDir = new File(DOC_UPLOAD_DIR);
if(!uploadDir.exists()) {
uploadDir.mkdirs();
}
byte[] bytes = new byte[0];
try {
bytes = importFile.getBytes();
} catch (IOException e) {
e.printStackTrace();
}
Path path = Paths.get(DOC_UPLOAD_DIR + "\\" + fileName);
try {
Files.write(path, bytes);
} catch (IOException e) {
e.printStackTrace();
}
File worksDir = new File(ROOT_PATH + WORKS_DIR);
if(!worksDir.exists()) {
worksDir.mkdirs();
}
executeConverter(DOC_UPLOAD_DIR + File.separator + fileName, ROOT_PATH + WORKS_DIR);
Integer[] serializedData = serializePbData(WORKS_DIR + "\\document.word.pb");
Map<String, Object> map = new HashMap<>();
map.put("serializedData", serializedData);
map.put("importPath", WORKS_DIR);
return map;
}
public static void executeConverter(String inputFilePath, String outputFilePath) {
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[]{SEDOC_CONVERT_DIR, "-f", FONT_DIR, inputFilePath, outputFilePath, TEMP_DIR});
process.waitFor();
} catch (Exception e) {
e.printStackTrace();
}
}
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;
} |