String filePath = VarFactory.getVarValue("DOC_IA") +person.getDeptCode() + "/" + isoIaAnnualPlan.getPlanName() +".doc";
System.out.println("filePath:"+ filePath);
if (!(file == null ||file.isEmpty())) {
StringfileName = file.getOriginalFilename();
StringfileFormat = fileName.substring(fileName.length() - 4,fileName.length());
System.out.println("!!fileName:" + fileName);
System.out.println("!!fileFormat:" + fileFormat);
if(!fileFormat.equals(".doc")) {
response.setContentType("text/html;charset=UTF-8");
try{
response.getWriter().write("{success:false,msg:'文件格式错误!请使用office2003Word文件格式!'}");
}catch (IOException e) {
e.printStackTrace();
}
returnnull;
}
FileexsitFile = new File(filePath);
if(exsitFile.exists()) {
exsitFile.delete();
}
try {
file.transferTo(createFile(isoIaAnnualPlan.getPlanName(),person.getDeptCode()));
System.out.println("!!filePath:" + filePath);
ConvertorUtil.convertWord2SWF(filePath);
} catch(IllegalStateException e) {
e.printStackTrace();
} catch(IOException e) {
e.printStackTrace();
}
} else {
filePath ="";
}
///----------------------------------------------------------------
public class ConvertorUtil {
public static void FlashPaperConvert(Stringf1,String f2){
//ProcessBuilder p=newProcessBuilder("flashprinter.exe",f1,"-o",f2);
ProcessBuilder p=newProcessBuilder("pdf2swf.exe ",f1,"-o ",f2+" -T 9");
try {
Processp1=p.start();
} catch (IOException e) {
e.printStackTrace();
}
}
安装完swftools-0.9.2.exe,将pdf2swf.exe拷贝到c:\windows\system32下面,就能调用下面程序来将pdf转成swf
public static void convertWord2SWF(String word){
//String swfFile = "cmd /cflashprinter.exe " + word + " -o " + StringUtils.removeEnd(word,".doc") + ".swf";
String tempDirStr =VarFactory.getVarValue("DOC_TEMP");
File tempDir = newFile(tempDirStr);
if (!tempDir.exists()) {
tempDir.mkdirs();
}
File file = new File(tempDirStr+ Math.random() * 10 + ".pdf");
JOD4DocToPDF.docToPdf(newFile(word), file);
String swfFile = "pdf2swf.exe "+ file.getAbsolutePath() + " -o " + StringUtils.removeEnd(word,".doc") + ".swf -T 9";
System.out.println("!!!swfFile: " + swfFile);
try {
Runtime.getRuntime().exec(swfFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}
///----------------------------------------------------------
public class JOD4DocToPDF extends Thread {
protected final Log logger =LogFactory.getLog(getClass());
private File inputFile;
private JOD4DocToPDF() {
}
public JOD4DocToPDF(File inputFile) {
this.inputFile =inputFile;
}
public static void docToPdf(File inputFile,File outputFile) {
// connect to anOpenOffice.org instance running on port 8100
// 用cmd进入目录后 敲命令 soffice-headless -accept="socket,host=127.0.0.1,port=8100;urp;"-nofirststartwizard
Date start = new Date();
OpenOfficeConnection connection= new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverterconverter = new OpenOfficeDocumentConverter(connection);
converter.convert(inputFile,outputFile);
} catch (ConnectException cex){
throw newRuntimeException();
} finally {
if(connection != null) {
connection.disconnect();
connection= null;
}
}
long l = (start.getTime() - newDate().getTime());
long day = l / (24 * 60 * 60 *1000);
long hour = (l / (60 * 60 *1000) - day * 24);
long min = ((l / (60 * 1000)) -day * 24 * 60 - hour * 60);
long s = (l / 1000 - day * 24 *60 * 60 - hour * 60 * 60 - min * 60);
//logger.info("生成" +outputFile.getAbsolutePath() + "耗费:" + min + "分" + s + "秒");
}
@Override
public void run() {
JOD4DocToPDF t = newJOD4DocToPDF();
StringBuffer pdfName = newStringBuffer(inputFile.getParent()).append(File.separator).append(StringUtils.removeEnd(inputFile.getName(),".doc")).append(".pdf");
t.docToPdf(inputFile, newFile(pdfName.toString()));
}
public static void main(String[] args) {
docToPdf(newFile("c:/aaaa.doc"), new File("c:/aaaa.pdf"));
}
}
//-----------------------------------------------------------------------------------------
public static String convertWord2SWF(String word) {
//String swfFile = "cmd /cflashprinter.exe " + word + " -o " + StringUtils.removeEnd(word,".doc") + ".swf";
String tempDirStr =VarFactory.getVarValue("DOC_TEMP");
File tempDir = newFile(tempDirStr);
String pdfFile = "";
if (!tempDir.exists()) {
tempDir.mkdirs();
}
File file = new File(tempDirStr+ Math.random() * 10 + ".pdf");
JOD4DocToPDF.docToPdf(newFile(word), file);
pdfFile =file.getAbsolutePath();
String swfFile = "pdf2swf.exe "+ file.getAbsolutePath() + " -o " + StringUtils.removeEnd(word,".doc") + ".swf -T 9";
try {
Runtime.getRuntime().exec(swfFile);
} catch (IOException e) {
e.printStackTrace();
}
return pdfFile;
}