您的位置:首页 > 编程语言 > Java开发

jdk1.7实现ftp上传文件

2016-09-01 00:27 399 查看
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;
import sun.net.ftp.FtpProtocolException;
public class FtpUtil {

private String localfilename;
private String remotefilename;
private FtpClient ftpClient;

public void connectServer(String ip, int port, String user, String password, String path)
throws FtpProtocolException{
try {
ftpClient = FtpClient.create();
try {
SocketAddress addr = new InetSocketAddress(ip, port);
ftpClient.connect(addr);
ftpClient.login(user, password.toCharArray());
System.out.println("login success!");
if (path.length() != 0) {
// 把远程系统上的目录切换到参数path所指定的目录
ftpClient.changeDirectory(path);
}
} catch (FtpProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}
public void closeConnect()
{
try
{
ftpClient.close();
System.out.println("disconnect success");
} catch (IOException ex)
{
System.out.println("not disconnect");
ex.printStackTrace();
throw new RuntimeException(ex);
}
}

public boolean upload(String localFile, String remoteFile) throws FtpProtocolException

{
this.localfilename = localFile;
this.remotefilename = remoteFile;
OutputStream os = null;
FileInputStream is = null;
boolean flage = false;
try
{
// 将远程文件加入输出流中
os = (OutputStream) ftpClient.putFileStream(this.remotefilename, true);
// 获取本地文件的输入流
File file_in = new File(this.localfilename);
is = new FileInputStream(file_in);
// 创建一个缓冲区
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
System.out.println("upload success");
flage = true;
// return true;
} catch (IOException ex)
{
System.out.println("not upload");
ex.printStackTrace();
flage = false;
throw new RuntimeException(ex);
} finally
{
try
{
if (is != null)
{
is.close();
}
} catch (IOException e)
{
e.printStackTrace();
} finally
{
try
{
if (os != null)
{
os.close();
}
} catch (IOException e)
{
e.printStackTrace();
}
}
}
return flage;
}

public void download(String remoteFile, String localFile) throws FtpProtocolException{
TelnetInputStream is = null;
FileOutputStream os = null;
try
{
// 获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
is = (TelnetInputStream) ftpClient.getFileStream(remoteFile);
File file_in = new File(localFile);
os = new FileOutputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
}
System.out.println("download success");
} catch (IOException ex){
System.out.println("not download");
ex.printStackTrace();
throw new RuntimeException(ex);
} finally{
try{
if (is != null)

{
is.close();
}
}catch (IOException e){
e.printStackTrace();
} finally{
try{
if (os != null)
{
os.close();
}
} catch (IOException e)

{
e.printStackTrace();
}
}
}
}
public static void main(String agrs[]) {
String filepath[] ={ "/aa.txt", "/regist.log" };
String localfilepath[] ={ "C:\\tmp\\1.txt", "C:\\tmp\\2.log" };
FtpUtil ftp = new FtpUtil();
try
{
ftp.connectServer(ftp的地址, 端口, 用户名, 密码, 路径);
} catch (FtpProtocolException e)
{
e.printStackTrace();
}
// 下载
String localfile = "D:/ceshi.jpg";
String remotefile = ftp上传路径"/javaftp测试.jpg";
// 上传
try
{
ftp.upload(localfile, remotefile);
} catch (FtpProtocolException e)
{
e.printStackTrace();
}
ftp.closeConnect();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ftp上传文件