java实现ftp上传下载(jdk1.7以上)

java实现ftp上传下载(jdk1.7以上)

网上有很多关于java实现ftp的上传与下载,但都是jdk1.7以下的,最近在做java实现ftp文件上传与下在,搜到的代码不是jdk1.7一下的,就是apache的。将我测试通过的代码与大家共享。

 

完整代码,复制可用

 

FTP实现代码:

package com.pifeng.util;

 

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 sun.net.ftp.FtpClient;

import sun.net.ftp.FtpProtocolException;

 

/**

 * 

 * @author 皮锋 java自带的API对FTP的操作

 *

 */

public class FtpUtil {

 

// FTP客户端

private FtpClient ftpClient;

 

/**

 *  服务器连接

 * @param ip  服务器IP

 * @param port  服务器端口

 * @param user  用户名

 * @param password 密码

 * @param path  服务器路径

 */

public void connectServer(String ipint port, String user,String password, String path) {

try {

ftpClient = FtpClient.create();

try {

SocketAddress addr = new InetSocketAddress(ipport);

this.ftpClient.connect(addr);

this.ftpClient.login(userpassword.toCharArray());

System.out.println("login success!");

if (path.length() != 0) {

// 把远程系统上的目录切换到参数path所指定的目录

this.ftpClient.changeDirectory(path);

}

catch (FtpProtocolException e) {

e.printStackTrace();

}

catch (IOException ex) {

ex.printStackTrace();

throw new RuntimeException(ex);

}

}

 

/**

 * 上传文件

 * @param localFile 本地文件

 * @param remoteFile 远程文件

 */

public void upload(String localFile, String remoteFile) {

//this.localFilename = localFile;

//this.remoteFilename = remoteFile;

OutputStream os = null;

FileInputStream is = null;

try {

try {

// 将远程文件加入输出流中

os = this.ftpClient.putFileStream(remoteFile);

catch (FtpProtocolException e) {

e.printStackTrace();

}

// 获取本地文件的输入流

File file_in = new File(localFile);

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");

catch (IOException ex) {

System.out.println("not upload");

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();

}

}

}

}

 

/**

 * 文件下载

 * @param remoteFile  远程文件

 * @param localFile 本地文件

 */

public void download(String remoteFile, String localFile) {

InputStream is = null;

FileOutputStream os = null;

try {

try {

// 获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地

is = this.ftpClient.getFileStream(remoteFile);

catch (FtpProtocolException e) {

e.printStackTrace();

}

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 void closeConnect() {

try {

this.ftpClient.close();

System.out.println("disconnect success");

catch (IOException ex) {

System.out.println("not disconnect");

ex.printStackTrace();

throw new RuntimeException(ex);

}

}

 

}

 

测试代码:
package test;
 
import java.text.SimpleDateFormat;
import com.pifeng.util.FtpUtil;
public class TestFtpUtil {
 

public static void main(String agrs[]) {
FtpUtil fu=new FtpUtil();
String filepath[] = { "AWC1002972G020-1.dwg", "AWC1002974G020-1.cgm" };
String localfilepath[] = { "D:\\AWC1002972G020-1.dwg","D:\\AWC1002974G020-1.cgm" };
/* * 使用默认的端口号、用户名、密码以及根目录连接FTP服务器 */
fu.connectServer("127.0.0.1", 21, "pifeng", "19920720", "/");
// 下载
for (int i = 0; i < filepath.length; i++) {
fu.download(filepath[i], localfilepath[i]);
}
String localfile = "D:\\WorkToolKit\\PDF\\ReadMe.htm";
String remotefile = "ReadMe.htm";
// 上传
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyy-MM-dd   hh:mm:ss");
System.out.println(sDateFormat.format(new java.util.Date()));
// new Date()为获取当前系统时间
fu.upload(localfile, remotefile);
System.out.println(sDateFormat.format(new java.util.Date()));
// new Date()为获取当前系统时间
fu.closeConnect();

}

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

E%3Dmc%B2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值