我自己的代码都只能够实现把文件下载到我所建立的这个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; }
解决方案 »
- 怎样学习 java 的GUI编程
- 我写的java程序怎么才能生成exe供其它用户使用??
- 下载了一份 eXtremeComponents_pdf 文件怎么打开?麻烦大家了,谢谢。
- 图形编程:三点画弧 算法
- 大家有没JCDSee1.0的源代码?*********
- 怎样学java
- 到达一个iterator的末尾,如何回到开始再次使用next进行循环
- 一道简单的程序员考题(java)
- Java文件无法编译
- 大虾们,快来帮帮我把,这段代码怎么了?
- net.sourceforge.jtds.jdbc.Driver 问题
- 使用JAVA语言 如何将文件从服务器下载入指定的本地文件夹内?(FTP) 急用 在线等
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);
}
}