|  |  |  | 
|---|
|  |  |  | package com.doumee.core.utils; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.io.*; | 
|---|
|  |  |  | import java.net.MalformedURLException; | 
|---|
|  |  |  | import java.util.Date; | 
|---|
|  |  |  | import java.util.UUID; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | 
|---|
|  |  |  | import org.apache.http.impl.client.CloseableHttpClient; | 
|---|
|  |  |  | import org.apache.http.impl.client.HttpClientBuilder; | 
|---|
|  |  |  | import org.apache.http.util.EntityUtils; | 
|---|
|  |  |  | import org.apache.xpath.operations.Bool; | 
|---|
|  |  |  | import sun.misc.BASE64Encoder; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** */ | 
|---|
|  |  |  | 
|---|
|  |  |  | FtpUtil.password =password; | 
|---|
|  |  |  | connect(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * 初始化ftp服务器 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean connect() { | 
|---|
|  |  |  | boolean flag = false; | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | System.out.println("connecting...ftp服务器:"+this.hostname+":"+this.port); | 
|---|
|  |  |  | ftpClient.setRemoteVerificationEnabled(false); | 
|---|
|  |  |  | ftpClient.connect(hostname, port); //连接ftp服务器 | 
|---|
|  |  |  | ftpClient.login(username, password); //登录ftp服务器 | 
|---|
|  |  |  | //            if (FTPReply.isPositiveCompletion(ftpClient.sendCommand("OPTS UTF8", "ON"))) { | 
|---|
|  |  |  | //               LOCAL_CHARSET = "UTF-8"; | 
|---|
|  |  |  | //         } | 
|---|
|  |  |  | ftpClient.setControlEncoding("GBK"); | 
|---|
|  |  |  | ftpClient.enterLocalPassiveMode(); | 
|---|
|  |  |  | int replyCode = ftpClient.getReplyCode(); //是否成功登录服务器 | 
|---|
|  |  |  | if(!FTPReply.isPositiveCompletion(replyCode)){ | 
|---|
|  |  |  | log.error("connect failed...ftp服务器:"+this.hostname+":"+this.port+"返回码:"+replyCode); | 
|---|
|  |  |  | }else { | 
|---|
|  |  |  | flag = true; | 
|---|
|  |  |  | log.info("connect successful...ftp服务器:"+this.hostname+":"+this.port+"返回码:"+replyCode); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | }catch (MalformedURLException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | log.error("connect exception...ftp服务器:"+this.hostname+":"+this.port+   e.getMessage()); | 
|---|
|  |  |  | }catch (IOException e) { | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | log.error("connect exception...ftp服务器:"+this.hostname+":"+this.port+   e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return flag; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** */ | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | 
|---|
|  |  |  | * @return 是否连接成功 | 
|---|
|  |  |  | * @throws IOException | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | public boolean connect() throws IOException { | 
|---|
|  |  |  | public boolean connect2() throws IOException { | 
|---|
|  |  |  | ftpClient.connect(hostname, port); | 
|---|
|  |  |  | ftpClient.setControlEncoding("GBK"); | 
|---|
|  |  |  | if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | public boolean uploadInputstream(InputStream inputStream, String remote)  { | 
|---|
|  |  |  | // 设置PassiveMode传输 | 
|---|
|  |  |  | Date d1 = new Date(); | 
|---|
|  |  |  | log.error("上传文件成功=============开始========="+DateUtil.getPlusTime2(d1)); | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | ftpClient.enterLocalPassiveMode(); | 
|---|
|  |  |  |  | 
|---|
|  |  |  | // 设置以二进制流的方式传输 | 
|---|
|  |  |  | ftpClient.setFileType(FTP.BINARY_FILE_TYPE); | 
|---|
|  |  |  | ftpClient.setControlEncoding("GBK"); | 
|---|
|  |  |  | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | log.error("上传文件失败======================="+remote); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | Date d2= new Date(); | 
|---|
|  |  |  | log.error("上传文件成功=============结束========="+DateUtil.getPlusTime2(d2) +"耗时毫秒:"+( (d2.getTime()-d1.getTime())  )); | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public boolean uploadInputstreamBatch(InputStream inputStream, String remote, Boolean close , Integer index )  { | 
|---|
|  |  |  | // 设置PassiveMode传输 | 
|---|
|  |  |  | try { | 
|---|
|  |  |  | ftpClient.enterLocalPassiveMode(); | 
|---|
|  |  |  | // 设置以二进制流的方式传输 | 
|---|
|  |  |  | ftpClient.setFileType(FTP.BINARY_FILE_TYPE); | 
|---|
|  |  |  | ftpClient.setControlEncoding("GBK"); | 
|---|
|  |  |  | // 对远程目录的处理 | 
|---|
|  |  |  | String remoteFileName = remote; | 
|---|
|  |  |  | if (remote.contains("/")) { | 
|---|
|  |  |  | remoteFileName = remote.substring(remote.lastIndexOf("/") + 1); | 
|---|
|  |  |  | // 创建服务器远程目录结构,创建失败直接返回 | 
|---|
|  |  |  | if (Constants.equalsInteger(index,Constants.ONE) && StringUtils.equals(CreateDirecroty(remote, ftpClient), "2")) { | 
|---|
|  |  |  | log.error("创建ftp目录失败======================="+remote); | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|
|  |  |  | boolean result = ftpClient.storeFile(remoteFileName, inputStream); | 
|---|
|  |  |  | inputStream.close(); | 
|---|
|  |  |  | if(close){ | 
|---|
|  |  |  | ftpClient.logout(); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | if(result){ | 
|---|
|  |  |  | log.info("上传文件成功======================"+remote); | 
|---|
|  |  |  | }else{ | 
|---|
|  |  |  | log.error("上传文件失败======================="+remote); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return result; | 
|---|
|  |  |  | }catch (Exception e){ | 
|---|
|  |  |  | e.printStackTrace(); | 
|---|
|  |  |  | log.error("上传文件失败======================="+remote); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return false; | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | public int getNumFromStr(String str,char searchstr) { | 
|---|
|  |  |  | int count = 0; | 
|---|
|  |  |  | char[] charArray = str.toCharArray(); | 
|---|