package com.northking.dataManager.util;
import sun.net.ftp.*;
import sun.net.*;
import java.io.*;
/**
* 使用sun.net.ftp工具包进行ftp上传下载
* @author maochangming
* @date 2008-6-20 13:09:29
* @description:
*/
public class FtpTool {
String ip;
int port;
String user;
String pwd;
String remotePath;
String localPath;
FtpClient ftpClient;
/**
* 连接ftp服务器
* @param ip
* @param port
* @param user
* @param pwd
* @return
* @throws Exception
*/
public boolean connectServer(String ip, int port, String user, String pwd)
throws Exception {
boolean isSuccess = false;
try {
ftpClient = new FtpClient();
ftpClient.openServer(ip, port);
ftpClient.login(user, pwd);
isSuccess = true;
} catch (Exception ex) {
throw new Exception("Connect ftp server error:" + ex.getMessage());
}
return isSuccess;
}
/**
* 下载文件
* @param remotePath
* @param localPath
* @param filename
* @throws Exception
*/
public void downloadFile(String remotePath,String localPath, String filename) throws Exception {
try {
if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
if (remotePath.length() != 0)
ftpClient.cd(remotePath);
ftpClient.binary();
TelnetInputStream is = ftpClient.get(filename);
File file_out = new File(localPath + File.separator + filename);
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
}
} catch (Exception ex) {
throw new Exception("ftp download file error:" + ex.getMessage());
}
}
/**
* 上传文件
* @param remotePath
* @param localPath
* @param filename
* @throws Exception
*/
public void uploadFile(String remotePath,String localPath, String filename) throws Exception {
try {
if (connectServer(getIp(), getPort(), getUser(), getPwd())) {
if (remotePath.length() != 0)
ftpClient.cd(remotePath);
ftpClient.binary();
TelnetOutputStream os = ftpClient.put(filename);
File file_in = new File(localPath + File.separator + filename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
}
} catch (Exception ex) {
throw new Exception("ftp upload file error:" + ex.getMessage());
}
}
/**
* @return
*/
public String getIp() {
return ip;
}
/**
* @return
*/
public int getPort() {
return port;
}
/**
* @return
*/
public String getPwd() {
return pwd;
}
/**
* @return
*/
public String getUser() {
return user;
}
/**
* @param string
*/
public void setIp(String string) {
ip = string;
}
/**
* @param i
*/
public void setPort(int i) {
port = i;
}
/**
* @param string
*/
public void setPwd(String string) {
pwd = string;
}
/**
* @param string
*/
public void setUser(String string) {
user = string;
}
/**
* @return
*/
public FtpClient getFtpClient() {
return ftpClient;
}
/**
* @param client
*/
public void setFtpClient(FtpClient client) {
ftpClient = client;
}
/**
* @return
*/
public String getRemotePath() {
return remotePath;
}
/**
* @param string
*/
public void setRemotePath(String string) {
remotePath = string;
}
/**
* @return
*/
public String getLocalPath() {
return localPath;
}
/**
* @param string
*/
public void setLocalPath(String string) {
localPath = string;
}
}
Junit测试类
package com.northking.dataManager.dataimport.parse.test;
import com.northking.dataManager.util.FtpTool;
import junit.framework.TestCase;
/**
* @author maochangming
* @date 2008-6-20 13:09:11
* @description:
*/
public class FtpToolTest extends TestCase {
FtpTool ftpTool;
/**
* Constructor for FtpToolTest.
* @param arg0
*/
public FtpToolTest(String arg0) {
super(arg0);
}
public static void main(String[] args) {
junit.textui.TestRunner.run(FtpToolTest.class);
}
public void testDownLoadFile()throws Exception{
ftpTool.downloadFile(ftpTool.getRemotePath(),"c:/downloads","JBFImgMng.CAB");
}
/*
* @see TestCase#setUp()
*/
protected void setUp() throws Exception {
ftpTool = new FtpTool();
ftpTool.setIp("10.164.12.70");
ftpTool.setPort(2100);
ftpTool.setUser("share");
ftpTool.setPwd("share");
ftpTool.setRemotePath("/paeams");
super.setUp();
}
/*
* @see TestCase#tearDown()
*/
protected void tearDown() throws Exception {
super.tearDown();
}
}
分享到:
相关推荐
NULL 博文链接:https://starbhhc.iteye.com/blog/2315455
com.sun.net.ssl.jar
com.sun.net.httpserver
sun.net.ftp.FtpClient,it.sauronsoftware.ftp4j.FTPClient,org.apache.commons.net.ftp.FTPClient三种不同的方式操作FTP
JAVA HTTP 发送 接收 com.sun.net.httpserver包 demo JAVA HTTP 发送 接收 com.sun.net.httpserver包 demo
NULL 博文链接:https://nassir.iteye.com/blog/1112713
别忘了在程序中还要引入两个库文件(import sun.net.*,import sun.net.ftp.*)。 以下是这三部分的JAVA源程序: (1)显示FTP服务器上的文件 void ftpList_actionPerformed(ActionEvent e) { String server=serverEdit....
1.下载winsw官网上的xml文件和.exe文件(已下载) 使用此地址下载: http://repo.jenkins-ci.org/releases/com/sun/winsw/winsw/ 2.创建一个文件夹,将所需要转换的jar包,和这两个文件放在一起,并且将.exe的文件名修改...
java通过ftp三种方式 上传 下载文件: 1. 通过sun :sun.net.ftp.FtpClient 2. 通过apache:org.apache.commons.net.ftp.FTPClient 3. 通过edtftp: com.enterprisedt.net.ftp.FileTransferClient
在JAVA的编程中,也许会遇到FTP方面的编程,本文就来演示如何实现它。本程序是由JBUILDER2.0来开发的,为了节约篇幅我只列出主要的三...别忘了在程序中还要引入两个库文件(import sun.net.*,import sun.net.ftp.*)。
Java实现文件下载并解决中文文件名乱码
sun没有公开的API,文件上传下载、登陆FTP服务器时会用到
* 从网络Url中下载文件 * * @param urlStr * @param fileName * @param savePath * @throws IOException */ public static void downLoadByUrl(String urlStr, String fileName, String savePath) throws ...
包含众多很难找到的包 com.borland.jbcl.layout.jar sun公司的包 apache的一些类 isqlviewer包 javax.jms的包 artima
别忘了在程序中还要引入两个库文件(import sun.net.*,import sun.net.ftp.*)。以下是这三部分的JAVA源程序。1)显示FTP服务器上的文件,2)从FTP服务器上下传一个文件,3)向FTP服务器上上传一个文件。
原型Java的com.sun.net.HttpServer和相关类的包装ProtoJ是一个原型服务器,旨在使编写Java简单服务器变得快速而轻松。 目前尚不适合生产,并且不适合生产,但如果将来会适用,那就太好了。 ProtoJ应该针对Java 8和...
主要介绍了javax.net.ssl.SSLException: java.lang.RuntimeException: Could not generate DH keypair 解决方法,有需要的朋友们可以学习下。
最近做了一个JAVA FTP协议 APACHE包内,上传,下载,查看,删除文件和目录的东西,拿出来给不明白的同学分享一下,希望得到帮助。搭建好服务器后,运行代码实现相应功能。