兄弟现在写了一个用作ftp的java文件叫MultiFtp.java,已经编译成功了,生成了两个class文件,但是在jsp文件中我想添加一个按钮,当用户点击按钮时再触发ftp上传,不知道哪位大侠能出个主意呢??帮帮忙~~
ftp文件如下:
package mftp;import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FTPConnectMode;import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.File;public final class MultiFtp { private String username = null;
private String password = null;
private String keyword = null;
private String srcpath = "";
private String targetpath ="";
private String hostaddr = null;
private boolean debug=false;
private boolean RUNNING=true; public MultiFtp() {
} public void TransferFile(String[] flist)
{
FTPClient ftp = null;
System.out.println("开始连接服务器...\n");
try
{
ftp = new FTPClient(hostaddr, 21);
if(debug) ftp.debugResponses(true);
ftp.login(username, password);
System.out.print("登录服务器成功...\n");
ftp.setType(FTPTransferType.BINARY);
ftp.setTimeout(180*1000); // 3 minutes
ftp.setConnectMode(FTPConnectMode.ACTIVE);
if(targetpath == "") ftp.chdir("/");
else ftp.chdir(targetpath); }catch(FTPException e)
{
System.out.println("登录服务器错误,返回码:"+e.getReplyCode());
if(ftp != null) try{ ftp.quit(); ftp=null; }catch(Exception f){}
System.out.println("请确定您有相应的访问权限. 本次操作无效, 准备进行下次尝试...\n");
return;
} catch(IOException e)
{
System.out.println("系统出现连接错误或超时连接, 请确认线路连接正常:\n");
e.printStackTrace();
ftp=null;
return;
} try
{
for (int i=0;i<flist.length;i++)
{
System.out.print("正在传输文件: "+flist[i]+"... ");
try
{
ftp.put(srcpath+File.separator+flist[i],targetpath+"/"+flist[i]);
}catch(FTPException e)
{
System.out.print("\n服务器文件传输错误,返回码:"+e.getReplyCode());
System.out.print("\n请确定您对文件:"+flist[i]+"相应的访问权限. 本次文件操作无效,尝试传输下一组文件...\n");
continue;
}
catch(FileNotFoundException ee)
{ continue;
}
catch(IOException e)
{
System.out.print("\n系统传输时出现连接错误, 请确认线路连接正常:\n");
e.printStackTrace();
ftp=null;
break;
} File rf = new File(srcpath+File.separator+flist[i]);
rf.delete(); rf=null;
System.out.print("传输完成\n"); }
}catch(NullPointerException pe)
{
System.out.print("系统在传输过程中发生了空值错误,尝试自动修复 ...\n");
RUNNING=false;
}
catch(RuntimeException re)
{
System.out.print("系统在传输过程中发生了运行时错误,尝试自动修复 ...\n");
RUNNING=false;
}
catch(Error er)
{
System.out.println("系统在运行过程中发生严重错误, 试图自动修复...");
RUNNING=false;
}
catch(Exception e)
{
System.out.print("系统在传输过程中发生了未知错误,尝试自动修复 ...\n");
System.out.println("原因:"+e.getMessage());
e.printStackTrace();
RUNNING=false;
}
finally {
if(ftp != null) try{ ftp.quit(); ftp=null;}catch(Exception f){}
} System.out.print("本次传输结束.\n");
ftp=null;
}
public void doWork(String[] args)
{
System.out.println("MFTP ver 0.62");
if (args.length< 6)
{
System.out.print("Invalid Commandline , please try again.\n");
System.out.print("Usage: host user pass keyword srcpath targetpath\n");
return;
}
else if(args.length == 7) debug=true; hostaddr = args[0];
username = args[1]; password=args[2];
keyword = args[3]; if(args[4].trim().length()>1) srcpath=args[4];
if(args[5].trim().length()>1) targetpath=args[5]; //========= Begin Main Loop ==============
File fd = new File(srcpath);
KwFileFilter kw = new KwFileFilter();
String[] flist =null;
System.out.print("开始监控目录数据,并传输...\n");
while(RUNNING)
{
//System.out.println("文件检查开始 ...");
// flist = fd.list(kw);
flist=fd.list();
if(flist != null)
{
if(flist.length >= 1)
{
System.out.println("\n共检索到"+flist.length+"个文件");
TransferFile(flist);
}
}
try
{
Thread.sleep(1000*60);
}
catch(InterruptedException ie)
{
if(!RUNNING) break;
}
System.out.print(".");
} // end while System.out.println("监控循环已经停止");
//========= End Main Loop ================
} public static void main(String[] args) {
//args : hostaddr, username, password, keyword,srcpath, targetpath
//System.out.println("MFTP ver 0.39\n");
MultiFtp mftp = new MultiFtp();
mftp.doWork(args);
} class KwFileFilter implements FilenameFilter {
public boolean accept(File dir,String name){
if(name.endsWith(".xml")) return true;
if(name.endsWith(".XML")) return true;
return false;
}
}
}
ftp文件如下:
package mftp;import com.enterprisedt.net.ftp.FTPException;
import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.net.ftp.FTPConnectMode;import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.File;public final class MultiFtp { private String username = null;
private String password = null;
private String keyword = null;
private String srcpath = "";
private String targetpath ="";
private String hostaddr = null;
private boolean debug=false;
private boolean RUNNING=true; public MultiFtp() {
} public void TransferFile(String[] flist)
{
FTPClient ftp = null;
System.out.println("开始连接服务器...\n");
try
{
ftp = new FTPClient(hostaddr, 21);
if(debug) ftp.debugResponses(true);
ftp.login(username, password);
System.out.print("登录服务器成功...\n");
ftp.setType(FTPTransferType.BINARY);
ftp.setTimeout(180*1000); // 3 minutes
ftp.setConnectMode(FTPConnectMode.ACTIVE);
if(targetpath == "") ftp.chdir("/");
else ftp.chdir(targetpath); }catch(FTPException e)
{
System.out.println("登录服务器错误,返回码:"+e.getReplyCode());
if(ftp != null) try{ ftp.quit(); ftp=null; }catch(Exception f){}
System.out.println("请确定您有相应的访问权限. 本次操作无效, 准备进行下次尝试...\n");
return;
} catch(IOException e)
{
System.out.println("系统出现连接错误或超时连接, 请确认线路连接正常:\n");
e.printStackTrace();
ftp=null;
return;
} try
{
for (int i=0;i<flist.length;i++)
{
System.out.print("正在传输文件: "+flist[i]+"... ");
try
{
ftp.put(srcpath+File.separator+flist[i],targetpath+"/"+flist[i]);
}catch(FTPException e)
{
System.out.print("\n服务器文件传输错误,返回码:"+e.getReplyCode());
System.out.print("\n请确定您对文件:"+flist[i]+"相应的访问权限. 本次文件操作无效,尝试传输下一组文件...\n");
continue;
}
catch(FileNotFoundException ee)
{ continue;
}
catch(IOException e)
{
System.out.print("\n系统传输时出现连接错误, 请确认线路连接正常:\n");
e.printStackTrace();
ftp=null;
break;
} File rf = new File(srcpath+File.separator+flist[i]);
rf.delete(); rf=null;
System.out.print("传输完成\n"); }
}catch(NullPointerException pe)
{
System.out.print("系统在传输过程中发生了空值错误,尝试自动修复 ...\n");
RUNNING=false;
}
catch(RuntimeException re)
{
System.out.print("系统在传输过程中发生了运行时错误,尝试自动修复 ...\n");
RUNNING=false;
}
catch(Error er)
{
System.out.println("系统在运行过程中发生严重错误, 试图自动修复...");
RUNNING=false;
}
catch(Exception e)
{
System.out.print("系统在传输过程中发生了未知错误,尝试自动修复 ...\n");
System.out.println("原因:"+e.getMessage());
e.printStackTrace();
RUNNING=false;
}
finally {
if(ftp != null) try{ ftp.quit(); ftp=null;}catch(Exception f){}
} System.out.print("本次传输结束.\n");
ftp=null;
}
public void doWork(String[] args)
{
System.out.println("MFTP ver 0.62");
if (args.length< 6)
{
System.out.print("Invalid Commandline , please try again.\n");
System.out.print("Usage: host user pass keyword srcpath targetpath\n");
return;
}
else if(args.length == 7) debug=true; hostaddr = args[0];
username = args[1]; password=args[2];
keyword = args[3]; if(args[4].trim().length()>1) srcpath=args[4];
if(args[5].trim().length()>1) targetpath=args[5]; //========= Begin Main Loop ==============
File fd = new File(srcpath);
KwFileFilter kw = new KwFileFilter();
String[] flist =null;
System.out.print("开始监控目录数据,并传输...\n");
while(RUNNING)
{
//System.out.println("文件检查开始 ...");
// flist = fd.list(kw);
flist=fd.list();
if(flist != null)
{
if(flist.length >= 1)
{
System.out.println("\n共检索到"+flist.length+"个文件");
TransferFile(flist);
}
}
try
{
Thread.sleep(1000*60);
}
catch(InterruptedException ie)
{
if(!RUNNING) break;
}
System.out.print(".");
} // end while System.out.println("监控循环已经停止");
//========= End Main Loop ================
} public static void main(String[] args) {
//args : hostaddr, username, password, keyword,srcpath, targetpath
//System.out.println("MFTP ver 0.39\n");
MultiFtp mftp = new MultiFtp();
mftp.doWork(args);
} class KwFileFilter implements FilenameFilter {
public boolean accept(File dir,String name){
if(name.endsWith(".xml")) return true;
if(name.endsWith(".XML")) return true;
return false;
}
}
}
public void actionPerformed(ActionEvent event)
{
new Thread(new Runnable(){
public void run(){
MultiFtp mftp = new MultiFtp();
String [] ss = new String[6];
ss[0] = "";
//...省略
mftp.doWork(ss);}
}).start();
}
});
手写代码,不知道是否正确
public void run(){
MultiFtp mftp = new MultiFtp();
String [] ss = new String[6];
ss[0] = "";
//...省略
mftp.doWork(ss);}
}).start();