使用swftools出现错误!

指尖de柔情 2015-07-16 03:20:58
package com.doc;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;

public class DocConverter01 {
private static final int environment = 1;// 环境1:windows,2:linux(涉及pdf2swf路径问题)
private String fileString;
private String outputPath = "";// 输入路径,如果不设置就输出在默认位置
private String fileName;
private File pdfFile;
private File swfFile;
private File docFile;

public DocConverter01(String fileString) {
ini(fileString);
}

/*
* 重新设置 file @param fileString
*/
public void setFile(String fileString) {
ini(fileString);
}

/*
* 初始化 @param fileString
*/
private void ini(String fileString) {
this.fileString = fileString;
fileName = fileString.substring(0, fileString.lastIndexOf("."));
docFile = new File(fileString);
pdfFile = new File(fileName + ".pdf");
swfFile = new File(fileName + ".swf");
}

/*
* 转为PDF @param file
*/
private void doc2pdf() throws Exception {
if (docFile.exists()) {
if (!pdfFile.exists()) {
OpenOfficeConnection connection = new SocketOpenOfficeConnection(8100);
try {
connection.connect();
DocumentConverter converter = new OpenOfficeDocumentConverter(connection);
converter.convert(docFile, pdfFile);
// close the connection
connection.disconnect();
System.out.println("****pdf转换成功,PDF输出:" + pdfFile.getPath() + "****");
} catch (java.net.ConnectException e) {
// ToDo Auto-generated catch block
e.printStackTrace();
System.out.println("****swf转换异常,openoffice服务未启动!****");
throw e;
} catch (com.artofsolving.jodconverter.openoffice.connection.OpenOfficeException e) {
e.printStackTrace();
System.out.println("****swf转换器异常,读取转换文件失败****");
throw e;
} catch (Exception e) {
e.printStackTrace();
throw e;
}
} else {
System.out.println("****已经转换为pdf,不需要再进行转化****");
}
} else {
System.out.println("****swf转换器异常,需要转换的文档不存在,无法转换****");
}
}

/*
* 转换成swf
*/
private void pdf2swf() throws Exception {
Runtime r = Runtime.getRuntime();
if (!swfFile.exists()) {
if (pdfFile.exists()) {
if (environment == 1)// windows环境处理
{
try {
// 这里根据SWFTools安装路径需要进行相应更改gpdf2swf
//Process p = r.exec("d:/SWFTools/pdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
Process p = r.exec("d:/swftools-0.9.1/gpdf2swf.exe " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
System.out.println("1111111111111");
System.out.print(loadStream(p.getInputStream())+"1");
System.err.print(loadStream(p.getErrorStream()));
System.out.print(loadStream(p.getInputStream()));
System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
System.out.println("wwwwwwwww");
e.printStackTrace();
throw e;
}
} else if (environment == 2)// linux环境处理
{
try {
Process p = r.exec("pdf2swf " + pdfFile.getPath() + " -o " + swfFile.getPath() + " -T 9");
System.out.print(loadStream(p.getInputStream()));
System.err.print(loadStream(p.getErrorStream()));
System.err.println("****swf转换成功,文件输出:" + swfFile.getPath() + "****");
if (pdfFile.exists()) {
pdfFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
} else {
System.out.println("****pdf不存在,无法转换****");
}
} else {
System.out.println("****swf已存在不需要转换****");
}
}

static String loadStream(InputStream in) throws IOException {
int ptr = 0;
//把InputStream字节流 替换为BufferedReader字符流 2013-07-17修改
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder buffer = new StringBuilder();
while ((ptr = reader.read()) != -1) {
buffer.append((char) ptr);
}
return buffer.toString();
}

/*
* 转换主方法
*/
public boolean conver() {
if (swfFile.exists()) {
System.out.println("****swf转换器开始工作,该文件已经转换为swf****");
return true;
}

if (environment == 1) {
System.out.println("****swf转换器开始工作,当前设置运行环境windows****");
} else {
System.out.println("****swf转换器开始工作,当前设置运行环境linux****");
}

try {
doc2pdf();
pdf2swf();
} catch (Exception e) {
// TODO: Auto-generated catch block
e.printStackTrace();
return false;
}

if (swfFile.exists()) {
return true;
} else {
return false;
}
}

/*
* 返回文件路径 @param s
*/
public String getswfPath() {
if (swfFile.exists()) {
String tempString = swfFile.getPath();
tempString = tempString.replaceAll("\\\\", "/");
return tempString;
} else {
return "";
}
}

/*
* 设置输出路径
*/
public void setOutputPath(String outputPath) {
this.outputPath = outputPath;
if (!outputPath.equals("")) {
String realName = fileName.substring(fileName.lastIndexOf("/"), fileName.lastIndexOf("."));
if (outputPath.charAt(outputPath.length()) == '/') {
swfFile = new File(outputPath + realName + ".swf");
} else {
swfFile = new File(outputPath + realName + ".swf");
}
}
}

public static void main(String s[]) {
//DocConverter d = new DocConverter("E:/TDDOWNLOAD/test.doc");
DocConverter d = new DocConverter("j:/2.doc");
d.conver();
}
}


jar包截图:


运行main方法报错:

****swf转换器开始工作,当前设置运行环境windows****
2015-7-16 15:16:23 com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection connect
信息: connected
2015-7-16 15:16:23 com.artofsolving.jodconverter.openoffice.connection.AbstractOpenOfficeConnection disposing
信息: disconnected
****pdf转换成功,PDF输出:j:\2.pdf
File "j:\2.pdf", line 1
%PDF-1.4
^
SyntaxError: invalid syntax
****swf转换成功,文件输出:j:\2.swf


...全文
304 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
a_3509749 2018-02-06
  • 打赏
  • 举报
回复
为楼上点个赞
xishuishenliu 2016-05-25
  • 打赏
  • 举报
回复
我的问题原因是:开始swftools的安装路径写错了
xishuishenliu 2016-05-25
  • 打赏
  • 举报
回复
楼主,我的问题解决了,pdf转成swf代码如下: /* *linux版本还没有试过,windows版本转换没问题 */ public static void convertPDF2SWF(String src,String target) throws IOException { Process pro = null; if (isWindowsSystem()) { //如果是windows系统 String cmd = "D:/SWFTools/pdf2swf.exe "+src+" -o "+target+" -T 9" ;//命令行命令 pro = Runtime.getRuntime().exec(cmd);//Runtime执行后返回创建的进程对象 }else {//如果是linux系统,路径不能有空格,而且一定不能用双引号,否则无法创建进程 String[] cmd = new String[6]; cmd[0] = "/usr/local/bin/pdf2swf"; cmd[1] = src; cmd[2] = "-o"; cmd[3] = target; cmd[4] = "-T"; cmd[5] = "9"; pro = Runtime.getRuntime().exec(cmd);//Runtime执行后返回创建的进程对象 } final InputStream is = pro.getInputStream(); new Thread( new Runnable(){ public void run(){ BufferedReader br = new BufferedReader(new InputStreamReader(is)); try { while(br.readLine() != null); } catch (IOException e) { e.printStackTrace(); } } } ).start(); // 启动单独的线程来清空process.getInputStream()的缓冲区 InputStream is_ = pro.getErrorStream(); BufferedReader br_ = new BufferedReader(new InputStreamReader(is_)); StringBuilder buf = new StringBuilder(); // 保存输出结果流 String line = null; while((line = br_.readLine()) != null){ buf.append(line); // 循环等待ffmpeg进程结束 } System.out.println("输出结果为:" + buf); } /** * 判断是否是windows操作系统 * @author qingui * @return */ private static boolean isWindowsSystem() { String p = System.getProperty("os.name"); System.out.println(p); return p.toLowerCase().indexOf("windows") >= 0 ? true : false; }
xishuishenliu 2016-05-25
  • 打赏
  • 举报
回复
楼主,我也遇见这问题了,还没找到解决办法,楼主解决了吗,求解!
qq_26790777 2016-05-09
  • 打赏
  • 举报
回复
楼主,你的问题解决了没有,我和你也有类似一样的问题 怎么搞

50,523

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧