永发信息网

java jdk1.6环境下实现 ftp文件上传

答案:1  悬赏:40  手机版
解决时间 2021-03-09 09:53
java jdk1.6环境下实现 ftp文件上传
最佳答案
通过JDK自带的API实现

package com.cloudpower.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;


public class Ftp {

private String localfilename;

private String remotefilename;

private FtpClient ftpClient;


public void connectServer(String ip, int port, String user,
String password, String path) {
try {

//第一种方法
// ftpClient = new FtpClient();
// ftpClient.openServer(ip, port);
//第二种方法
ftpClient = new FtpClient(ip);

ftpClient.login(user, password);
// 设置成2进制传输
ftpClient.binary();
System.out.println("login success!");
if (path.length() != 0){
//把远程系统上的目录切换到参数path所指定的目录
ftpClient.cd(path);
}
ftpClient.binary();
} catch (IOException ex) {
ex.printStackTrace();
throw new RuntimeException(ex);
}
}

public void closeConnect() {
try {
ftpClient.closeServer();
System.out.println("disconnect success");
} catch (IOException ex) {
System.out.println("not disconnect");
ex.printStackTrace();
throw new RuntimeException(ex);
}
}

public void upload(String localFile, String remoteFile) {
this.localfilename = localFile;
this.remotefilename = remoteFile;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
//将远程文件加入输出流中
os = ftpClient.put(this.remotefilename);
//获取本地文件的输入流
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");
} 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();
}
}
}
}


public void download(String remoteFile, String localFile) {
TelnetInputStream is = null;
FileOutputStream os = null;
try {
//获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
is = ftpClient.get(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[] = { "/temp/aa.txt", "/temp/regist.log"};
String localfilepath[] = { "C:\\tmp\\1.txt","C:\\tmp\\2.log"};

Ftp fu = new Ftp();

fu.connectServer("127.0.0.1", 22, "anonymous", "IEUser@", "/temp");

//下载
for (int i = 0; i < filepath.length; i++) {
fu.download(filepath[i], localfilepath[i]);
}

String localfile = "E:\\号码.txt";
String remotefile = "/temp/哈哈.txt";
//上传
fu.upload(localfile, remotefile);
fu.closeConnect();
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
松霖一键花洒怎么样?质量好吗?
E3+GTX960主机这样搭配可以吗?
梦见自己割断一个人的喉咙,医生帮他接好了,
wii一般需要多大的移动硬盘
若|向量a|=1,|向量b|=2,a与b的夹角为60°,若
仁寿县公安局交警大队北斗中队地址在哪,我要
微信网名三个字
男朋友在电影院做工程部主管需要整夜上班么?
我给别人汇钱汇错账户了,能找回来吗
海子和徐志摩的诗中,描写友情的诗句
aj12黑金扣求鉴定!在线等!急~求大神帮忙啊
火蓝刀锋第11集中龙队去见榕声时的那段轻音乐
英语aaa,abb,abc,aba,的单词各3个
壮乡黑猪骋望骊都店我想知道这个在什么地方
已知M的平方+M-1=0,求M的立方+2M的平方+2009
推荐资讯
我国《土工合成材料应用技术规范》GB50290-19
智能手机超市在哪里啊,我有事要去这个地方
太阁立志传5怎么偷别人的东西?
求余罪一二季百度云资源
爱心黄金珠宝厂价直销体验店都江堰店在什么地
小孩的跟玩具啪啪长大有生育吗
这道题的解题过程是?At the railway stati
印度电影《武力》里男主角开的那辆黑色吉普车
如何联系到一位明星的经纪人?
英特尔 ValleyView Gen7安装什么显卡
春熙路到青城山怎么走
《竞业限制协议》说离职一年内不得进入竞争对
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?