开通会员
- 尊享所有功能
- 文件大小最高200M
- 文件无水印
- 尊贵VIP身份
- VIP专属服务
- 历史记录保存30天云存储
package com.cectsims.util;
import java.io.bufferedinputstream;
import java.io.file;
import java.io.ioexception;
import java.io.inputstream;
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;
/**
* doc docx格式转换
*/
public class docconverter {
private static final int environment = 1;// 环境 1:windows 2:linux
private string filestring;// (只涉及pdf2swf路径问题)
private string outputpath = "";// 输入路径 ,如果不设置就输出在默认的位置
private string filename;
private file pdffile;
private file swffile;
private file docfile;
public docconverter(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) {
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
*/
@suppresswarnings("unused")
private void pdf2swf() throws exception {
runtime r = runtime.getruntime();
if (!swffile.exists()) {
if (pdffile.exists()) {
if (environment == 1) {// windows环境处理
try {
// process p = r.exec("d:/program files/swftools/pdf2swf.exe "+ pdffile.getpath() + " -o "+ swffile.getpath() + " -t 9");
process p = r.exec("c:/program files (x86)/swftools/pdf2swf.exe "+ pdffile.getpath() + " -o "+ swffile.getpath() + " -t 9");
system.out.print(loadstream(p.getinputstream()));
system.err.print(loadstream(p.geterrorstream()));
system.out.print(loadstream(p.getinputstream()));
system.err.println("****swf转换成功,文件输出:"
+ swffile.getpath() + "****");
if (pdffile.exists()) {
pdffile.delete();
}
} catch (ioexception e) {
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;
in = new bufferedinputstream(in);
stringbuffer buffer = new stringbuffer();
while ((ptr = in.read()) != -1) {
buffer.append((char) ptr);
}
return buffer.tostring();
}
/**
* 转换主方法
*/
@suppresswarnings("unused")
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) {
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");
}
}
}
}
转换为pdf,然后转换为swf,网上很多这个过程的代码,你可以看看
连续签到7天
获得积分+10
明天签到可得10积分
扫描二维码,添加客服微信