当前位置:文档之家› FTP文件上传(纯代码)

FTP文件上传(纯代码)

File file_in = new File(savefilename);//打开本地待长传的文件
if (!file_in.exists()) {
throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");
}
if (file_in.isDirectory()) {
this.port=port;
ername=username;
this.password=password;
}
public static void main(String args[]){
FtpUtil ftp=new FtpUtil("172.16.10.142","admin","adminly");
if(!isDirExist(newName)){
createDir(newName);
}
ftpClient.cd(newName);
File sourceFile[]=file_in.listFiles();
for(int i=0;i<sourceFile.length;i++){
if(!sourceFile[i].exists()){
ftpClient.closeServer();
}
System.out.println("已与服务器断开连接");
return true;
}catch(IOException e){
e.printStackTrace();
return false;
}
}
/**
*检查文件在当前目录下是否存在
* @param dir
System.out.println(result ? "上传成功!" : "上传失败!");
ftp.closeServer();
}
/**
* ftp上传如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换
* @param fileName要上传的文件(或文件夹)名
StringTokenizer s=new StringTokenizer(dir,"/");//sign(记号,符号)
s.countTokens();
/**
* countTokens
public int countTokens()计算在生成异常之前可以调用此tokenizer的nextToken方法的次数。当前位置没有提前。
String newname="";
if(filename.indexOf("/")>-1){
newname=filename.substring(stIndexOf("/")+1);
import .TelnetOutputStream;
import .ftp.FtpClient;
/**
* Title: FTP上传功能类
* Description: FTP上传功能类
* Copyright: Copyright (c) 2012
* Company:北京世纪国彩科技有限公司
返回:
使用当前分隔符集的字符串中剩余的标记数。
以上是API中的说明,
我写了一个小程序:
import java.util.*;
public class Tokenizer{
public static void main(String[] args){
StringTokenizer a = new StringTokenizer("a b c");
ftpClient.cd(this.path);//path是ftp服务下主目录的子目录
}
ftpClient.binary();//使用二进制上传与下载
System.out.println("已登录到\""+ftpClient.pwd()+"\"目录");
return true;
}catch(IOException e){
this.ip=serverIp;
ername=username;
this.password=password;
}
public FtpUtil(String serverIp , int port,String username,String password){
this.ip=serverIp;
}
return true;
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception e in Ftp upload(): " + e.toString());
return false;
} finally {
try {
System.out.println(a.countTokens());
}
}
程序执行的结果是3,因为a b c采用默认的分隔符分隔,一共有a,b,c三个token,所以countTokens()的结果是3
*/
String pathName=ftpClient.pwd();
while(s.hasMoreElements()){
File file_in=new File(savefilename);//打开本地待上传的文件
if(!file_in.exists()){
throw new Exception("此文件或文件夹["+file_in.getName()+"]有误或不存在!");
}
if(file_in.isDirectory()){
ftp.connectServer();
boolean result=ftp.upload("C:/Documents and Settings/SJKC/桌面/java/cc.txt", "cc.txt");
//boolean result=ftp.downloadFile("C:/Documents and Settings/SJKC/桌面/java/cc.txt", "cc.txt");
private String path="";
FtpClient ftpClient=null;
OutputStream os=null;
FileInputStream is=null;
public FtpUtil(String serverIp ,String username,String password){
* @param path
*/
private void upload(String fileName, String newName, String path) throws Exception{
String savefilename=new String(fileName.getBytes("ISO-8859-1"),"GBK");
try{
if(this.port!=-1){
ftpClient.openServer(this.ip,this.port);
}else{
ftpClient.openServer(this.ip);
}
ftpClient.login(ername, this.password);
if(this.path.length()!=0){
e.printStackTrace();
return false;
}
}
/**
*断开与ftp服务器的连接
* @return
*/
public boolean closeServer(){
try{
if(is!=null){
is.close();
}
if(os!=null){
os.close();
}
if(ftpClient!=null){
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import .TelnetInputStream;
* @param newName服务器端要生成的文件(或文件夹)名
* @return
*/
public boolean upload(String fileName,String newName){
try{
String savefilename=new String(fileName.getBytes("GBK"),"GBK");
}
ftpClient.binary();
return true;
}catch(IOException ee){
ee.printStackTrace();
return false;
}
}
/**
*连接ftp服务器
* @return
*/
public boolean connectServer(){
ftpClient=new FtpClient();
相关主题