Commit 3008305a authored by 杜科's avatar 杜科

文档转换

parent 92c25bcf
......@@ -3,15 +3,22 @@ package com.yonde.doc.entity;
/**
* description:文档转换服务配置信息
* create date: 2019-09-16
* author: liu-shuai
*/
public class DocTransferProperties {
public static String host="pdm.yonde91.com" ;// #需设为转换服务存放位置
public static String port="8082";
public static String namespaceURI="www.ptc.com/ws";
public static String localPart="ConvertDocPDFService";
public static String supportDocType="doc,docx,xls,xlsx,ppt,pptx,txt";
public static final String host="pdm.yonde91.com" ;// #需设为转换服务存放位置
public static final Integer port=8082;
public static final String namespaceURI="www.ptc.com/ws";
public static final String localPart="ConvertDocPDFService";
public static final String supportDocType="doc,docx,xls,xlsx,ppt,pptx,txt";
public static final String office2pdf="D:\\\\transfer\\\\office2pdf\\\\";
//ftp服务器地址
public static final String hostname="192.168.1.240";
//ftp服务器端口号默认为21
public static final Integer ftpport=21;
//ftp登录账号
public static final String username="ftpname";
//ftp登录密码
public static final String password="ftppassword";
}
......@@ -7,12 +7,11 @@ import org.springframework.stereotype.Component;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import java.io.File;
import java.net.URL;
/**
* description:
* create date: 2019-09-21
* author: liu-shuai
*/
@Component
public class ConvertDoc2PdfUtil {
......@@ -36,4 +35,15 @@ public class ConvertDoc2PdfUtil {
}
return null;
}
public static String downloadFile(String serverPdfFile) {
long beginTime = System.currentTimeMillis();
if (!new File(DocTransferProperties.office2pdf).exists()) {
new File(DocTransferProperties.office2pdf).mkdirs();
}
String serverPath = serverPdfFile.substring(0, serverPdfFile.lastIndexOf(File.separator) + 1);
String pdfFile = serverPdfFile.substring(serverPdfFile.lastIndexOf(File.separator) + 1);
FtpClient.downloadFile(serverPath, pdfFile, DocTransferProperties.office2pdf);
return DocTransferProperties.office2pdf + pdfFile;
}
}
package com.yonde.doc.util;
import com.yonde.doc.entity.DocTransferProperties;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import wt.log4j.LogR;
import java.io.*;
/**
* description:
*/
@Service
public class FtpClient {
private static final Logger log=LogR.getLogger(FtpClient.class.getName());
/**
* FTPClient ftp = new FTPClient();
* FTPClientConfig config = new FTPClientConfig();
* config.setXXX(YYY); // change required options
* // for example config.setServerTimeZoneId("Pacific/Pitcairn")
* ftp.configure(config );
* boolean error = false;
* try {
* int reply;
* String server = "ftp.example.com";
* ftp.connect(server);
* System.out.println("Connected to " + server + ".");
* System.out.print(ftp.getReplyString());
* <p>
* // After connection attempt, you should check the reply code to verify
* // success.
* reply = ftp.getReplyCode();
* <p>
* if(!FTPReply.isPositiveCompletion(reply)) {
* ftp.disconnect();
* System.err.println("FTP server refused connection.");
* System.exit(1);
* }
* ... // transfer files
* ftp.logout();
* } catch(IOException e) {
* error = true;
* e.printStackTrace();
* } finally {
* if(ftp.isConnected()) {
* try {
* ftp.disconnect();
* } catch(IOException ioe) {
* // do nothing
* }
* }
* System.exit(error ? 1 : 0);
* }
*/
public static void initFtp(FTPClient ftp) throws IOException {
ftp.connect(DocTransferProperties.hostname, DocTransferProperties.ftpport);
ftp.login(DocTransferProperties.username, DocTransferProperties.password);
ftp.setControlEncoding("utf-8");
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
int replyCode = ftp.getReplyCode(); //是否成功登录服务器
if (!FTPReply.isPositiveCompletion(replyCode)) {
ftp.disconnect();
System.err.println("FTP server refused connection.");
log.error("ftp :{}:{} 服务器连接失败:"+DocTransferProperties.hostname+"---" +DocTransferProperties.ftpport);
}
ftp.enterLocalPassiveMode();
log.info("ftp :{}:{} 服务器连接成功:"+DocTransferProperties.hostname+"---" +DocTransferProperties.ftpport);
}
public static void closeFtp(FTPClient ftp) {
try {
ftp.logout();
} catch (IOException ioe) {
log.error("ftp用户 :{}:{} 退出失败,原因{}:"+ DocTransferProperties.username+"---" +DocTransferProperties.hostname, ioe);
}
if (ftp.isConnected()) {
try {
ftp.disconnect();
log.debug("ftp :{}:{} 服务器断开连接成功:"+ DocTransferProperties.hostname+"---" +DocTransferProperties.ftpport);
} catch (IOException ioe) {
log.error("ftp :{}:{} 服务器断开连接失败,原因{}:"+DocTransferProperties.hostname+"---"+DocTransferProperties.ftpport, ioe);
}
}
}
/**
* 下载文件 *
*
* @param pathname FTP服务器文件目录 *
* @param fileName 文件名称 *
* @param localPath 下载后的文件路径 *
* @return
*/
public static void downloadFile(String pathname, String fileName, String localPath) {
FTPClient ftp = new FTPClient();
try {
initFtp(ftp);
// transfer files传输文件开始
boolean flag;
OutputStream os = null;
try {
if (!localPath.endsWith(File.separator)) {
localPath += File.separator;
}
//切换FTP目录
final boolean changeDir = ftp.changeWorkingDirectory(pathname);
if (!changeDir) {
log.error("没有找到" + pathname + "该路径");
return;
}
File localFile = new File(localPath + fileName);
os = new FileOutputStream(localFile);
//复制文件流
flag = ftp.retrieveFile(new String(fileName.getBytes("utf-8"), "iso-8859-1"), os);
if (!flag) {
log.error("没有找到" + fileName + "该文件");
localFile.delete();
}
} catch (Exception e) {
log.error("ftp下载:从ftp目录:{}下载文件:{}到本地文件夹:{}--失败,原因{}"+pathname+fileName+localPath, e);
} finally {
closeOutputStream(os);
}
log.info("ftp下载:从ftp目录:{}下载文件:{}到本地文件夹:{}--成功"+pathname+ fileName+localPath);
// transfer files传输文件结束
} catch (IOException e) {
log.error("ftp:{}:{} 服务器连接失败,原因{}:"+ DocTransferProperties.hostname+"---" +DocTransferProperties.ftpport, e);
} finally {
closeFtp(ftp);
}
}
/**
* 上传文件
*
* @param pathname ftp服务保存地址
* @param fileName 上传到ftp的文件名
* @param originfilename 待上传文件的名称(绝对地址) *
* @return
*/
public static void uploadFile(String pathname, String fileName, String originfilename) {
FTPClient ftp = new FTPClient();
try {
initFtp(ftp);
// transfer files传输文件开始
log.info("ftp上传{}文件到ftp目录{}--开始"+ fileName+"---"+pathname);
InputStream inputStream = null;
try {
inputStream = new FileInputStream(new File(originfilename));
upload(ftp, pathname, fileName, inputStream);
} catch (Exception e) {
log.error("ftp上传{}文件到ftp目录{}--出错"+fileName+"---"+ pathname, e);
} finally {
closeInputStream(inputStream);
}
log.info("ftp上传{}文件到ftp目录{}--成功"+fileName+"---"+ pathname);
// transfer files传输文件结束
} catch (IOException e) {
log.error("ftp:{}:{} 服务器连接失败,原因{}:"+DocTransferProperties.hostname+"---"+ DocTransferProperties.ftpport, e);
} finally {
closeFtp(ftp);
}
}
/**
* 上传文件
*
* @param pathname ftp服务保存地址
* @param fileName 上传到ftp的文件名
* @param inputStream 输入文件流
* @return
*/
public static boolean uploadFile(String pathname, String fileName, InputStream inputStream) {
FTPClient ftp = new FTPClient();
try {
initFtp(ftp);
// transfer files传输文件开始
boolean flag = false;
try {
upload(ftp, pathname, fileName, inputStream);
flag = true;
} catch (Exception e) {
log.error("ftp上传{}文件到ftp目录{}--失败,"+fileName+"---"+ pathname, e);
} finally {
closeInputStream(inputStream);
}
log.info("ftp上传{}文件到ftp目录{}--成功"+ fileName+"---"+ pathname);
return flag;
// transfer files传输文件结束
} catch (IOException e) {
log.error("ftp:{}:{} 服务器连接失败,原因{}:"+DocTransferProperties.hostname+"---"+DocTransferProperties.ftpport, e);
return false;
} finally {
closeFtp(ftp);
}
}
public static void closeOutputStream(OutputStream os) {
if (null != os) {
try {
os.close();
} catch (IOException e) {
log.error("关闭outputStream异常", e);
}
}
}
public static void closeInputStream(InputStream is) {
if (null != is) {
try {
is.close();
} catch (IOException e) {
log.error("关闭inputStream异常", e);
}
}
}
public static void upload(FTPClient ftp, String pathname, String fileName, InputStream inputStream) throws IOException {
ftp.makeDirectory(pathname);
ftp.changeWorkingDirectory(pathname);
fileName = new String(fileName.getBytes("utf-8"), "ISO-8859-1");
ftp.storeFile(fileName, inputStream);
inputStream.close();
}
}
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