import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.nio.charset.CharsetDecoder;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class SysTools{
public SysTools(String path){
}
public static String InputStream2String(InputStream in)throws IOException {
StringBuffer out = new StringBuffer();
byte[] b = new byte[4096];
for (int n;(n = in.read(b)) != -1;){
out.append(new String(b,0,n));
}
return out.toString();
}
public static String MountAndModifyPath(char[] chNewSrcFile,int srcLen,String strNewStrFile){
return strNewStrFile;
}
public static int GetPdfPageNum(String file){
int page_num;
Process exe;
Process test;
try {
String[] cmd = new String[]{"/bin/sh","-c","./pdf2swf -I " + file + " | grep -c 'page'"};
exe = Runtime.getRuntime().exec(cmd);
InputStream inputStream = exe.getInputStream();
String outInf = InputStream2String(inputStream);
System.out.println("******* outInf:" + outInf);
outInf = outInf.replaceAll("\r|\n", "");
page_num = Integer.parseInt(outInf);
} catch (IOException e) {
e.printStackTrace();
page_num = -1;
}
return page_num;
}
public static String GetFileName(String path){
int index = path.lastIndexOf("/");
//将字符串转为字符数组
char[] ch = path.toCharArray();
//根据 copyValueOf(char[] data, int offset, int count) 取得最后一个字符串
String fileName = String.copyValueOf(ch, index + 1, ch.length - index - 1);
return fileName;
}
/*
* Java文件操作 获取文件扩展名
*
* Created on: 2011-8-2
* Author: blueeagle
*/
public static String GetExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
/*
* Java文件操作 获取不带扩展名的文件名
*
* Created on: 2011-8-2
* Author: blueeagle
*/
public static String GetFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}
public static void CheckAndBuildTargetFoleder(String targetFoleder){
File file = new File(targetFoleder);
//判断文件夹是否存在,如果不存在则创建文件夹
if (!file.exists()) {
file.mkdir();
}
}
public static String ByteBuffToString(ByteBuffer buffer){
Charset charset = null;
CharsetDecoder decoder = null;
CharBuffer charBuffer = null;
try{
charset = Charset.forName("UTF-8");
decoder = charset.newDecoder();
// charBuffer = decoder.decode(buffer);//用这个的话,只能输出来一次结果,第二次显示为空
charBuffer = decoder.decode(buffer.asReadOnlyBuffer());
return charBuffer.toString();
} catch (Exception ex) {
ex.printStackTrace();
return "";
}
}
public static String GetFilePath(String path){
String filePath = path.replaceAll(GetFileName(path), "");
return filePath;
}
/**
* 删除单个文件
* @param sPath 被删除文件的文件名
* @return 单个文件删除成功返回true,否则返回false
*/
public static boolean deleteFile(String sPath) {
boolean flag = false;
File file = new File(sPath);
// 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) {
file.delete();
flag = true;
}
return flag;
}
/**
* 删除目录(文件夹)以及目录下的文件
* @param sPath 被删除目录的文件路径
* @return 目录删除成功返回true,否则返回false
*/
public static boolean deleteDirectory(String sPath) {
//如果sPath不以文件分隔符结尾,自动添加文件分隔符
if (!sPath.endsWith(File.separator)) {
sPath = sPath + File.separator;
}
File dirFile = new File(sPath);
//如果dir对应的文件不存在,或者不是一个目录,则退出
if (!dirFile.exists() || !dirFile.isDirectory()) {
return false;
}
boolean flag = true;
//删除文件夹下的所有文件(包括子目录)
File[] files = dirFile.listFiles();
for (int i = 0; i < files.length; i++) {
//删除子文件
if (files.isFile()) {
flag = deleteFile(files.getAbsolutePath());
if (!flag) break;
} //删除子目录
else {
flag = deleteDirectory(files.getAbsolutePath());
if (!flag) break;
}
}
if (!flag) return false;
//删除当前目录
if (dirFile.delete()) {
return true;
} else {
return false;
}
}
public static Boolean CreateNewFile(String target,String totalPage){
FileWriter fwr = null;
BufferedWriter bwr = null;
File file = new File(target);
if(file.exists()){
System.out.println("file:" + target + "exists");
if (file.isFile()) { // 为文件时调用删除文件方法
file.delete();
// if(deleteFile(target) == false)
// return false;
} else { // 为目录时调用删除目录方法
if(deleteDirectory(target) == false)
return false;
}
}
try {
if (file.createNewFile()) {
System.out.println("Create file: " + target + " sucess!");
//return true;
} else {
System.out.println("Create file: " + target + " fail!");
return false;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("error:" + target + "!" + e.getMessage());
return false;
}
try{
fwr = new FileWriter(file);
bwr = new BufferedWriter(fwr);
bwr.write(totalPage);
bwr.newLine();//换行
bwr.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}finally{
try {
bwr.close();
fwr.close();
return true;
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
public static Boolean convert_pdf2swf(RecvMessage msg){
//File file = new File(msg.getFile());
//if(! file.isDirectory()){
// System.out.println("file:" + msg.getFile() + "is not exist!");
// return -1;
//}
String formatcmd;
String cmd;
Process exe;
String file = msg.getFile();
String fileName = SysTools.GetFileName(file);
String fileNameNoEx = SysTools.GetFileNameNoEx(fileName);
//String pdfFileName = msg.getPrefix() + '.' + fileNameNoEx + ".pdf";
int pagecount = GetPdfPageNum(msg.getTargetfolder() + "/" + msg.getPrefix() + '.' + fileNameNoEx + ".pdf");
if(pagecount == -1){
System.out.println("GetPdfPageNum :" + msg.getTargetfolder() + "/" + msg.getPrefix() + '.' + fileNameNoEx + ".pdf" + "error!");
return false;
}
System.out.println("GetPdfPageNum");
CheckAndBuildTargetFoleder(msg.getTargetfolder());
String tpfilename = msg.getTargetfolder() + "/totalpage.txt";
System.out.println("tpfilename:" + tpfilename);
Boolean ret = CreateNewFile(tpfilename,"" + pagecount);
if(ret == false){
return ret;
}
System.out.println("CreateNewFile->totalpage.txt pagecount:" + pagecount);
// TODO: write tpfile
for(int i = 0;i < pagecount;i ++){
if(msg.getPrefix().equals(""))
{
formatcmd = "./pdf2swf -p %d %s -o %s/%d.swf";
cmd = String.format(formatcmd,i + 1,msg.getTargetfolder() + "/" + msg.getPrefix() + '.' + fileNameNoEx + ".pdf",msg.getTargetfolder(),i);
System.out.println("++++ cmd:" + cmd);
}
else
{
formatcmd = "./pdf2swf -p %d %s -o %s/%s%d.swf";
cmd = String.format(formatcmd,i + 1,msg.getTargetfolder() + "/" + msg.getPrefix() + '.' + fileNameNoEx + ".pdf",msg.getTargetfolder(),msg.getPrefix() + '.',i);
System.out.println("*++++++++ cmd:" + cmd + " i:" + i);
//cmd = CommonTool::format_string(formatcmd.c_str(), i+1, filename.c_str(), pTransitionRequest->GetTargetFoleder().c_str(),(pTransitionRequest->GetPrefix()+".").c_str(), i);
}
try {
//exe = Runtime.getRuntime().exec(new String[]{"/bin/sh","-c","/root/workspace/openoffice/pdf2swf -I " + file + " | grep -c 'page'"});
exe = Runtime.getRuntime().exec(cmd);
InputStream inputStream = exe.getInputStream();
String outInf = InputStream2String(inputStream);
if(outInf.indexOf("Writing SWF file") >= 0){
// TODO: CommonTool::write_emptyimg
System.out.println("CommonTool::write_emptyimg");
//return false;
}
} catch (IOException e) {
e.printStackTrace();
// TODO:
}
}
return true;
}
public static boolean SetIniFile(String file,String section,String variable,String value)throws IOException {
String fileContent, allLine,strLine, newLine, remarkStr;
String getValue;
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
boolean isInSection = false;
fileContent = "";
try {
while ((allLine = bufferedReader.readLine()) != null) {
allLine = allLine.trim();
if (allLine.split("[;]").length > 1)
remarkStr = ";" + allLine.split(";")[1];
else
remarkStr = "";
strLine = allLine.split(";")[0];
Pattern p;
Matcher m;
p = Pattern.compile("[s*.*s*]");
m = p.matcher((strLine));
if (m.matches()) {
p = Pattern.compile("[s*" + section + "s*]");
m = p.matcher(strLine);
if (m.matches()) {
isInSection = true;
} else {
isInSection = false;
}
}
if (isInSection == true) {
strLine = strLine.trim();
String[] strArray = strLine.split("=");
getValue = strArray[0].trim();
if (getValue.equalsIgnoreCase(variable)) {
newLine = getValue + " = " + value + " " + remarkStr;
fileContent += newLine + " ";
while ((allLine = bufferedReader.readLine()) != null) {
fileContent += allLine + " ";
}
bufferedReader.close();
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file, false));
bufferedWriter.write(fileContent);
bufferedWriter.flush();
bufferedWriter.close();
return true;
}
}
fileContent += allLine + " ";
}
}catch(IOException ex){
throw ex;
} finally {
bufferedReader.close();
}
return false;
}
}
|