Commit ac8131d4 authored by 吴琼's avatar 吴琼

导出修改

parent 2aa71ad1
......@@ -73,7 +73,7 @@ public class TransactionReceiptRecordServiceImpl implements ITransactionReceiptR
@Value("${kdsp-operation}")
private String kdspOperationUrl;
//@Value("${customer.contract.http}")
@Value("${pdf.quantgroup.cn.http}")
private String contractUrl;
@Value("${passportapi2.tjzimu.http}")
......@@ -89,6 +89,13 @@ public class TransactionReceiptRecordServiceImpl implements ITransactionReceiptR
private String password;
@Value("${customer.transaction.basePath}")
private String basePath;
@Value("${customer.transaction.local.file.uploadPath}")
private String uploadLocalPath;
@Value("${customer.transaction.local.file.downloadPath}")
private String downloadLocalPath;
@Value("${customer.transaction.local.file.downloadZipPath}")
private String downloadZipLocalPath;
@Override
public JsonResult<Page<TransactionReceiptRecordVO>> findRecordsByQuery(TransactionReceiptRecordQuery query) {
......@@ -262,7 +269,10 @@ public class TransactionReceiptRecordServiceImpl implements ITransactionReceiptR
return JsonResult.buildErrorStateResult("没有可以导出得数据");
}
List <Map<String,Object>> filePathList =new ArrayList<>();
log.info("[TransactionReceiptRecordServiceImpl.kdspOperationUrl 调用kdsp-op 接口begin]");
log.info("[TransactionReceiptRecordServiceImpl.kdspOperationUrl 调用kdsp-op 接口begin]", System.currentTimeMillis());
SFTPUtil sftpUtil = new SFTPUtil(username,password,host,port);
sftpUtil.login();
log.info("uploadFile | 成功连接ftp");
for(TransactionReceiptRecord record : transactionReceiptRecordList){
Integer userId = record.getUserId();
String orderNo = record.getOrderNo();
......@@ -311,24 +321,17 @@ public class TransactionReceiptRecordServiceImpl implements ITransactionReceiptR
continue;
}
//生成pdf
basePath= "D:/交易凭证";
//String basicPath="D:\\交易凭证0225";
String directory ="/"+record.getBatchNo()+record.getUserName()+"/";
FileToZip.mkdir(basePath+directory);
String directory =record.getBatchNo()+record.getUserName();
String pdfFileName = record.getOrderNo()+".pdf";
generatePDF(transactionReceiptVOList.get(0),basePath+directory+pdfFileName);
//保存到文件服务器上
/*SFTPUtil sftpUtil = new SFTPUtil(username,password,host,port);
sftpUtil.login();
log.info("uploadFile | 成功连接ftp");
File file = new File(pdfFileName);
InputStream is = new FileInputStream(file);
sftpUtil.upload(basePath,directory,pdfFileName,is);
sftpUtil.logout();*/
FileToZip.mkdir(uploadLocalPath+directory);
generatePDF(transactionReceiptVOList.get(0),uploadLocalPath+"/"+directory+"/"+pdfFileName);
File file1 = new File(uploadLocalPath+"/"+directory+"/"+pdfFileName);
InputStream inputStream = new FileInputStream(file1);
sftpUtil.upload(basePath,directory,pdfFileName,inputStream);
//将文件路径和useId放入list,进行签章
Map<String,Object> map =Maps.newHashMap();
map.put("userId",record.getUserId());
map.put("fileAddress",basePath+directory+pdfFileName);
map.put("fileAddress",basePath+directory+"/"+pdfFileName);
filePathList.add(map);
}
} catch (Exception e) {
......@@ -336,9 +339,11 @@ public class TransactionReceiptRecordServiceImpl implements ITransactionReceiptR
return JsonResult.buildErrorStateResult(e.getMessage());
}
}
log.info("[TransactionReceiptRecordServiceImpl.kdspOperationUrl 调用kdsp-op 接口end]");
sftpUtil.logout();
log.info("[TransactionReceiptRecordServiceImpl.kdspOperationUrl 调用kdsp-op 接口end]",System.currentTimeMillis());
//上传文件
//调用签章接口
/*String url = contractUrl + "/contract/batch/sign";
/* String url = contractUrl + "/contract/batch/sign";
Map<String, String> header = Maps.newHashMap();
header.put("Content-type", "application/json");
Map param = Maps.newHashMap();
......@@ -349,24 +354,81 @@ public class TransactionReceiptRecordServiceImpl implements ITransactionReceiptR
JSONObject json = JSONObject.parseObject(contractResult);
String jsonResult = json.getString("data");
String businessCode = json.getString("businessCode");*/
//下载签章文件
/* SFTPUtil sftpUtil = new SFTPUtil(username,password,host,port);
sftpUtil.login();
log.info("downloadFile | 成功连接ftp");
sftpUtil.download("","");*/
//转换成zip包
File file = ZipUtil.zip(basePath, basePath+".zip");//zip 包保存路径
//sftpUtil.logout();
for(Map<String,Object> map :filePathList){
SFTPUtil sftpUtil1 = new SFTPUtil(username,password,host,port);
sftpUtil1.login();
String filepath = (String) map.get("fileAddress");
int totallength =filepath.length();
//byte[] bytes = sftpUtil1.download("/lzshopkf/transaction/交易凭证/1张三/","1498571250795298816.pdf");
//getFile(bytes,downloadLocalPath+"交易凭证/1张三","1498571250795298816.pdf");//downloadLocalPath=D:/tmp1/
int length = filepath.lastIndexOf("/");
String fileName =filepath.substring(length+1,totallength);
filepath =filepath.substring(0,length+1);
byte[] bytes = sftpUtil1.download(filepath,fileName);
String subPath = filepath.substring(0,filepath.length()-1);
int length2 = subPath.lastIndexOf("/");
String directory = subPath.substring(length2+1,subPath.length());
FileToZip.mkdir(downloadLocalPath+"/"+directory);
getFile(bytes,downloadLocalPath+"/"+directory,fileName);
sftpUtil1.logout();
}
//byte[] bytes = sftpUtil.download("/lzshopkf/transaction/交易凭证/1张三","",username,password,host,port);
//getFile(bytes,"D:/0301/交易凭证/1张三","1498571250795298816.pdf");
//转换成zip包
//FileToZip.mkdir("D:/0301/交易凭证");
//File file = ZipUtil.zip("D:/0301/交易凭证", "D:/0301/交易凭证"+".zip");//zip 包保存路径
//FileToZip.mkdir(downloadLocalPath);
File file = ZipUtil.zip(downloadLocalPath, downloadLocalPath+".zip");//zip 包保存路径
FileInputStream is = new FileInputStream(file);
byte[] fileData = IOUtils.readNBytes(is, is.available());
//FileOutputStream fos1 = new FileOutputStream(new File("D:/交易凭证.zip"));
//FileToZip.toZip(basicPath, fos1,true);
//返回给前端
return this.outputFile(response,"交易凭证.zip",fileData ,100,20);
}
/**
*将字节数组转换为file
*/
/**
* 根据byte数组,生成文件
*/
public void getFile(byte[] bfile, String filePath,String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if(!dir.exists()&&dir.isDirectory()){//判断文件目录是否存在
dir.mkdirs();
}
FileToZip.mkdir(filePath);
file = new File(filePath+"/"+fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(bfile);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
}
}
public JsonResult outputFile(HttpServletResponse response, String fileName,
byte[] file,Integer successCount,Integer failCount){
if (ArrayUtils.isEmpty(file)) {
......
......@@ -9,8 +9,13 @@ import com.jcraft.jsch.SftpATTRS;
import com.jcraft.jsch.SftpException;
import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.Properties;
import java.util.Vector;
import lombok.SneakyThrows;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.IOUtils;
......@@ -60,12 +65,20 @@ public class SFTPUtil {
this.session.connect();
Channel channel = this.session.openChannel("sftp");
channel.connect();
Class<ChannelSftp> c = ChannelSftp.class;
Field f = c.getDeclaredField("server_version");
f.setAccessible(true);
// chSftp = (ChannelSftp) channel;
this.sftp = (ChannelSftp)channel;
f.set(sftp, 2);
sftp.setFilenameEncoding("GBK");
this.logger.info("pwd = [{}]", this.sftp.pwd());
} catch (JSchException var4) {
var4.printStackTrace();
} catch (SftpException var5) {
var5.printStackTrace();
}catch (Exception var5){
}
}
......@@ -82,38 +95,56 @@ public class SFTPUtil {
}
public void upload(String basePath, String directory, String sftpFileName, InputStream input) throws SftpException {
try {
this.sftp.cd(basePath);
this.sftp.cd(directory);
} catch (SftpException var14) {
String[] dirs = directory.split("/");
String tempPath = basePath;
/*try {
String[] dirs = basePath.split("/");
String[] var8 = dirs;
int var9 = dirs.length;
for(int var10 = 0; var10 < var9; ++var10) {
String dir = var8[var10];
if (null != dir && !"".equals(dir)) {
tempPath = tempPath + "/" + dir;
this.sftp.cd(dir);
}
}
if(this.isDirExist(basePath+"/"+directory)){
this.sftp.cd(directory);
}else{
this.sftp.mkdir(directory);
this.sftp.cd(directory);
}
} catch (SftpException var14) {
String tempPath = "/"+directory;
String[] dirs = tempPath.split("/");
String[] var8 = dirs;
int var9 = dirs.length;
for(int var10 = 0; var10 < var9; ++var10) {
String dir = var8[var10];
if (null != dir && !"".equals(dir)) {
//tempPath = tempPath + "/" + dir;
try {
this.sftp.cd(tempPath);
this.sftp.cd(dir);
} catch (SftpException var13) {
this.sftp.mkdir(tempPath);
this.sftp.cd(tempPath);
this.sftp.mkdir(dir);
this.sftp.cd(dir);
}
}
}
}
}*/
createDir(basePath+directory);
this.sftp.put(input, sftpFileName);
}
public void download(String directory, String downloadFile, String saveDirectory, String saveFile) throws SftpException, FileNotFoundException {
if (directory != null && !"".equals(directory)) {
this.sftp.cd(directory);
String[] dirs = directory.split("/");
String[] var8 = dirs;
int var9 = dirs.length;
for(int var10 = 0; var10 < var9; ++var10) {
String dir = var8[var10];
if (null != dir && !"".equals(dir)) {
this.sftp.cd(dir);
}
}
File file = new File(saveDirectory, saveFile);
this.sftp.get(downloadFile, new FileOutputStream(file));
}
......@@ -126,14 +157,80 @@ public class SFTPUtil {
this.sftp.get(downloadFile, new FileOutputStream(file));
}
public byte[] download(String directory, String downloadFile) throws SftpException, IOException {
if (directory != null && !"".equals(directory)) {
this.sftp.cd(directory);
public byte[] download(String directory, String downloadFile){
ByteArrayOutputStream fileOutputStream = new ByteArrayOutputStream();
String[] dirs = directory.split("/");
String[] var8 = dirs;
int var9 = dirs.length;
for(int var10 = 0; var10 < var9; ++var10) {
String dir = var8[var10];
if (null != dir && !"".equals(dir)) {
try {
this.sftp.cd(dir);
} catch (SftpException e) {
e.printStackTrace();
}
}
}
InputStream is = this.sftp.get(downloadFile);
byte[] fileData = IOUtils.readNBytes(is, is.available());
try {
this.sftp.get(downloadFile,fileOutputStream);
} catch (SftpException e) {
e.printStackTrace();
}
byte[] fileData = fileOutputStream.toByteArray();
//byte[] fileData= IOUtils.readNBytes(is, is.available());
return fileData;
}
public byte[] download(String directory,String downloadFile,
String userName,String password,String host,int port) throws SftpException, IOException {
if (StringUtils.isNotBlank(directory)){
downloadFile = directory.concat("/"+downloadFile);
}
ByteArrayOutputStream fileOutputStream = new ByteArrayOutputStream();
JSch jsch = null;
Session session = null;
Channel channel = null;
ChannelSftp channelSftp = null;
try {
jsch = new JSch();
session = jsch.getSession(userName, host,port);
session.setPassword(password.getBytes(Charset.forName("ISO-8859-1")));
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
//session.setTimeout(SO_TIME_OUT);
//log.info("连接 SFTP 服务器 " + baseInfo.getHost() + ":" + baseInfo.getPort());
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp) channel;
if (channelSftp == null || session == null || !session.isConnected() || !channelSftp.isConnected()) {
//log.info("SFTP channel 已经关闭");
throw new Exception("系统内部异常");
}
channelSftp.get(downloadFile, fileOutputStream);
//log.info("SFTP.client.下载成功 文件名={}", fileName);
return fileOutputStream.toByteArray();
} catch (Exception e) {
//log.error("SFTP.client.创建连接失败");
return null;
} finally {
if (channelSftp != null) {
channelSftp.disconnect();
}
if (channel != null) {
channel.disconnect();
}
if (session != null) {
session.disconnect();
}
}
}
public void delete(String directory, String deleteFile) throws SftpException {
......@@ -192,15 +289,17 @@ public class SFTPUtil {
}
public static void main(String[] args) {
SFTPUtil sftp = new SFTPUtil("lhphy", "47.105.211.91", 22223, "MIIEpQIBAAKCAQEAzCJdHIHjBobXq8S+Vq6IoAY7y3u98KQ1dyA6Ixpa83bw0xZJ0yA/vrcNxueyUesdL7DaG/4kruAdiCeLukYqlmJR+SsFcBUxnB4zYSrWx106CoLjPujvv1DvFgPacDGyp4+e7ifGt/RQlAPONQ+XbaHDHjr6R/0i/93Gm+/yn+/wQvsGUVOqE4kSvpY+36mcVyAgXf2f7Q5YvGsuA7Y7wx0tqU/M/7RI2cRYa3/PumMtBAvx/3Ny17is4I+8RRCq5B/UjOiwsSd391QWMpT0B3T5tO9stpMXLL1Vfm/O4lBUIWkEJsGBqjtgLp7LUpCzDyOGW7+Bi+kDTo5Z1huvfwIDAQABAoIBAQDHcX+lnaXRN6e6NDR/fnE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6fTw3i3rZBco6qqGwyxt/d6BCvEGUuh3s3pcKcNfdRr6AeZRiwtgk8uu7dcwZBdF6TnCQqzJefReyVlVFDpyt44dKVDtOlckwc+BcaM56NHCWrGEJAwxAzl84rAywxktvT63x+LfY8w5KUyPAhAoGBAPM0G6fzWeIpExIrxtUuLKLPzdYfB0L6P/8kHhxVMXRQDv3lEWwsTlle+eeAxEV+J+FEt5krbGTQr9EMFtsNBxu6F9KTixxtr684XwSh1ifrw0YAPu+47tR+Zu9P71vfo83+CO5NZA12q6DmQySzrMFNu781lzhKDYspXevpKlBtAoGBALPZUrCELWPUQ5yqeNsz+JfSWm60etuBrwTP5HeywQdji8iKP73L0dviL28sVbSy12H/1FAMdUy1z2CEgP1bocNSZ4YnBtmrtFGIdCNb1kb6tk44mNYtxzbe1L88eqFdICh1xlI4YytAVlh2f0rOmltkLY6ax/o+BPPC6TaUKmiBAoGASC1VYOzpelcN0ne3RfOrTDzRH3lQzGTh7E+2UxsVpkdxJg0N4e47MGKpGyylxEiuqpcoc9w7ec+IqylcL5Hx2jzDLx4OJHagtZVv8Gx9tixG6+vAdjjl7E38HS5WQcFJzQRAyktCeeoiLYIA3UvSN0GOXqDPAkFzPtX853APRwQ=");
SFTPUtil sftp = new SFTPUtil("lzshopkf","Q0lEkbamFm4aKZEi","172.27.8.47",22);
sftp.login();
try {
boolean has = sftp.isDirExist("./tst/20190819/account");
/*boolean has = sftp.isDirExist("/lzshopkf/transaction/交易凭证/2张三");
System.out.println(has);
if (!has) {
sftp.createDir("./tst/20190819/account");
}
sftp.createDir("/lzshopkf/transaction/交易凭证/2账务");
}*/
byte [] bytes = sftp.download("/lzshopkf/transaction","交易凭证");
} catch (Exception var3) {
var3.printStackTrace();
}
......
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