用到的jar包: comments-net.jar
下载地址:http://download.csdn.net/detail/xuanjiewu/9838448
这里仅仅是对ftp工具类的简单使用,很多东西还不是很了解。当然学以致用,先用到这里吧。
[java]
view plain
copy
print
?
- public class FtpTest {
- /**
- * 向ftp写文件(数据)
- */
- @Test
- public void uploadFile() {
- // 要写入的文件内容
- String fileContent = "hello world,你好世界";
- // ftp登录用户名
- String userName = "admin";
- // ftp登录密码
- String userPassword = "xxxx";
- // ftp地址
- String server = "127.0.0.1";//直接ip地址
- // 创建的文件
- String fileName = "ftp.txt";
- // 指定写入的目录
- String path = "wd";
- FTPClient ftpClient = new FTPClient();
- try {
- InputStream is = null;
- // 1.输入流
- is = new ByteArrayInputStream(fileContent.getBytes());
- // 2.连接服务器
- ftpClient.connect(server);
- // 3.登录ftp
- ftpClient.login(userName, userPassword);
- // 4.指定写入的目录
- ftpClient.changeWorkingDirectory(path);
- // 5.写操作
- ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
- ftpClient.storeFile(new String(fileName.getBytes("utf-8"),
- "iso-8859-1"), is);
- is.close();
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (ftpClient.isConnected()) {
- try {
- ftpClient.disconnect();
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * ftp下载数据
- */
- @Test
- public void downFile() {
- // ftp登录用户名
- String userName = "admin";
- // ftp登录密码
- String userPassword = "xxxx";
- // ftp地址:直接IP地址
- String server = "xxxx";
- // 创建的文件
- String fileName = "ftp.txt";
- // 指定写入的目录
- String path = "wd";
- // 指定本地写入文件
- String localPath="D:\\";
- FTPClient ftp = new FTPClient();
- try {
- int reply;
- //1.连接服务器
- ftp.connect(server);
- //2.登录服务器 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
- ftp.login(userName, userPassword);
- //3.判断登陆是否成功
- reply = ftp.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftp.disconnect();
- }
- //4.指定要下载的目录
- ftp.changeWorkingDirectory(path);// 转移到FTP服务器目录
- //5.遍历下载的目录
- FTPFile[] fs = ftp.listFiles();
- for (FTPFile ff : fs) {
- //解决中文乱码问题,两次解码
- byte[] bytes=ff.getName().getBytes("iso-8859-1");
- String fn=new String(bytes,"utf8");
- if (fn.equals(fileName)) {
- //6.写操作,将其写入到本地文件中
- File localFile = new File(localPath + ff.getName());
- OutputStream is = new FileOutputStream(localFile);
- ftp.retrieveFile(ff.getName(), is);
- is.close();
- }
- }
- ftp.logout();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ftp.isConnected()) {
- try {
- ftp.disconnect();
- } catch (IOException ioe) {
- }
- }
- }
- }
- }
很多知识点是相互联系的,希望以后的例子中能够结合更多的知识点进行实例编写,这样也有助于知识的巩固。
参见:
http://blog.csdn.net/techbirds_bao/article/details/8593706
http://blog.csdn.net/kardelpeng/article/details/6588284
封装
/**
* FTP 连接服务
* @param ftpHost
* @param ftpPort
* @param ftpUserName
* @param ftpPassword
* @return
*/
public static FTPClient getFTPClient(String ftpHost, int ftpPort, String ftpUserName, String ftpPassword) {
FTPClient ftpClient = null;
try {
ftpClient = new FTPClient();
ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
ftpClient.login(ftpUserName, ftpPassword);// 登陆FTP服务器
if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
System.out.println("未连接到FTP,用户名或密码错误!");
ftpClient.disconnect();
} else {
System.out.println("FTP连接成功!");
}
} catch (SocketException e) {
e.printStackTrace();
System.out.println("FTP的IP地址可能错误,请正确配置!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("FTP的端口错误,请正确配置!");
}
return ftpClient;
}
/**
* FTP文件下载/读取
* @param ftpServer
* @param ftpPort
* @param ftpUser
* @param ftpPwd
* @param fileName
* @param filePath
* @param localPath
*/
public static String ftpDownload(String ftpServer, int ftpPort, String ftpUser,String ftpPwd,String filePath,String fileName,String localPath){
String message = "";
try {
FTPClient ftp = getFTPClient(ftpServer, ftpPort, ftpUser, ftpPwd);
ftp.changeWorkingDirectory(filePath); //转移到FTP服务器目录
File localFile = new File(localPath + fileName);
OutputStream os = new FileOutputStream(localFile);
ftp.retrieveFile(fileName, os);
os.close();
System.out.println("FTP文件:"+fileName+" 已成功下载到 "+localPath);
message = fileName+" 已成功下载到 "+localPath;
ftp.logout();
} catch (IOException e) {
message = "FTP文件:"+fileName+"下载失败!";
e.printStackTrace();
}
return message;
}
原文链接:https://blog.csdn.net/xuanjiewu/article/details/71558548
本站声明:网站内容来源于网络,如有侵权,请联系我们,我们将及时处理。
还没有人抢沙发呢~