package com.uas.service.Impl; import com.uas.service.DownloadFilePathService; import com.uas.util.BaseUtil; import com.uas.util.FileUtil; import com.uas.util.FtpUtil; import com.uas.util.JdbcUtil; import org.apache.commons.net.ftp.FTPClient; import org.apache.http.entity.ContentType; import org.springframework.mock.web.MockMultipartFile; import org.springframework.scheduling.annotation.Async; import org.springframework.stereotype.Service; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileInputStream; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.util.List; import java.util.Map; /** * @author koul * @email koul@usoftchina.com * @date 2020-11-27 10:13 */ @Service public class DownloadFilePathServiceImpl implements DownloadFilePathService{ @Override public void runDownloadFilePath(){ //香港华商龙-得尔达-回签单扫描回传 downloadFilePath("N_HUASL_T-DRD","N_HUASL_T"); //香港华商龙-富森-回签单扫描回传 downloadFilePath("N_HUASL_T-FS","N_HUASL_T"); //柏建控股-富森 downloadFilePath("N_BJKG_T-FS","N_BJKG_T"); } @SuppressWarnings("unchecked") @Override @Async("taskExecutor") public void downloadFilePath(String depot,String sob){ BaseUtil.getLogger().info("downloadFilePath():"+sob+"开始"); FTPClient client = null; Connection connect = null; try { Map servMap = JdbcUtil.getFtpConfigs(); Map ftpMap = (Map)servMap.get(depot); client = FtpUtil.connect(ftpMap); if(client!=null) { String fileName = ""; List files = FtpUtil.downloadFileByDirectory(client,ftpMap.get("file").toString()); if (files != null && files.size() > 0) { connect = JdbcUtil.getConnectBySob(sob); if (connect != null) { for (File file : files) { try { fileName += "," + file.getName(); Statement statement = connect.createStatement(); FileInputStream fileInputStream = new FileInputStream(file); MultipartFile file1 = new MockMultipartFile(file.getName(), file.getName(), ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream); boolean bol = FileUtil.fileUp(statement, file1, sob); if (bol) { client.rename(file.getName(), "bak/" + file.getName()); //移动处理完的文件到bak文件夹下 }else { //移动处理失败的文件到failed文件夹下 client.rename(file.getName(), "failed/" + file.getName()); } statement.close(); } catch (Exception e) { BaseUtil.getLogger().error("downloadFilePath():"+e.toString()); e.printStackTrace(); continue; } } } } if (!"".equals(fileName)) { BaseUtil.logDownload(fileName.substring(1), ftpMap.get("ip").toString(), ftpMap.get("file").toString()); } } BaseUtil.getLogger().info("downloadFilePath():"+sob+"结束"); } catch (Exception e) { BaseUtil.getLogger().error(e.toString()); e.printStackTrace(); }finally{ if(client!=null){ FtpUtil.closeFtpClient(client); client = null; } //把当前的连接关闭 try { if(connect!=null){ connect.close(); } } catch (SQLException e) { e.printStackTrace(); }finally{ connect = null; } } } }