| | |
| | | package com.java110.job.util; |
| | | |
| | | import com.java110.common.util.SpringBeanInvoker; |
| | | import com.java110.job.dao.IHccFtpFileDAO; |
| | | import com.java110.job.dao.IHcFtpFileDAO; |
| | | import com.java110.job.model.FtpTaskLogDetail; |
| | | import com.java110.job.smo.IHcFtpFileSMO; |
| | | import com.java110.job.smo.impl.HcFtpFileSMOImpl; |
| | | import org.apache.commons.net.PrintCommandListener; |
| | | import org.apache.commons.net.ftp.FTP; |
| | | import org.apache.commons.net.ftp.FTPClient; |
| | | import org.apache.commons.net.ftp.FTPFile; |
| | | import org.apache.commons.net.ftp.FTPReply; |
| | | import org.apache.log4j.Logger; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.slf4j.Logger; |
| | | import org.slf4j.LoggerFactory; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.io.*; |
| | | import java.net.SocketException; |
| | | import java.util.*; |
| | |
| | | //--------------------------------------------------------------------- |
| | | // Instance data |
| | | //--------------------------------------------------------------------- |
| | | /** logger */ |
| | | protected final Logger log = Logger.getLogger(getClass()); |
| | | /** logger */ |
| | | private static final Logger logger = LoggerFactory.getLogger(HcFtpFileSMOImpl.class); |
| | | private static ThreadLocal<FTPClient> ftpClientThreadLocal = new ThreadLocal<FTPClient>(); |
| | | private String host; |
| | | private int port; |
| | |
| | | private String localfilename; //本地保存的文件名 |
| | | private String transferflag; //传输类型 |
| | | private Map threadpara; //线程执行参数 |
| | | @Autowired |
| | | private IHccFtpFileDAO prvncFtpFileDAO; |
| | | @Autowired |
| | | private IHcFtpFileSMO prvncFtpFileSMO; |
| | | @Resource(name = "hcFtpFileDAOImpl") |
| | | private IHcFtpFileDAO hcFtpFileDAOImpl; |
| | | @Resource(name = "hcFtpFileSMOImpl") |
| | | private IHcFtpFileSMO hcFtpFileSMOImpl; |
| | | public static String SPECIAL_SINGLESPLIT_a="+*|"; //字符串分隔符特殊字符需要用加"\\"来转译 |
| | | //传输类型(“U"是上传 upload ”D“是下载 download) |
| | | public static class TransferType{ |
| | |
| | | init(); |
| | | } |
| | | private void init(){ |
| | | this.prvncFtpFileDAO = ((IHccFtpFileDAO) SpringBeanInvoker.getBean("provInner.PrvncFtpFileDAO")); |
| | | this.prvncFtpFileSMO=((IHcFtpFileSMO) SpringBeanInvoker.getBean("provInner.PrvncFtpFileSMO")); |
| | | |
| | | } |
| | | public Map call() throws Exception { |
| | | Map result=null; |
| | |
| | | //处理传输 |
| | | input = new FileInputStream(localAbsoluteFile); |
| | | getFTPClient().storeFile(remoteAbsoluteFile, input); |
| | | log.debug("put " + localAbsoluteFile); |
| | | logger.debug("put " + localAbsoluteFile); |
| | | return true; |
| | | } catch (FileNotFoundException e) { |
| | | throw new Exception("local file not found.", e); |
| | |
| | | } |
| | | } catch (IOException e) { |
| | | result.put("flag", "0"); |
| | | log.debug(" not upload !!! "); |
| | | log.debug("uploadFileFrom IOException : {}", e); |
| | | logger.debug(" not upload !!! "); |
| | | logger.debug("uploadFileFrom IOException : {}", e); |
| | | throw e; |
| | | } catch (Exception e1) { |
| | | result.put("flag", "0"); |
| | |
| | | tablename=tablename.trim(); |
| | | tablename=tablename.substring(tablename.lastIndexOf(" "), tablename.length()); |
| | | //查字段的数据类型 |
| | | List<Map> collist=prvncFtpFileDAO.queryTableColInfo(tablename.trim(), colnames.trim()); |
| | | List<Map> collist=hcFtpFileDAOImpl.queryTableColInfo(tablename.trim(), colnames.trim()); |
| | | RandomAccessFile raf = new RandomAccessFile(localfilename, "r"); |
| | | long filelen=raf.length(); |
| | | if (filelen != 0L) { |
| | |
| | | logdetail.setRemark((String)threadpara.get("remark")); |
| | | logdetail.setServerfilename((String)threadpara.get("serverfilename")); |
| | | logdetail.setLocalfilename((String)threadpara.get("localfilename")); |
| | | prvncFtpFileSMO.saveTaskRunDetailLog(logdetail); |
| | | hcFtpFileSMOImpl.saveTaskRunDetailLog(logdetail); |
| | | } |
| | | public void saveStringToData(String data, String insertsql, String colnames, List<Map> collist){ |
| | | //拼接数据保存的SQL语句,日期型的值要格式化下 |
| | |
| | | } |
| | | insertsql=getInsertTableSQL(colnames,collist,datarows,insertsql); |
| | | //执行SQL语句,保存数据 |
| | | prvncFtpFileSMO.insertFileData2Table(insertsql); |
| | | hcFtpFileSMOImpl.insertFileData2Table(insertsql); |
| | | } |
| | | /** |
| | | * 拼接数据保存的SQL语句,日期型的值要格式化下 |
| | |
| | | if(end>=filelength){ |
| | | end=filelength-1; |
| | | } |
| | | log.debug("线程:" + tnum +"从"+start+"开始上传到"+end+ "结束,共需上传:"+(end-start+1)); |
| | | logger.debug("线程:" + tnum +"从"+start+"开始上传到"+end+ "结束,共需上传:"+(end-start+1)); |
| | | accessFile = new RandomAccessFile(file, "rwd"); |
| | | ftpClient = getFTPClient(); |
| | | ftpClient.setRestartOffset(start); |
| | |
| | | while ((len =accessFile.read(data)) != -1 && downedlength < threadDownSize) { |
| | | downedlength = downedlength + len; |
| | | out.write(data,0,len); |
| | | log.debug("线程:" + tnum + "已上传:"+downedlength); |
| | | logger.debug("线程:" + tnum + "已上传:"+downedlength); |
| | | if(downedlength==threadDownSize){ |
| | | break; |
| | | } |
| | |
| | | data = new byte[Integer.valueOf(Long.toString(threadDownSize-downedlength))]; |
| | | } |
| | | } |
| | | log.debug("线程:" + tnum + "上传完成!共上传"+downedlength); |
| | | logger.debug("线程:" + tnum + "上传完成!共上传"+downedlength); |
| | | } catch (IOException e) { |
| | | flag = "0"; |
| | | log.debug(" not upload !!! "); |
| | | log.debug("uploadFileFrom IOException : {}", e); |
| | | logger.debug(" not upload !!! "); |
| | | logger.debug("uploadFileFrom IOException : {}", e); |
| | | } catch (Exception e1) { |
| | | // TODO Auto-generated catch block |
| | | flag="0"; |
| | |
| | | if(end>=filelength){ |
| | | end=filelength-1; |
| | | } |
| | | log.debug("线程:" + tnum +"从"+start+"开始下载到"+end+ "结束,共需下载:"+(end-start+1)); |
| | | logger.debug("线程:" + tnum +"从"+start+"开始下载到"+end+ "结束,共需下载:"+(end-start+1)); |
| | | ftpClient = getFTPClient(); |
| | | accessFile = new RandomAccessFile(file, "rwd"); |
| | | ftpClient.setRestartOffset(start); |
| | |
| | | while ((len = in.read(data)) != -1 && downedlength < threadDownSize) { |
| | | downedlength = downedlength + len; |
| | | accessFile.write(data, 0, len); |
| | | log.debug("线程:" + tnum + "已下载:"+downedlength); |
| | | logger.debug("线程:" + tnum + "已下载:"+downedlength); |
| | | if(downedlength==threadDownSize){ |
| | | break; |
| | | } |
| | |
| | | data = new byte[Integer.valueOf(Long.toString(threadDownSize-downedlength))]; |
| | | } |
| | | } |
| | | log.debug("线程:" + tnum + "下载完成!共下载"+downedlength); |
| | | logger.debug("线程:" + tnum + "下载完成!共下载"+downedlength); |
| | | } catch (IOException e1) { |
| | | // TODO Auto-generated catch block |
| | | flag="0"; |
| | | log.error("下载线程异常", e1); |
| | | logger.error("下载线程异常", e1); |
| | | } catch (Exception e1) { |
| | | // TODO Auto-generated catch block |
| | | flag="0"; |
| | | log.error("下载线程异常", e1); |
| | | logger.error("下载线程异常", e1); |
| | | } finally { |
| | | try { |
| | | // ftpClient.logout(); |