在实际的应用重,通常是通过程序来进行文件的上传。
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.3</version>
</dependency>
3.实例代码:
package com.test.fto.demo;
/**
*/
public class Ftp {
private String ipAddr;//ip地址
private Integer port;//端口号
private String userName;//用户名
private String pwd;//密码
private String path;//aaa路径
public String getIpAddr() {
return ipAddr;
}
public void setIpAddr(String ipAddr) {
this.ipAddr = ipAddr;
}
public Integer getPort() {
return port;
}
public void setPort(Integer port) {
this.port = port;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
测试代码:
package com.test.fto.demo;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
import org.testng.annotations.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class FtpUtil {
private static FTPClient ftp;
/**
* 获取ftp连接
*
* @param f
* @return
* @throws Exception
*/
public static boolean connectFtp(Ftp f) throws Exception {
ftp = new FTPClient();
boolean flag = false;
int reply;
if (f.getPort() == null) {
ftp.connect(f.getIpAddr(), 21);
} else {
ftp.connect(f.getIpAddr(), f.getPort());
}
ftp.login(f.getUserName(), f.getPwd());
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return flag;
}
ftp.changeWorkingDirectory(f.getPath());
flag = true;
return flag;
}
/**
* 关闭ftp连接
*/
public static void closeFtp() {
if (ftp != null && ftp.isConnected()) {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* ftp上传文件
*
* @param f
* @throws Exception
*/
public static void upload(File f) throws Exception {
if (f.isDirectory()) {
ftp.makeDirectory(f.getName());
ftp.changeWorkingDirectory(f.getName());
String[] files = f.list();
for (String fstr : files) {
File file1 = new File(f.getPath() + "/" + fstr);
if (file1.isDirectory()) {
upload(file1);
ftp.changeToParentDirectory();
} else {
File file2 = new File(f.getPath() + "/" + fstr);
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
} else {
File file2 = new File(f.getPath());
FileInputStream input = new FileInputStream(file2);
ftp.storeFile(file2.getName(), input);
input.close();
}
}
@Test
public static void test() throws Exception {
Ftp f = new Ftp();
f.setIpAddr("your ip");
f.setUserName("username");
f.setPwd("password");
FtpUtil.connectFtp(f);
File file = new File("F:/robotium-solo-5.6.1.jar");
FtpUtil.upload(file);//把文件上传在ftp上
System.out.println("上传文件完成。。。。");
}
}
__EOF__
本文作者:久曲建的测试窝
本文链接:https://www.cnblogs.com/longronglang/p/8724677.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角【推荐】一下。您的鼓励是博主的最大动力!
优秀不够,你是否无可替代
软件测试交流QQ群:721256703,期待你的加入!!
欢迎关注我的微信公众号:软件测试君
![](https://s4.51cto.com/images/blog/202011/30/926617efea0830de48e23dbfdd861f72.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)
原文:https://blog.51cto.com/15009374/2557206