當前位置:首頁 » 文件管理 » ftpjava下載文件

ftpjava下載文件

發布時間: 2022-12-27 11:51:27

『壹』 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();
}
}

『貳』 java 使用ftp 下載文件在windows環境下正常,在linux下載不了

是不是文件的讀寫許可權問題,linux的許可權控制比windows嚴格的多

『叄』 看了一段java代碼是從FTP上下載文件,ftpClient.setBufferSize()這個是什麼用處,要怎麼使用它

緩沖區大小,等從ftp下載的數據存儲到緩沖區,等緩沖區滿了,進行磁碟讀寫。

『肆』 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;
}
}

『伍』 java ftp批量下載異常

我之前也遇到過這樣的事,通過FTP獲取文件的二進制流有限制,獲取第二個流的時候需要斷掉鏈接後再重新連接伺服器讀取流

『陸』 java ftp連接一次下載多個文件

/**
*參考例子如下:
*@paramnamelist下載的文件列表
*@parampath下載路徑
*@paramzipname壓縮文件名稱
*/
publicvoidzipDownloadFile(HttpServletResponseresponse,List<string>namelist,Stringpath,Stringzipname){
byte[]buf=newbyte[1024];

try{

//本地保存設置
response.addHeader("Content-Disposition","attachment;filename="
+URLEncoder.encode(zipname,sysEncoding)+".zip");

response.setContentType("application/x-zip-compressed");

//向本地寫文件
ServletOutputStreamsos=response.getOutputStream();


ZipOutputStreamzipOut=newZipOutputStream(newBufferedOutputStream(sos));


for(Stringname:namelist){

ZipEntryentry=newZipEntry(name);
zipOut.putNextEntry(entry);

InputStreambis=this.getStream(path,name);
if(bis!=null){
intreadLen=-1;

while((readLen=bis.read(buf,0,1024))!=-1){
zipOut.write(buf,0,readLen);
}

bis.close();
}
}

zipOut.close();


}catch(Exceptione){

e.printStackTrace();
}

}</string>

『柒』 ftp java 下載的時候怎麼保存到指定位置

通過工具類來實現本地路徑定義和下載即可。

import hkrt.b2b.view.util.Log;
import hkrt.b2b.view.util.ViewUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class CCFCCBFTP {

/**
* 上傳文件
*
* @param fileName
* @param plainFilePath 明文文件路徑路徑
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上傳文件開始");
Log.info("連接遠程上傳伺服器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("檢查文件路徑是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查詢文件路徑不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上傳文件成功:"+fileName+"。文件保存路徑:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}

}

/**
*下載並解壓文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String SFP = System.getProperty("file.separator");
String bl = "false";
try {
Log.info("下載並解密文件開始");
Log.info("連接遠程下載伺服器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
CMBCUtil.CMBCLOGINPASSWORD);

FTPFile[] fs;

ftpClient.makeDirectory(routeFilepath);
ftpClient.changeWorkingDirectory(routeFilepath);
bl = "false";
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true";
Log.info("下載文件開始。");
ftpClient.setBufferSize(1024);
// 設置文件類型(二進制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
fos = new FileOutputStream(localFilePath+SFP+fileName);
fos.write(bos.toByteArray());
Log.info("下載文件結束:"+localFilePath);
}
}
Log.info("檢查文件是否存:"+fileName+" "+bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查詢無結果,請稍後再查詢。");
return bl;
}
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}

// 調用樣例:
public static void main(String[] args) {
try {

// 明文文件路徑
String plainFilePath = "D:/req_20150204_0011.txt";
// 密文文件路徑
String secretFilePath = "req_20150204_00134.txt";
fileDownloadByFtp("D://123.zip","123.zip","req/20150228");
} catch (Exception e) {
e.printStackTrace();
}
}

}

『捌』 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服務啟動");
}
}

『玖』 我在用JAVA寫FTP下載的時候出現這個問題

估計是C:\Documents and Settings\Administrator\Application Data\這個文件夾的問題,應該是windows系統指定用來存放某些文件的目錄。那你就不要非得放在這個目錄好了,不要在一棵樹上弔死。

『拾』 java 下載異地FTP中的zip文件

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

熱點內容
數控車床電腦編程 發布:2025-08-23 00:40:32 瀏覽:241
安卓手機如何一鍵修改序列號 發布:2025-08-23 00:31:33 瀏覽:426
vsqt編譯通過 發布:2025-08-23 00:29:58 瀏覽:458
android設置sd卡 發布:2025-08-23 00:18:47 瀏覽:861
混合存儲提供商 發布:2025-08-23 00:06:08 瀏覽:434
行鎖演算法 發布:2025-08-23 00:05:05 瀏覽:901
手機編程蘋果 發布:2025-08-23 00:02:41 瀏覽:285
腳本會被殺毒軟體 發布:2025-08-22 23:56:03 瀏覽:138
gta5車文件夾 發布:2025-08-22 23:47:58 瀏覽:182
編譯標准 發布:2025-08-22 23:45:59 瀏覽:171