ftputil.java
来自「cwbbs 云网论坛源码」· Java 代码 · 共 128 行
JAVA
128 行
package com.cloudwebsoft.framework.test;import java.io.*;import org.apache.commons.net.ftp.*;import cn.js.fan.util.StrUtil;public class FtpUtil { FTPClient ftp = new FTPClient(); public FtpUtil() throws Exception { } public boolean connect(String host, int port, String userName, String password, boolean isPassiveMode) throws IOException { ftp.connect(host, port); boolean re = ftp.login(userName, password); if (!re) { close(); } else { if (isPassiveMode) ftp.enterLocalPassiveMode(); else ftp.enterLocalActiveMode(); } return re; } public boolean storeFile(String ftpPath, String filePath) throws IOException { ftp.setFileType(ftp.BINARY_FILE_TYPE); String fileName; if (ftpPath.equals("")) return false; else { if (ftpPath.startsWith("/")) ftpPath = ftpPath.substring(1); String[] paths = ftpPath.split("/"); int len = paths.length; fileName = paths[len-1]; for (int i=0; i<len-1; i++) { ftp.changeWorkingDirectory(paths[i]); if (ftp.getReplyCode()!=250) { ftp.makeDirectory(paths[i]); ftp.changeWorkingDirectory(paths[i]); System.out.println(getClass() + " replyString=" + ftp.getReplyString()); } } } FileInputStream is = new FileInputStream(filePath); boolean re = ftp.storeFile(fileName, is); is.close(); return ftp.getReplyCode()==226; } public void close() throws IOException { if (ftp != null) { ftp.disconnect(); ftp = null; } } public void del(String path) throws IOException { ftp.dele(path); } public int getReplyCode() { return ftp.getReplyCode(); } public String getReplyMessage() { return ftp.getReplyString(); } public void test1() throws Exception { if (!connect("127.0.0.1", 21, "cws", "1", true)) { return; } System.out.println(getClass() + " replyString=" + ftp.getReplyString()); System.out.println(getClass() + " upfile replyString=" + ftp.getReplyString()); del("/111/222/2.jpg"); System.out.println(getClass() + " replyString1=" + ftp.getReplyString()); close(); } public static void main(String[] args) { try { FtpUtil ftpApache1 = new FtpUtil(); ftpApache1.test1(); } catch (Exception e) { e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?