我自己的代码都只能够实现把文件下载到我所建立的这个JAVA项目中的文件夹里 我不想让他那么死 想灵活一些 让他下载入我指定的文件 如何实现呢?
我是希望能把文件下载到本地计算机指定的目录中去 比如下到“D:/”
我的下载代码是
FtpClient ftp = new FtpClient(host);
ftp.login(username, password);
ftp.binary();
ftp.cd(path);
while((s=dr.readLine())!=null) {
System.out.println(a(s)); getfile = ftp.get(a(s));
str = ftp.getResponseString ();
System.out.println(str);
FileOutputStream output = new FileOutputStream (new File (a(s)));
while( true )
{
int y = getfile.read();
if( y == -1 ) break;
else
{
output.write ((byte)y);
output.flush();
}
}
getfile.close();
output.close ();
}public static String a(String s) {
String s1="";
StringTokenizer st=new StringTokenizer(s," ");
s1=st.nextToken();
while(st.hasMoreTokens())
{ s=st.nextToken();
}
if (s1.indexOf("-")!=-1)
s2=s ;
else
s2 = null;
return s1.indexOf("-")==-1?"Folder:"+s:"File:"+s; }
我是希望能把文件下载到本地计算机指定的目录中去 比如下到“D:/”
我的下载代码是
FtpClient ftp = new FtpClient(host);
ftp.login(username, password);
ftp.binary();
ftp.cd(path);
while((s=dr.readLine())!=null) {
System.out.println(a(s)); getfile = ftp.get(a(s));
str = ftp.getResponseString ();
System.out.println(str);
FileOutputStream output = new FileOutputStream (new File (a(s)));
while( true )
{
int y = getfile.read();
if( y == -1 ) break;
else
{
output.write ((byte)y);
output.flush();
}
}
getfile.close();
output.close ();
}public static String a(String s) {
String s1="";
StringTokenizer st=new StringTokenizer(s," ");
s1=st.nextToken();
while(st.hasMoreTokens())
{ s=st.nextToken();
}
if (s1.indexOf("-")!=-1)
s2=s ;
else
s2 = null;
return s1.indexOf("-")==-1?"Folder:"+s:"File:"+s; }
File file_out = new File(SavePath+FileName);//这里指定路径呀
FileOutputStream os = new FileOutputStream(file_out);
getfile = ftp.get(filename3);
str = ftp.getResponseString ();
System.out.println(str);
File file_out = new File("D:/"+filename3);
FileOutputStream os = new FileOutputStream(file_out);
// FileOutputStream output = new FileOutputStream (new File (filename3));
while( true )
{
int y = getfile.read();
if( y == -1 ) break;
else
{
os.write ((byte)y);
os.flush();
//output.flush();
}
}
getfile.close();
os.close ();
楼上帮我看下 我哪里写错了么?
import sun.net.ftp.*;
import sun.net.*;
import java.io.*;public class FTPServer {
public static FTPServer instance; private int Port=21;
private FTPServer() {
}
public static FTPServer getInstance()
{
if(instance==null)
instance=new FTPServer();
return instance;
} public void setPort(int Port)
{
this.Port=Port;
}
/**
* Server:FTP服务器地址
* User:FTP用户
* Pass:密码
*
* */
public int DownLoadFile(String Server,String User,String Pass,String FileName,String FtpPath,String SavePath)
{
int Ret=0; //成功
String server=Server;
String user=User;
String password=Pass;
String path=FtpPath; try {
FtpClient ftpClient = new FtpClient();
//创建FtpClient对象
ftpClient.openServer(server,this.Port);
//连接FTP服务器
ftpClient.login(user, password);
//登录FTP服务器
if (path.length() != 0) ftpClient.cd(path); //下载一个文件
ftpClient.binary(); TelnetInputStream is = ftpClient.get(FileName); File file_out = new File(SavePath+FileName);
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ( (c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close(); ftpClient.closeServer(); //退出FTP服务器
} catch (Exception ex) {
Ret=-1;
System.out.println(ex.getMessage());
} return Ret;
}
public static void main(String[] args)
{
String server="192.100.100.81";
String user="anonymous";
String password="0";
String path="/xcl";
String filename="SdFtpBszgxx.TaxBank.13701000000.200602.csv";
String savePath="e:/";
getInstance().DownLoadFile(server,user,password,filename,path,savePath);
}
}