Commit 9c84a878 authored by YangZhaoJun1's avatar YangZhaoJun1

ppt转pdf java代码编译

parent 653cf840
......@@ -341,7 +341,20 @@
<artifactId>ganymed-ssh2-build</artifactId>
<version>210</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>19.0</version>
</dependency>
<dependency>
<groupId>com.github.livesense</groupId>
<artifactId>jodconverter-core</artifactId>
<version>1.0.5</version>
</dependency>
</dependencies>
......
package org.rcisoft.business.bchapter.controller;
import org.artofsolving.jodconverter.OfficeDocumentConverter;
import org.artofsolving.jodconverter.office.DefaultOfficeManagerConfiguration;
import org.artofsolving.jodconverter.office.OfficeManager;
import java.io.File;
import java.io.FileNotFoundException;
/**
* Created by Administrator on 2018/1/19.
*/
public class PptToPdf {
private static OfficeManager officeManager;
private static String OFFICE_HOME = "D:/oppenOffice/openOffice";
private static int port[] = { 8100 };
public void convert2PDF(String inputFile, String outputFile) throws FileNotFoundException {
startService();
System.out.println("进行文档转换转换:" + inputFile + " --> " + outputFile);
OfficeDocumentConverter converter = new OfficeDocumentConverter(officeManager);
converter.convert(new File(inputFile), new File(outputFile));
stopService();
System.out.println();
}
// 打开服务器
public static void startService() {
DefaultOfficeManagerConfiguration configuration = new DefaultOfficeManagerConfiguration();
try {
System.out.println("准备启动服务....");
configuration.setOfficeHome(OFFICE_HOME);// 设置OpenOffice.org安装目录
configuration.setPortNumbers(port); // 设置转换端口,默认为8100
configuration.setTaskExecutionTimeout(1000 * 60 * 5L);// 设置任务执行超时为5分钟
configuration.setTaskQueueTimeout(1000 * 60 * 60 * 24L);// 设置任务队列超时为24小时
officeManager = configuration.buildOfficeManager();
officeManager.start(); // 启动服务
System.out.println("office转换服务启动成功!");
} catch (Exception ce) {
System.out.println("office转换服务启动失败!详细信息:" + ce);
}
}
// 关闭服务器
public static void stopService() {
System.out.println("关闭office转换服务....");
if (officeManager != null) {
officeManager.stop();
}
System.out.println("关闭office转换成功!");
}
public static void main(String[] args) throws Exception {
String path = "C:/Users/Administrator/Desktop/";
PptToPdf opc = new PptToPdf();
opc.convert2PDF(path+"abc.pptx", path+"1.pdf");
}
}
......@@ -30,7 +30,6 @@ public class BCodeFile {
private String isFirstFile;
private String studentName;
/**
* 容器属性
*
......
package org.rcisoft.business.bcode.model;
import lombok.Data;
import java.util.List;
/**
* Created by Administrator on 2018/1/19.
*/
@Data
public class StudentFile {
private String studentName;
/*0代表文件 1代表工程*/
private String projectOrFile;
private List<BCodeFile> project;
private BCodeFile bCodeFile;
}
package org.rcisoft.business.bcode.service;
import org.rcisoft.business.bcode.model.BCodeFile;
import org.rcisoft.business.bcode.model.StudentFile;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
/**
......@@ -33,5 +33,5 @@ public interface BCodeService {
String exportFiles(String slId, String studentId, String type, String slCode);
List<BCodeFile> queryAllStudentCode(String slId, String chapterId);
List<StudentFile> queryAllStudentCode(String slId, String chapterId);
}
......@@ -7,6 +7,7 @@ import org.rcisoft.business.bchapter.entity.BChapter;
import org.rcisoft.business.bcode.model.BCodeFile;
import org.rcisoft.business.bcode.model.BCodeLxc;
import org.rcisoft.business.bcode.model.CodeType;
import org.rcisoft.business.bcode.model.StudentFile;
import org.rcisoft.business.bcode.service.BCodeLxcService;
import org.rcisoft.business.bcode.service.BCodeService;
import org.rcisoft.business.brslstudent.dao.BRSlStudentRepository;
......@@ -165,7 +166,7 @@ public class BCodeServiceImpl implements BCodeService {
boolean isFile = true;
simpleCodeFile = new BCodeFile("0", "", null, simplePath.getName(),IdGen.uuid(),"0","");
//codeFile.add(simpleCodeFile);
getFileList(simplePath,codeFile,simpleCodeFile,isFile,"");
getFileList(simplePath,codeFile,simpleCodeFile,isFile);
}
/*容器是否已启动*/
byte[] results = rcRedisServiceImpl.getBytes(global.getLxcPrefix() + lxc.getUserId());
......@@ -216,7 +217,7 @@ public class BCodeServiceImpl implements BCodeService {
* @param codeFile
* @throws IOException
*/
private void getFileList(File files,List<BCodeFile> codeFile,BCodeFile parentFile,boolean isFile,String studentName) throws IOException {
private void getFileList(File files,List<BCodeFile> codeFile,BCodeFile parentFile,boolean isFile) throws IOException {
BCodeFile bCodeFile = new BCodeFile();
bCodeFile.setContent("");
......@@ -225,7 +226,6 @@ public class BCodeServiceImpl implements BCodeService {
bCodeFile.setName(files.getName());
bCodeFile.setId(IdGen.uuid());
bCodeFile.setPId(parentFile.getId());
bCodeFile.setStudentName(studentName);
codeFile.add(bCodeFile);
if(files.isFile()){
if(isFile){
......@@ -239,7 +239,7 @@ public class BCodeServiceImpl implements BCodeService {
bCodeFile.setFileType("0");
File[] ffs=files.listFiles();
for (File ff : ffs) {
getFileList(ff, codeFile, bCodeFile, isFile,studentName);
getFileList(ff, codeFile, bCodeFile, isFile);
}
}
......@@ -431,12 +431,14 @@ public class BCodeServiceImpl implements BCodeService {
@Override
public List<BCodeFile> queryAllStudentCode(String slId, String chapterId){
String codePath = "";
String suffixName = "";
public List<StudentFile> queryAllStudentCode(String slId, String chapterId){
List<StudentFile> studentFiles = new ArrayList<>();
BCodeFile codeFile = null;
List<BCodeFile> students = new ArrayList<>();
List<BChapter> bChapters = bChapterRepository.queryBchapterStudentById(chapterId);
String codePath = "";
String suffixName = "";
for(BChapter chapter : bChapters) {
if (chapter.getExperimentType().equals("1")) {//java单文件
codePath = global.getCOURSE_CODE_LOCATION();
......@@ -464,20 +466,29 @@ public class BCodeServiceImpl implements BCodeService {
if (file.exists()) {
if (file.isFile()) {//单文件的处理
String content = FileUtils.readFileToString(file);
codeFile = new BCodeFile("0", content, new Date(file.lastModified()), file.getName(), IdGen.uuid(), "0", "", studentName,null);
students.add(codeFile);
codeFile = new BCodeFile("0", content, new Date(file.lastModified()), file.getName(), IdGen.uuid(), "0", "",null);
StudentFile studentFile = new StudentFile();
studentFile.setBCodeFile(codeFile);
studentFile.setStudentName(studentName);
studentFile.setProjectOrFile("0");
studentFiles.add(studentFile);
}else if(file.isDirectory()){//工程的处理
List<BCodeFile> codeFiles = new ArrayList<>();
BCodeFile simpleCodeFile = null;
boolean isFile = true;
simpleCodeFile = new BCodeFile("0", "", null, file.getName(),IdGen.uuid(),"0","");
getFileList(file,codeFiles,simpleCodeFile,isFile,studentName);
getFileList(file,codeFiles,simpleCodeFile,isFile);
StudentFile studentFile = new StudentFile();
studentFile.setProject(codeFiles);
studentFile.setStudentName(studentName);
studentFile.setProjectOrFile("1");
studentFiles.add(studentFile);
}
}
}catch (Exception e){
}
}
return students;
return studentFiles;
}
}
......@@ -10,21 +10,22 @@ public class CompileUtil {
* @return
*/
public static String executeJavaCode(String code,String fileName,String fileUrl) throws Exception {
writeCodeToFile(code,fileName,fileUrl);
File file = new File(fileUrl+fileName+".java");
writeCodeToFile(code,fileName,fileUrl);//写文件
File file = new File(fileUrl+fileName+".java");//java文件
//等待文件写完
while(!file.exists()) {
Thread.sleep(10);
}
File dir = new File(fileUrl);
String os = System.getProperty("os.name");
String command1,command2;
String java,java_class;
//判断操作系统
if (os.toLowerCase().startsWith("win")) {
command1 = "c:\\Windows\\System32\\cmd.exe /c javac -encoding utf-8 "+fileName+".java";
command2 = "c:\\Windows\\System32\\cmd.exe /c java "+fileName;
java = "c:\\Windows\\System32\\cmd.exe /c javac -encoding utf-8 "+fileName+".java";
java_class = "c:\\Windows\\System32\\cmd.exe /c java "+fileName;
} else {
command1 = "javac -encoding utf-8 "+fileName+".java";
command2 = "java "+fileName;
java = "javac -encoding utf-8 "+fileName+".java";
java_class = "java "+fileName;
}
Runtime r1 = Runtime.getRuntime();
......@@ -32,13 +33,14 @@ public class CompileUtil {
Boolean comResult1 =true;
String result="";
try {
p1 = r1.exec(command1, null, dir);
//先编译java文件
p1 = r1.exec(java, null, dir);
InputStreamReader ir = new InputStreamReader(p1.getInputStream(),"GBK");
LineNumberReader input = new LineNumberReader(ir);
StringBuilder sb = new StringBuilder();
boolean firstLine = true;
String line;
//取出输出结果
while ((line = input.readLine()) != null) {
comResult1 = false;
if(!firstLine){
......@@ -50,65 +52,50 @@ public class CompileUtil {
}
input.close();
ir.close();
// result = new String(sb.toString().getBytes("iso8859-1"),"utf-8");
result = sb.toString();
/*if(input.readLine()!=null) {
comResult1 = false;
}*/
} catch (IOException e) {
e.printStackTrace();
}
if(!comResult1) {
return result;
}
String classUrl = fileUrl+fileName+".class";
File classFile = new File(classUrl);
while(!classFile.exists()) {
Thread.sleep(10);
if(!comResult1) {
break;
}
}
Runtime r2 = Runtime.getRuntime();
Process p2 = null;
try {
p2 = r2.exec(command2, null, dir);
InputStreamReader ir = new InputStreamReader(p2.getInputStream(),"GBK");
LineNumberReader input = new LineNumberReader(ir);
StringBuilder sb = new StringBuilder();
boolean firstLine = true;
String line;
while ((line = input.readLine()) != null) {
if(!firstLine){
sb.append(System.getProperty("line.separator"));
return result;
}
String classUrl = fileUrl+fileName+".class";
File classFile = new File(classUrl);
while(!classFile.exists()) {
Thread.sleep(10);
if(!comResult1) {
break;
}
}
Runtime r2 = Runtime.getRuntime();
Process p2 = null;
p2 = r2.exec(java_class, null, dir);
InputStreamReader isr = new InputStreamReader(p2.getInputStream(),"GBK");
LineNumberReader lnr = new LineNumberReader(isr);
StringBuilder sbl = new StringBuilder();
boolean firstLine2 = true;
String line2;
while ((line2 = lnr.readLine()) != null) {
if(!firstLine2){
sbl.append(System.getProperty("line.separator"));
}else{
firstLine = false;
firstLine2 = false;
}
sb.append(line);
sbl.append(line2);
}
input.close();
ir.close();
// result = new String(sb.toString().getBytes("iso8859-1"),"utf-8");
result = sb.toString();
//file.delete();
lnr.close();
isr.close();
result = sbl.toString();
file.delete();
classFile.delete();
p2.waitFor();
p2.destroy();
} catch (IOException e) {
e.printStackTrace();
result = "error";
}
p2.waitFor();
p2.destroy();
return result;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment