| File was renamed from src/main/java/org/springblade/common/utils/FtpUtil.java |
| | |
| | | package org.springblade.common.utils; |
| | | package org.springblade.modules.FTP; |
| | | |
| | | import org.apache.commons.logging.Log; |
| | | import org.apache.commons.logging.LogFactory; |
| | |
| | | |
| | | /** |
| | | * ftp工具类 |
| | | * |
| | | * @author lijj |
| | | */ |
| | | |
| | | public class FtpUtil { |
| | | |
| | | private final static Log logger = LogFactory.getLog(FtpUtil.class); |
| | | /** 本地字符编码 */ |
| | | /** |
| | | * 本地字符编码 |
| | | */ |
| | | private static String LOCAL_CHARSET = "GBK"; |
| | | |
| | | // FTP协议里面,规定文件名编码为iso-8859-1 |
| | |
| | | /** |
| | | * 获取FTPClient对象 |
| | | * |
| | | * @param ftpHost |
| | | * FTP主机服务器 |
| | | * |
| | | * @param ftpPassword |
| | | * FTP 登录密码 |
| | | * |
| | | * @param ftpUserName |
| | | * FTP登录用户名 |
| | | * |
| | | * @param ftpPort |
| | | * FTP端口 默认为21 |
| | | * |
| | | * @param ftpHost FTP主机服务器 |
| | | * @param ftpPassword FTP 登录密码 |
| | | * @param ftpUserName FTP登录用户名 |
| | | * @param ftpPort FTP端口 默认为21 |
| | | * @return |
| | | */ |
| | | public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) { |
| | |
| | | /** |
| | | * 从FTP服务器下载文件 |
| | | * |
| | | * @param ftpHost FTP IP地址 |
| | | * |
| | | * @param ftpHost FTP IP地址 |
| | | * @param ftpUserName FTP 用户名 |
| | | * |
| | | * @param ftpPassword FTP用户名密码 |
| | | * |
| | | * @param ftpPort FTP端口 |
| | | * |
| | | * @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa |
| | | * |
| | | * @param localPath 下载到本地的位置 格式:H:/download |
| | | * |
| | | * @param fileName 文件名称 |
| | | * @param ftpPort FTP端口 |
| | | * @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa |
| | | * @param localPath 下载到本地的位置 格式:H:/download |
| | | * @param fileName 文件名称 |
| | | */ |
| | | public static void downloadFtpFile(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort, |
| | | String ftpPath, String localPath, String fileName) { |
| | |
| | | InputStream retrieveFileStream = ftpClient.retrieveFileStream(fileNameTemp); |
| | | |
| | | // 第一种方式下载文件(推荐) |
| | | //File localFile = new File(localPath + File.separatorChar + fileName); |
| | | //OutputStream os = new FileOutputStream(localFile); |
| | | //ftpClient.retrieveFile(fileName, os); os.close(); |
| | | //File localFile = new File(localPath + File.separatorChar + fileName); |
| | | //OutputStream os = new FileOutputStream(localFile); |
| | | //ftpClient.retrieveFile(fileName, os); os.close(); |
| | | |
| | | |
| | | // 第二种方式下载:将输入流转成字节,再生成文件,这种方式方便将字节数组直接返回给前台jsp页面 |
| | | byte[] input2byte = input2byte(retrieveFileStream); |
| | | byte2File(input2byte, localPath, fileName); |
| | | |
| | | if(null != retrieveFileStream){ |
| | | if (null != retrieveFileStream) { |
| | | retrieveFileStream.close(); |
| | | } |
| | | } catch (FileNotFoundException e) { |
| | |
| | | return result; |
| | | } |
| | | |
| | | if(null != input){ |
| | | if (null != input) { |
| | | input.close(); |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |