图纸查看

master
philip 3 years ago
parent 5c19d31d85
commit 5afde21345

@ -105,5 +105,9 @@
<groupId>org.apache.axis</groupId>
<artifactId>axis-saaj</artifactId>
</dependency>
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
</dependency>
</dependencies>
</project>

@ -0,0 +1,115 @@
package com.foreverwin.mesnac.common.controller;
import com.foreverwin.mesnac.common.ftp.CappFtpClient;
import com.foreverwin.mesnac.common.service.FileService;
import com.foreverwin.mesnac.meapi.util.StringUtils;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.R;
import org.apache.commons.net.ftp.FTPClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
/**
*
* @author roc_li
* @since 2020-11-03
*/
@Controller
@Component
@RequestMapping("/files")
public class FileController {
private final Logger logger = LoggerFactory.getLogger(FileController.class);
@Autowired
private FileService fileService;
@Autowired
public CappFtpClient ftpClient;
/**
*
* @return
*/
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/getPdfPath")
public R getPdfPath(String sfc) {
try {
if(StringUtils.isBlank(sfc)){
throw new BaseException("产品条码不能为空!");
}
return R.ok(fileService.getFilePaths(sfc));
}catch (Exception e){
return R.failed("获取图纸路径失败:"+e.getMessage());
}
}
/**
*
* @return
*/
@ResponseBody
@RequestMapping(method = RequestMethod.GET, value = "/getPdfNew")
public R getPdfNew(HttpServletRequest request, HttpServletResponse response) {
InputStream in = null;
OutputStream out = null;
String path = request.getParameter("Path");
try {
//服务器发布
String coderPath = new String(path.getBytes("ISO8859_1"));
String newPath = new String(coderPath.getBytes("GBK"), FTPClient.DEFAULT_CONTROL_ENCODING);
in = ftpClient.getFtp(newPath);
if(null == in){
in = ftpClient.getFtp(new String(coderPath.getBytes("UTF-8"), FTPClient.DEFAULT_CONTROL_ENCODING));
}
//本地测试
/*String newPath = new String(path.getBytes("GBK"), FTPClient.DEFAULT_CONTROL_ENCODING);
in = plmFtpClient.getFtp(newPath);
if(null == in){
in = plmFtpClient.getFtp(new String(path.getBytes("UTF-8"), FTPClient.DEFAULT_CONTROL_ENCODING));
}*/
if(null == in){
throw new BaseException("获取FTP文件路径为:【"+path+"】的文件流失败!");
}
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len=in.read(buffer)) != -1 ){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();
out = response.getOutputStream();
out.write(data);
out.flush();
}catch (Exception e){
return R.failed("获取CAPP文件失败:文件路径为"+path+":"+e.getMessage());
}finally {
try {
if(null != out){
out.close();
}
if(null != in){
in.close();
}
}catch (Exception e1){
return R.failed("获取CAPP文件失败:关闭文件流处理异常"+e1.getMessage());
}
}
return R.ok(null,"获取CAPP文件成功!");
}
}

@ -0,0 +1,337 @@
package com.foreverwin.mesnac.common.ftp;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPClientConfig;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import java.io.*;
import java.net.SocketException;
import java.util.regex.Matcher;
@Component
@ConditionalOnProperty( prefix = "cappftp", name = { "server", "port", "username", "password", "uploadDir", "downloadDir" }, matchIfMissing = false )
public class CappFtpClient {
private FTPClient ftp;
@Value("${cappftp.server}")
private String address;
@Value("${cappftp.port}")
private int port;
@Value("${cappftp.username}")
private String userName;
@Value("${cappftp.password}")
private String password;
@Value("${cappftp.uploadDir}")
private String uploadDir;
@Value("${cappftp.downloadDir}")
private String downloadDir;
/**Ftp协议字符编码*/
private String serverCharset = "ISO-8859-1";
/**本体字符编码*/
private String localCharset = "GBK";
public FTPClient connect() throws Exception {
try {
//getFtpConf(ftpConfig);
ftp = new FTPClient();
ftp.setConnectTimeout(10000);
ftp.connect(address, port);
//2021/1/8 roc 修改读取文件阻塞时间
//ftp.setSoTimeout(10 * 60 * 1000);
ftp.setSoTimeout(30 * 1000);
ftp.login(userName, password);
//设置linux环境
FTPClientConfig conf = new FTPClientConfig( FTPClientConfig.SYST_UNIX);
ftp.configure(conf);
//ftp.setControlEncoding(localCharset); // 中文支持
//设置访问被动模式
ftp.setRemoteVerificationEnabled(false);
ftp.enterLocalPassiveMode();
ftp.setFileType(ftp.BINARY_FILE_TYPE);// 设置传输模式
return ftp;
}
catch (SocketException e) {
throw new Exception("登录ftp服务器 " + address + " 失败,连接超时!", e);
}
catch (IOException e) {
throw new Exception("登录ftp服务器 " + address + " 失败FTP服务器无法打开", e);
}
catch (Exception e) {
throw new Exception("访问FTP服务器【" + address + "】失败!", e);
}
}
/**
*
*
* @param file
* @throws Exception
*/
public void upload( File file ) throws Exception{
connect();
_upload( file );
closeConnect();
}
/**
*
*
* @param remotePath
* @param file
* @throws IOException
*/
public void upload( String remotePath, File file ) throws Exception{
connect();
_upload( remotePath, file );
closeConnect();
}
/**
*
*
* @param remotePath
* @param localPath
* @throws Exception
*/
public void download( String remotePath, String localPath ) throws Exception{
connect();
_download( remotePath, localPath );
closeConnect();
}
public InputStream downLoadFile(String filePath, String remoteFileName) throws Exception {
InputStream in = null;
// 下载文件
try {
connect();
ftp.setControlEncoding("UTF-8"); // 中文支持
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
remoteFileName = filePath + remoteFileName;
String remote = new String(remoteFileName.getBytes(localCharset),serverCharset);
in = ftp.retrieveFileStream(remote);
if (null == in || ftp.getReplyCode() == 550) {
throw new Exception("文件不存在");
}
return in;
}
catch (Exception e) {
throw e;
}
finally {
try {
if (ftp != null || ftp.isConnected()) {
//ftpClient.logout();
ftp.disconnect();
ftp = null;
}
}
catch (Exception e) {
throw e;
}
}
}
/**
* ftp
*
* @param remoteFile
* @param localFile
* @throws Exception
*/
public void download( String remoteFile, File localFile ) throws Exception{
connect();
_download( remoteFile, localFile );
closeConnect();
}
/**
*
*
* @param remotePath
* @throws IOException
*/
public void deleteFile( String remotePath ) throws Exception{
connect();
_deleteFile( remotePath );
closeConnect();
}
public void deleteFile1( String filePath ) throws Exception{
connect();
boolean b =ftp.deleteFile( filePath );
closeConnect();
}
public void mkdirs( String remotePath ) throws Exception {
connect();
String[] paths = remotePath.split( Matcher.quoteReplacement(File.separator) );
for( String p : paths ){
ftp.makeDirectory( p );
ftp.changeWorkingDirectory( p );
}
closeConnect();
}
private void _upload( File file ) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+ File.separator +files[i] );
_upload(file1);
}
ftp.changeToParentDirectory();
}else{
FileInputStream input = new FileInputStream(file);
ftp.storeFile(file.getName(), input);
input.close();
}
}
private void _upload( String remotePath, File file ) throws Exception {
String[] paths = remotePath.split( Matcher.quoteReplacement(File.separator) );
for( String p : paths ){
ftp.makeDirectory( p );
ftp.changeWorkingDirectory( p );
}
_upload( file );
}
private void _download( String remotePath, String localPath ) throws Exception {
FTPFile[] ftpFiles = ftp.listFiles( remotePath );
if( ftpFiles.length > 0 ){
for( FTPFile ftpFile : ftpFiles ){
if( ftpFile.isDirectory() ){
_download( remotePath + File.separator + ftpFile.getName(), localPath + File.separator + ftpFile.getName() );
}else{
File localDir = new File( localPath );
localDir.mkdirs();
File localFile = new File( localPath + File.separator + ftpFile.getName() );
localFile.createNewFile();
FileOutputStream fos = new FileOutputStream( localFile );
ftp.retrieveFile( remotePath + File.separator + ftpFile.getName(), fos );
fos.flush();
fos.close();
}
}
}
}
private void _download( String remoteFile, File localFile ) throws Exception {
localFile.createNewFile();
FileOutputStream fos = new FileOutputStream( localFile );
ftp.retrieveFile( remoteFile, fos );
fos.flush();
fos.close();
}
private void _deleteFile( String remotePath ) throws Exception {
FTPFile[] ftpFiles = ftp.listFiles( remotePath );
boolean b = false;
if( ftpFiles.length > 0 ){
for( FTPFile ftpFile : ftpFiles ){
if( ftpFile.isDirectory() ){
_deleteFile( remotePath + File.separator + ftpFile.getName() );
b = ftp.removeDirectory( remotePath + File.separator + ftpFile.getName() );
}else{
b= ftp.deleteFile( remotePath + File.separator + ftpFile.getName() );
}
}
}
}
private String convertFtpCharset( String fileName ){
try {
return new String( fileName.getBytes( localCharset ), serverCharset );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fileName;
}
private String convertLocalCharset( String fileName ){
try {
return new String( fileName.getBytes( serverCharset ), localCharset );
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fileName;
}
private void closeConnect() {
try{
if( ftp != null && ftp.isConnected() ){
ftp.logout();
ftp.disconnect();
}
}catch (IOException e) {
e.printStackTrace();
}
}
public String getUploadDir(){
return uploadDir;
}
public String getDownloadDir(){
return downloadDir;
}
/**
*
* @param path
* @return
* @throws Exception
*/
public InputStream getFtp(String path) throws Exception {
connect();
InputStream in = ftp.retrieveFileStream(path);
return in;
}
/**
*
*
* @param
*/
public boolean changeWorkingDirectory(String fDir) throws Exception {
try {
ftp.changeToParentDirectory();
return ftp.changeWorkingDirectory(fDir);
}
catch (Exception ioe) {
throw ioe;
}
}
public FTPFile[] listFiles(String remotePath) throws IOException {
FTPFile[] ftpFiles = ftp.listFiles( remotePath );
return ftpFiles;
}
}

@ -0,0 +1,11 @@
package com.foreverwin.mesnac.common.service;
import java.util.Map;
/**
* @author roc li
*/
public interface FileService {
Map<String,String> getFilePaths(String sfc) throws Exception;
}

@ -0,0 +1,70 @@
package com.foreverwin.mesnac.common.service.impl;
import com.foreverwin.mesnac.common.enums.HandleEnum;
import com.foreverwin.mesnac.common.ftp.CappFtpClient;
import com.foreverwin.mesnac.common.service.FileService;
import com.foreverwin.mesnac.common.util.ExceptionUtil;
import com.foreverwin.mesnac.common.util.StringUtil;
import com.foreverwin.mesnac.meapi.model.Sfc;
import com.foreverwin.mesnac.meapi.service.SfcService;
import com.foreverwin.modular.core.exception.BaseException;
import com.foreverwin.modular.core.util.CommonMethods;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
@Service
public class FileServiceImpl implements FileService {
@Value("${cappftp.server}")
private String address;
@Autowired
private SfcService sfcService;
@Autowired
private CappFtpClient cappFtpClient;
@Override
public Map<String, String> getFilePaths(String sfc) throws IOException {
Sfc sfcServiceById = sfcService.getById(HandleEnum.SFC.getHandle(CommonMethods.getSite(), sfc));
if (sfcServiceById==null){
throw new BaseException("未找到产品条码"+sfc);
}
String itemBo = sfcServiceById.getItemBo();
String path="/"+ StringUtil.trimHandle(itemBo)+"_"+StringUtil.trimRevision(itemBo)+"/";
FTPClient connect = null;
Map<String,String> pathMap = new HashMap<>();
try {
connect = cappFtpClient.connect();
if(path.startsWith("/")&&path.endsWith("/")){
String directory = path;
//更换目录到当前目录
connect.changeWorkingDirectory(directory);
FTPFile[] files = connect.listFiles(directory);
if(files!=null){
for (int i = 0; i < files.length; i++) {
if(files[i].isFile()){
String filename=files[i].getName();
pathMap.put(filename,address+filename);
}
}
}
}
connect.disconnect();
}catch (Exception e){
ExceptionUtil.throwException(e);
}finally {
if (connect!=null){
connect.disconnect();
}
}
return pathMap;
}
}

@ -53,4 +53,12 @@ quartz:
exportDocument:
filePath: /usr/word/
outputPath: /usr/word/outputWord/
template: SFC_SCRAP_TEMPLATE.ftl
template: SFC_SCRAP_TEMPLATE.ftl
cappftp:
server: 172.16.251.70
port: 9003
username: Administrator
password: cappnew@123
uploadDir:
downloadDir:

@ -52,7 +52,7 @@ activemq:
password: admin
user: admin
pool:
enabled: true
enabled: false
max-connections: 10
#WebService
@ -61,9 +61,9 @@ ws:
server: srm.mesnac.com
port: 8000
user: mes001
pwd: mesprd
pwd: mesprd1
valid: Y
enabled: false
enabled: true
#quartz任务启用
quartz:
@ -116,4 +116,12 @@ activeMq:
exportDocument:
filePath: /Users/zhaojiawei/Desktop/青岛项目后台/mesnac6.biz/production/src/main/resources/
outputPath: /Users/zhaojiawei/Desktop/
template: SFC_SCRAP_TEMPLATE.ftl
template: SFC_SCRAP_TEMPLATE.ftl
cappftp:
server: 172.16.251.70
port: 9003
username: Administrator
password: cappnew@123
uploadDir:
downloadDir:

@ -54,4 +54,12 @@ print:
exportDocument:
filePath: /usr/word/
outputPath: /usr/word/outputWord/
template: SFC_SCRAP_TEMPLATE.ftl
template: SFC_SCRAP_TEMPLATE.ftl
cappftp:
server: 172.16.251.70
port: 9003
username: Administrator
password: cappnew@123
uploadDir:
downloadDir:

@ -30,10 +30,10 @@ ws:
erp:
server: 172.16.251.223
port: 8000
user: mesd001
user: mesd002
pwd: a123456
valid: N
enabled: false
enabled: true
#ftp
ftp:
@ -57,4 +57,12 @@ activeMq:
exportDocument:
filePath: /usr/word/
outputPath: /usr/word/outputWord/
template: SFC_SCRAP_TEMPLATE.ftl
template: SFC_SCRAP_TEMPLATE.ftl
cappftp:
server: 172.16.251.70
port: 9003
username: Administrator
password: cappnew@123
uploadDir:
downloadDir:
Loading…
Cancel
Save