package com.kl.print.util;apache import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.SocketException;服务器 import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.commons.net.ftp.FTP; import org.apache.commons.net.ftp.FTPClient; import org.apache.commons.net.ftp.FTPReply;工具 /** * ftp上传下载工具类 * @author wangn * @date 2018年01月30日 * @version 1.0 */ public class FtpUtils { private final static Log logger = LogFactory.getLog(FtpUtils.class); private static FTPClient ftp; public static FTPClient getFTPClient(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort) { FTPClient ftpClient = new FTPClient(); try { ftpClient = new FTPClient(); ftpClient.connect(ftpHost, ftpPort);// 链接FTP服务器 ftpClient.login(ftpUserName, ftpPassword);// 登录FTP服务器 if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { logger.info("未链接到FTP,用户名或密码错误。"); ftpClient.disconnect(); } else { logger.info("FTP链接成功。"); } } catch (SocketException e) { e.printStackTrace(); logger.info("FTP的IP地址可能错误,请正确配置。"); } catch (IOException e) { e.printStackTrace(); logger.info("FTP的端口错误,请正确配置。"); } return ftpClient; } /** * Description: 向FTP服务器上传文件 * @param host FTP服务器hostname * @param port FTP服务器端口 * @param username FTP登陆帐号 * @param password FTP登陆密码 * @param filePath FTP服务器文件存放路径。 * @param filename 上传到FTP服务器上的文件名 * @param input 输入流 * @return 成功返回true,不然返回false */ public static boolean uploadFile(String host, int port, String username, String password, String filePath,String path, String filename, InputStream input) { boolean result = false; FTPClient ftp = new FTPClient(); try { int reply; ftp.connect(host, port);// 链接FTP服务器 // 若是采用默认端口,能够使用ftp.connect(host)的方式直接链接FTP服务器 ftp.enterLocalPassiveMode(); ftp.login(username, password);// 登陆 reply = ftp.getReplyCode(); if (!FTPReply.isPositiveCompletion(reply)) { ftp.disconnect(); return result; } //设置上传文件的类型为二进制类型 ftp.setFileType(FTP.BINARY_FILE_TYPE); ftp.cwd("/"); path = path.replaceAll("\\\\", "/"); String[] paths = path.split("/"); for (int i = 0; i < paths.length; i++) { String pathTemp = paths[i]; /* 建立的是父子目录 */ if(!ftp.makeDirectory(pathTemp)) { System.out.println("---建立目录失败--"); } ftp.changeWorkingDirectory(pathTemp); /* 进入到该目录 */ /*ftp.cwd(pathTemp); System.out.println("ftp.cwd(pathTemp)"+ ftp.cwd(pathTemp)); ftp.storeFile(filename, input);*/ } return ftp.storeFile(filename, input); //上传文件 // ftp.makeDirectory(path); /* if (!ftp.storeFile(filename, input)) { return result; } input.close(); ftp.logout(); result = true; */ } catch (IOException e) { e.printStackTrace(); } finally { if (ftp.isConnected()) { try { ftp.disconnect(); } catch (IOException ioe) { } } } return result; } /* * 从FTP服务器下载文件 * * @param ftpHost FTP IP地址 * * @param ftpPassword FTP用户名密码 * * @param ftpPort FTP端口 * * @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa * * @param localPath 下载到本地的位置 格式:H:/download * * @param fileName 文件名称 */ public static OutputStream downloadFtpFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, String ftpPath, String fileName) { FTPClient ftpClient = null; OutputStream os=null; try { ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort); ftpClient.setControlEncoding("UTF-8"); // 中文支持 ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); ftpClient.enterLocalPassiveMode(); ftpClient.changeWorkingDirectory(ftpPath); /*File localFile = new File(localPath + File.separatorChar + fileName); 判断目录是否存在,不存在的话建立(父目录) if(!localFile.getParentFile().exists()){ localFile.getParentFile().mkdirs(); }*/ os = new FileOutputStream(fileName); ftpClient.retrieveFile(fileName, os); os.close(); ftpClient.logout(); return os; } catch (FileNotFoundException e) { logger.error("没有找到" + ftpPath + "文件"); e.printStackTrace(); } catch (SocketException e) { logger.error("链接FTP失败."); e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); logger.error("文件读取错误。"); e.printStackTrace(); } return null; } spa /** * @param ip * @param port * @param userName * @param userPwd * @param path * @throws SocketException * @throws IOException function:链接到服务器 */ public static OutputStream downloadFile(String ip, int port, String userName, String userPwd, String path,String fileName) { ftp = new FTPClient(); ByteArrayOutputStream swapStream =null; try { // 链接 ftp.connect(ip, port); // 登陆 ftp.login(userName, userPwd); if (path != null && path.length() > 0) { //System.out.println("path:"+path); // 跳转到指定目录 ftp.changeWorkingDirectory(path); InputStream ins = null; try { ins = ftp.retrieveFileStream(fileName); // System.out.println("ins:"+ins.available()); swapStream= new ByteArrayOutputStream(); int ch; while ((ch = ins.read()) != -1) { swapStream.write(ch); } // System.out.println("swapStream:"+swapStream.size()); if(ins != null){ ins.close(); } // 主动调用一次getReply()把接下来的226消费掉. 这样作是能够解决这个返回null问题 ftp.getReply(); } catch (IOException e) { e.printStackTrace(); } } } catch (SocketException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { FtpUtils.closeServer(); } return swapStream; }.net /** * @throws IOException function:关闭链接 */ public static void closeServer() { if (ftp.isConnected()) { try { ftp.logout(); ftp.disconnect(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args) throws Exception { /*String ftpHost = "192.168.0.85"; String ftpUserName = "icbc"; String ftpPassword = "evhAB6#5"; int ftpPort = 21; String ftpPath = "/data/ftp/icbc/2017/02/10"; //String localPath = "D:/dd/bb"; String fileName="1708041358198180SW01.pdf";*/ //读取文件的名称,能够经过给定字段进行查询文件 //FtpUtils.downloadFtpFile(ftpHost, ftpUserName, ftpPassword, ftpPort, ftpPath, fileName); // File localFile = new File(localPath + File.separatorChar + fileName); /* 判断目录是否存在,不存在的话建立(父目录) */ String fileName="1708041358198180SW03.pdf"; String ip = "192.168.0.85"; // 服务器IP地址 String userName = "*******"; // 用户名 String userPwd = "*********"; // 密码 int port = 21; // 端口号 String path = "/2018/02/12"; // 读取文件的存放目录 OutputStream outputStream= FtpUtils.downloadFile(ip, port, userName, userPwd, path,fileName); System.out.println("文件内容为:"+outputStream.toString()); } } ip |