當前位置:首頁 » 文件傳輸 » java怎麼用ftp下載文件
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

java怎麼用ftp下載文件

發布時間: 2022-12-11 20:09:07

1. java如何實現txt下載 就是從遠程FTP伺服器上直接把TXT文本下載下來!

strUrl為文件地址,fileName為文件在本地的保存路徑,試試吧~

public static void writeFile(String strUrl, String fileName) {
URL url = null;
try {
url = new URL(strUrl);
} catch (MalformedURLException e2) {
e2.printStackTrace();
}
InputStream is = null;
try {
is = url.openStream();
} catch (IOException e1) {
e1.printStackTrace();
}
OutputStream os = null;
try {
os = new FileOutputStream( fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];

while ((bytesRead = is.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
System.out.println("下載成功:"+strUrl);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

2. 如何在Java程序中實現FTP的上傳下載功能

以下是這三部分的JAVA源程序: (1)顯示FTP伺服器上的文件 void ftpList_actionPerformed(ActionEvent e) {String server=serverEdit.getText();//輸入的FTP伺服器的IP地址 String user=userEdit.getText();//登錄FTP伺服器的用戶名 String password=passwordEdit.getText();//登錄FTP伺服器的用戶名的口令 String path=pathEdit.getText();//FTP伺服器上的路徑 try {FtpClient ftpClient=new FtpClient();//創建FtpClient對象 ftpClient.openServer(server);//連接FTP伺服器 ftpClient.login(user, password);//登錄FTP伺服器 if (path.length()!=0) ftpClient.cd(path); TelnetInputStream is=ftpClient.list(); int c; while ((c=is.read())!=-1) { System.out.print((char) c);} is.close(); ftpClient.closeServer();//退出FTP伺服器 } catch (IOException ex) {;} } (2)從FTP伺服器上下傳一個文件 void getButton_actionPerformed(ActionEvent e) { String server=serverEdit.getText(); String user=userEdit.getText(); String password=passwordEdit.getText(); String path=pathEdit.getText(); String filename=filenameEdit.getText(); try { FtpClient ftpClient=new FtpClient(); ftpClient.openServer(server); ftpClient.login(user, password); if (path.length()!=0) ftpClient.cd(path); ftpClient.binary(); TelnetInputStream is=ftpClient.get(filename); File file_out=new File(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(); } catch (IOException ex) {;} } (3)向FTP伺服器上上傳一個文件 void putButton_actionPerformed(ActionEvent e) { String server=serverEdit.getText(); String user=userEdit.getText(); String password=passwordEdit.getText(); String path=pathEdit.getText(); String filename=filenameEdit.getText(); try { FtpClient ftpClient=new FtpClient(); ftpClient.openServer(server); ftpClient.login(user, password); if (path.length()!=0) ftpClient.cd(path); ftpClient.binary(); TelnetOutputStream os=ftpClient.put(filename); File file_in=new File(filename); FileInputStream is=new FileInputStream(file_in); byte[] bytes=new byte[1024]; int c; while ((c=is.read(bytes))!=-1){ os.write(bytes,0,c);} is.close(); os.close(); ftpClient.closeServer(); } catch (IOException ex) {;} } }

3. java FTP下載文件在代碼中如何實現知道下載完成

(KmConfigkmConfig,StringfileName,StringclientFileName,OutputStreamoutputStream){
try{
StringftpHost=kmConfig.getFtpHost();
intport=kmConfig.getFtpPort();
StringuserName=kmConfig.getFtpUser();
StringpassWord=kmConfig.getFtpPassword();
Stringpath=kmConfig.getFtpPath();
FtpClientftpClient=newFtpClient(ftpHost,port);//ftpHost為FTP伺服器的IP地址,port為FTP伺服器的登陸埠,ftpHost為String型,port為int型。
ftpClient.login(userName,passWord);//userName、passWord分別為FTP伺服器的登陸用戶名和密碼
ftpClient.binary();
ftpClient.cd(path);//path為FTP伺服器上保存上傳文件的路徑。
try{
TelnetInputStreamin=ftpClient.get(fileName);
byte[]bytes=newbyte[1024];
intcnt=0;
while((cnt=in.read(bytes,0,bytes.length))!=-1){
outputStream.write(bytes,0,cnt);
}
//##############################################
//這里文件就已經下載完了,自己理解一下
//#############################################

outputStream.close();
in.close();
}catch(Exceptione){
ftpClient.closeServer();
e.printStackTrace();
}
ftpClient.closeServer();
}catch(Exceptione){
System.out.println("下載文件失敗!請檢查系統FTP設置,並確認FTP服務啟動");
}
}

4. java 下載異地FTP中的zip文件

好像需要一個支持jar包把,把ftp4j的下載地址貼出來

5. Java的ftp操作方法有哪幾種

FTP(File Transfer Protocol)是 Internet 上用來傳送文件的協議(文件傳輸協議)。它是為了我們能夠在 Internet 上互相傳送文件而制定的的文件傳送標准,規定了 Internet 上文件如何傳送。也就是說,通過 FTP 協議,我們就可以跟 Internet 上的 FTP 伺服器進行文件的上傳(Upload)或下載(Download)等動作。

和其他 Internet 應用一樣,FTP 也是依賴於客戶程序/伺服器關系的概念。在 Internet 上有一些網站,它們依照 FTP 協議提供服務,讓網友們進行文件的存取,這些網站就是 FTP 伺服器。網上的用戶要連上 FTP 伺服器,就要用到 FPT 的客戶端軟體,通常 Windows 都有「ftp」命令,這實際就是一個命令行的 FTP 客戶程序,另外常用的 FTP 客戶程序還有 CuteFTP、Ws_FTP、FTP Explorer等。

要連上 FTP 伺服器(即「登陸」),必須要有該 FTP 伺服器的帳號。如果是該伺服器主機的注冊客戶,你將會有一個 FTP 登陸帳號和密碼,就憑這個帳號密碼連上該伺服器。但 Internet 上有很大一部分 FTP 伺服器被稱為「匿名」(Anonymous)FTP 伺服器。這類伺服器的目的是向公眾提供文件拷貝服務,因此,不要求用戶事先在該伺服器進行登記注冊。

Anonymous(匿名文件傳輸)能夠使用戶與遠程主機建立連接並以匿名身份從遠程主機上拷貝文件,而不必是該遠程主機的注冊用戶。用戶使用特殊的用戶名「anonymous」和「guest」就可有限制地訪問遠程主機上公開的文件。現在許多系統要求用戶將Emai1地址作為口令,以便更好地對訪問進行跟綜。出於安全的目的,大部分匿名FTP主機一般只允許遠程用戶下載(download)文件,而不允許上載(upload)文件。也就是說,用戶只能從匿名FTP主機拷貝需要的文件而不能把文件拷貝到匿名FTP主機。另外,匿名FTP主機還採用了其他一些保護措施以保護自己的文件不至於被用戶修改和刪除,並防止計算機病毒的侵入。在具有圖形用戶界面的 WorldWild Web環境於1995年開始普及以前,匿名FTP一直是Internet上獲取信息資源的最主要方式,在Internet成千上萬的匿名PTP主機中存儲著無以計數的文件,這些文件包含了各種各樣的信息,數據和軟體。 人們只要知道特定信息資源的主機地址, 就可以用匿名FTP登錄獲取所需的信息資料。雖然目前使用WWW環境已取代匿名FTP成為最主要的信息查詢方式,但是匿名FTP仍是 Internet上傳輸分發軟體的一種基本方法

6. 用java完成文件ftp上傳下載

使用rt.jar中的sun.net.FtpClient類來實現對FTP伺服器的文件上傳下載

7. java ftp下載

這個和ftp沒有太大關系,只是一個普通的下載,java連接ftp伺服器傳輸文件是需要提供ip 埠號,用戶名密碼 路徑的,這個只是一個靜態資源,用這個就可以,支持斷點續傳

這邊用到了apachecommons-httpclient-3.1包

importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.RandomAccessFile;

importorg.apache.http.HttpEntity;
importorg.apache.http.HttpResponse;
importorg.apache.http.client.ClientProtocolException;
importorg.apache.http.client.HttpClient;
importorg.apache.http.client.methods.HttpGet;
importorg.apache.http.impl.client.DefaultHttpClient;

@SuppressWarnings("deprecation")
publicclassDownloadTool{
publicstaticvoidmain(String[]args)throwsClientProtocolException,IOException{
Stringurl="
[陽光電影
].神探駕到.BD.720p.國粵雙語中字.mkv";
StringdownFile="d:\aaa.mkv";//本地存放路徑
LongnetFileLenght=getNetFileSize(url);
LonglocalFileLenght=getLocalFileSize(downFile);

if(localFileLenght>=netFileLenght){
System.out.println("已下載完成");
return;
}

System.out.println("netFileLenght:"+netFileLenght+"localFileLenght:"+localFileLenght);
finalHttpClienthttpClient=newDefaultHttpClient();
httpClient.getParams().setIntParameter("http.socket.timeout",5000);

finalHttpGethttpGet=newHttpGet(url);
httpGet.addHeader("Range","bytes="+localFileLenght+"-");

finalHttpResponseresponse=httpClient.execute(httpGet);
finalintcode=response.getStatusLine().getStatusCode();
finalHttpEntityentity=response.getEntity();
System.out.println(code);

if(entity!=null&&code<400){

Filefile=newFile(downFile);
=newRandomAccessFile(file,"rw");
randomAccessFile.seek(localFileLenght);

InputStreaminputStream=entity.getContent();
intb=0;
finalbytebuffer[]=newbyte[1024];
while((b=inputStream.read(buffer))!=-1){
randomAccessFile.write(buffer,0,b);
}

randomAccessFile.close();
inputStream.close();
httpClient.getConnectionManager().shutdown();
System.out.println("下載完成");
}
}

(StringfileName){
Filefile=newFile(fileName);
returnfile.length();
}

(Stringurl){
Longcount=-1L;
finalHttpClienthttpClient=newDefaultHttpClient();
httpClient.getParams().setIntParameter("http.socket.timeout",5000);

finalHttpGethttpGet=newHttpGet(url);
HttpResponseresponse=null;
try{
response=httpClient.execute(httpGet);
finalintcode=response.getStatusLine().getStatusCode();
finalHttpEntityentity=response.getEntity();
if(entity!=null&&code==200){
count=entity.getContentLength();
}
}catch(ClientProtocolExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
httpClient.getConnectionManager().shutdown();
}
returncount;
}
}

8. 用java實現ftp下載,路徑的問題,很暈

ftpClient.changeWorkingDirectory(」20110814「),你應該這么寫,如果用/開頭表示是絕對路徑,而 20110814表示是相對路徑,你看下絕對路徑跟相對路徑就明白了