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

r讀取ftp

發布時間: 2022-11-01 18:48:33

⑴ java獲取ftp文件路徑怎麼寫

package com.weixin.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

import com.weixin.constant.DownloadStatus;
import com.weixin.constant.UploadStatus;

/**
* 支持斷點續傳的FTP實用類
* @version 0.1 實現基本斷點上傳下載
* @version 0.2 實現上傳下載進度匯報
* @version 0.3 實現中文目錄創建及中文文件創建,添加對於中文的支持
*/
public class ContinueFTP {
public FTPClient ftpClient = new FTPClient();

public ContinueFTP(){
//設置將過程中使用到的命令輸出到控制台
this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
}

/**
* 連接到FTP伺服器
* @param hostname 主機名
* @param port 埠
* @param username 用戶名
* @param password 密碼
* @return 是否連接成功
* @throws IOException
*/
public boolean connect(String hostname,int port,String username,String password) throws IOException{
ftpClient.connect(hostname, port);
ftpClient.setControlEncoding("GBK");
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username, password)){
return true;
}
}
disconnect();
return false;
}

/**
* 從FTP伺服器上下載文件,支持斷點續傳,上傳百分比匯報
* @param remote 遠程文件路徑
* @param local 本地文件路徑
* @return 上傳的狀態
* @throws IOException
*/
public DownloadStatus download(String remote,String local) throws IOException{
//設置被動模式
ftpClient.enterLocalPassiveMode();
//設置以二進制方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatus result;

//檢查遠程文件是否存在
FTPFile[] files = ftpClient.listFiles(new String(remote.getBytes("GBK"),"iso-8859-1"));
if(files.length != 1){
System.out.println("遠程文件不存在");
return DownloadStatus.Remote_File_Noexist;
}

long lRemoteSize = files[0].getSize();
File f = new File(local);
//本地存在文件,進行斷點下載
if(f.exists()){
long localSize = f.length();
//判斷本地文件大小是否大於遠程文件大小
if(localSize >= lRemoteSize){
System.out.println("本地文件大於遠程文件,下載中止");
return DownloadStatus.Local_Bigger_Remote;
}

//進行斷點續傳,並記錄狀態
FileOutputStream out = new FileOutputStream(f,true);
ftpClient.setRestartOffset(localSize);
InputStream in = ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=localSize /step;
int c;
while((c = in.read(bytes))!= -1){
out.write(bytes,0,c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
System.out.println("下載進度:"+process);
//TODO 更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
boolean isDo = ftpClient.completePendingCommand();
if(isDo){
result = DownloadStatus.Download_From_Break_Success;
}else {
result = DownloadStatus.Download_From_Break_Failed;
}
}else {
OutputStream out = new FileOutputStream(f);
InputStream in= ftpClient.retrieveFileStream(new String(remote.getBytes("GBK"),"iso-8859-1"));
byte[] bytes = new byte[1024];
long step = lRemoteSize /100;
long process=0;
long localSize = 0L;
int c;
while((c = in.read(bytes))!= -1){
out.write(bytes, 0, c);
localSize+=c;
long nowProcess = localSize /step;
if(nowProcess > process){
process = nowProcess;
if(process % 10 == 0)
System.out.println("下載進度:"+process);
//TODO 更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
boolean upNewStatus = ftpClient.completePendingCommand();
if(upNewStatus){
result = DownloadStatus.Download_New_Success;
}else {
result = DownloadStatus.Download_New_Failed;
}
}
return result;
}

/**
* 上傳文件到FTP伺服器,支持斷點續傳
* @param local 本地文件名稱,絕對路徑
* @param remote 遠程文件路徑,使用/home/directory1/subdirectory/file.ext 按照Linux上的路徑指定方式,支持多級目錄嵌套,支持遞歸創建不存在的目錄結構
* @return 上傳結果
* @throws IOException
*/
public UploadStatus upload(String local,String remote) throws IOException{
//設置PassiveMode傳輸
ftpClient.enterLocalPassiveMode();
//設置以二進制流的方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.setControlEncoding("GBK");
UploadStatus result;
//對遠程目錄的處理
String remoteFileName = remote;
if(remote.contains("/")){
remoteFileName = remote.substring(remote.lastIndexOf("/")+1);
//創建伺服器遠程目錄結構,創建失敗直接返回
if(CreateDirecroty(remote, ftpClient)==UploadStatus.Create_Directory_Fail){
return UploadStatus.Create_Directory_Fail;
}
}

//檢查遠程是否存在文件
FTPFile[] files = ftpClient.listFiles(new String(remoteFileName.getBytes("GBK"),"iso-8859-1"));
if(files.length == 1){
long remoteSize = files[0].getSize();
File f = new File(local);
long localSize = f.length();
if(remoteSize==localSize){
return UploadStatus.File_Exits;
}else if(remoteSize > localSize){
return UploadStatus.Remote_Bigger_Local;
}

//嘗試移動文件內讀取指針,實現斷點續傳
result = uploadFile(remoteFileName, f, ftpClient, remoteSize);

//如果斷點續傳沒有成功,則刪除伺服器上文件,重新上傳
if(result == UploadStatus.Upload_From_Break_Failed){
if(!ftpClient.deleteFile(remoteFileName)){
return UploadStatus.Delete_Remote_Faild;
}
result = uploadFile(remoteFileName, f, ftpClient, 0);
}
}else {
result = uploadFile(remoteFileName, new File(local), ftpClient, 0);
}
return result;
}
/**
* 斷開與遠程伺服器的連接
* @throws IOException
*/
public void disconnect() throws IOException{
if(ftpClient.isConnected()){
ftpClient.disconnect();
}
}

/**
* 遞歸創建遠程伺服器目錄
* @param remote 遠程伺服器文件絕對路徑
* @param ftpClient FTPClient對象
* @return 目錄創建是否成功
* @throws IOException
*/
public UploadStatus CreateDirecroty(String remote,FTPClient ftpClient) throws IOException{
UploadStatus status = UploadStatus.Create_Directory_Success;
String directory = remote.substring(0,remote.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(new String(directory.getBytes("GBK"),"iso-8859-1"))){
//如果遠程目錄不存在,則遞歸創建遠程伺服器目錄
int start=0;
int end = 0;
if(directory.startsWith("/")){
start = 1;
}else{
start = 0;
}
end = directory.indexOf("/",start);
while(true){
String subDirectory = new String(remote.substring(start,end).getBytes("GBK"),"iso-8859-1");
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else {
System.out.println("創建目錄失敗");
return UploadStatus.Create_Directory_Fail;
}
}

start = end + 1;
end = directory.indexOf("/",start);

//檢查所有目錄是否創建完畢
if(end <= start){
break;
}
}
}
return status;
}

/**
* 上傳文件到伺服器,新上傳和斷點續傳
* @param remoteFile 遠程文件名,在上傳之前已經將伺服器工作目錄做了改變
* @param localFile 本地文件File句柄,絕對路徑
* @param processStep 需要顯示的處理進度步進值
* @param ftpClient FTPClient引用
* @return
* @throws IOException
*/
public UploadStatus uploadFile(String remoteFile,File localFile,FTPClient ftpClient,long remoteSize) throws IOException{
UploadStatus status;
//顯示進度的上傳
long step = localFile.length() / 100;
long process = 0;
long localreadbytes = 0L;
RandomAccessFile raf = new RandomAccessFile(localFile,"r");
OutputStream out = ftpClient.appendFileStream(new String(remoteFile.getBytes("GBK"),"iso-8859-1"));
//斷點續傳
if(remoteSize>0){
ftpClient.setRestartOffset(remoteSize);
process = remoteSize /step;
raf.seek(remoteSize);
localreadbytes = remoteSize;
}
byte[] bytes = new byte[1024];
int c;
while((c = raf.read(bytes))!= -1){
out.write(bytes,0,c);
localreadbytes+=c;
if(localreadbytes / step != process){
process = localreadbytes / step;
System.out.println("上傳進度:" + process);
//TODO 匯報上傳狀態
}
}
out.flush();
raf.close();
out.close();
boolean result =ftpClient.completePendingCommand();
if(remoteSize > 0){
status = result?UploadStatus.Upload_From_Break_Success:UploadStatus.Upload_From_Break_Failed;
}else {
status = result?UploadStatus.Upload_New_File_Success:UploadStatus.Upload_New_File_Failed;
}
return status;
}

public static void main(String[] args) {
ContinueFTP myFtp = new ContinueFTP();
try {
System.err.println(myFtp.connect("10.10.6.236", 21, "5", "jieyan"));
// myFtp.ftpClient.makeDirectory(new String("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.changeWorkingDirectory(new String("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.makeDirectory(new String("愛你等於愛自己".getBytes("GBK"),"iso-8859-1"));
// System.out.println(myFtp.upload("E:\\yw.flv", "/yw.flv",5));
// System.out.println(myFtp.upload("E:\\愛你等於愛自己.mp4","/愛你等於愛自己.mp4"));
//System.out.println(myFtp.download("/愛你等於愛自己.mp4", "E:\\愛你等於愛自己.mp4"));
myFtp.disconnect();
} catch (IOException e) {
System.out.println("連接FTP出錯:"+e.getMessage());
}
}
}

⑵ windows命令行怎麼使用ftp

1、首先,同時按下快捷鍵
Win+R,調出運行窗口,然後在運行窗口中輸入
FTP,回車。
2、這時,將會打開
FTP
命令行窗口。在命令行窗口中輸入以下命令並回車,以便連接到伺服器open
伺服器地址。
3、連接到伺服器後,輸入FTP用戶名並回車。
4、然後,再輸入FTP密碼並回車,在此注意,輸入密碼時不會有任何提示。
5、如果用戶名和密碼都正確無誤,網路也沒有問題,很快便會出現登陸成功的提示信息。

⑶ 用JAVA獲取FTP文件列表

學習一下ftp協議,然後用socket來模擬就可以了

⑷ 讀取ftp文件最後一行以後報錯,無法訪問已釋放的對象。 對象名:System.Net.Sockets.NetworkStream

while ((strLine = reader.ReadLine()) != null) //這里報錯 讀取到最後一的時候
會這樣是因為當讀取流讀取到最後一行內容後就關閉了文件了
雖然在正常的讀取流中會在讀取完內容後返回null
但再ftp文件的讀取中,讀取完最後一行後再讀取就會拋出這個異常
其實用您代碼注釋的這句//string strfs = reader.ReadToEnd();來讀取就可以了
如果要分行處理直接用分行符分割一下就可以了

⑸ ftp伺服器怎麼搭建

ftp伺服器搭建遇到此問題。

電腦:華為MateBook

系統:Windows10

軟體:ftp伺服器

1、WIN+R打開運行,輸入appwiz.cpl,打開程序和功能。

⑹ R語言如何從外部讀取數據到R中

R語言如何從外部讀取數據到R中
R語言可以從鍵盤,文本,excel,access,資料庫,專業處理軟體sas

一、使用鍵盤的輸入
mydata<-data.frame(age=numeric(0),gender=character(0),weight=numeric(0))
mydata<-edit(mydata)
二、讀入帶有分隔符文本格式的數據
data<-read.table(文件,header=true/false,sep="delimeter",row.names=列名)
其中文件可以有很多選項的
file()gzfile(),bzfile(),等一些壓縮文件以及url(http://,ftp://,smtp://)
例子:
默認的時候,字元串會自動使用factor轉化為數值型
data<-read.table("student.csv",header=TRUE,sep=",",row.names="studentid",stringsAsFactors=FALSE)
三、將xls文件導入到R中
(1)將xls變成csv的格式導入
(2)在Windows系統中,你也可以使用RODBC包來訪問Excel文件。
library(RODBC)
channel <- odbcConnectExcel("student.xls")
mydataframe<-sqlFetch(channel,"Sheet1")
odbcClose(channel)
四、抓取網頁並且提取信息
五、導入spss數據
library(Hmisc)
mydata<-spss.get("mydata.sav",use.value.labels=TRUE)
六、導入SAS數據
將sas格式的數據轉換為csv格式的數據 然後用read.table()形式導入
七、導入關系型資料庫的數據
R中有多種面向關系型資料庫管理系統(DBMS)的介面,包括Microsoft SQL Server、Microsoft Access、MySQL、Oracle、PostgreSQL、DB2、Sybase、Teradata以及SQLite。其中一些包通過原生的資料庫驅動來提供訪問功能,另一些則是通過ODBC或JDBC來實現訪問的。
(1)使用ODBC的方式導入數據

⑺ vim 如何象本地目錄一樣打開遠程ftp文件

打開:
:e ftp://192.168.10.76/abc.txt
保存(如果不存在則創建):
:w ftp://192.168.10.76/abc.txt
讀取:
:r ftp://192.168.10.76/abc.txt
以上操作第一次打開時需要輸入用戶名和密碼,
以後就不用了,vim 會記住的。

總之,大多數針對文件的操作都可以直接用來操作 ftp 文件,
vim 通過解析文件名中的 URL 標記來區別。

其實打開文件還有一個比較方便的辦法,
那就是當你編輯的文件中包含有類似於 ftp://192.168.10.76/abc.txt 這樣的文件 URL 時,
你直接把游標挪到上面,再用 gf 命令就可以打開了。

需要注意的一點是,
vim 本身並不攜帶 ftp 客戶端,vim 只是調用操作系統提供的 ftp 客戶端並捕獲它的輸出。不過大多數操作系統都是有 ftp 客戶端的,所以這一點不用擔心。

⑻ windowsftp如何獲取文件夾下所有的文件

可以通過命令窗口來打開所有的文件。具體步驟如下:
點擊win+R後輸入cmd打開dos命令窗口。
打開需獲取文件名的位置。
獲取名稱,命令格式:dir/b文件目標盤符文件夾位置(可省略)目標名稱.目標後綴。
獲取文件大小及文件名、修改時間(文件大小需處理)。
Microsoft Surface是一個由微軟所開發的第一款平面電腦,結合硬體與軟體的新技術,用家可以直接用手或聲音對屏幕作出指令,觸摸和其他外在物理物來和電腦進行交互,毋須再依賴會令手部勞損的滑鼠與鍵盤。

⑼ 用java怎麼獲取ftp上的文件

public class FtpClientUtil {
FtpClient ftpClient;
private String server;
private int port;
private String userName;
private String userPassword;

public FtpClientUtil(String server,int port,String userName,String userPassword)
{
this.server=server;
this.port=port;
this.userName=userName;
this.userPassword=userPassword;
}
/**
* 鏈接到伺服器
* @return
*/
public boolean open()
{
if(ftpClient!=null&&ftpClient.serverIsOpen())
return true;
try
{
ftpClient= new FtpClient();
ftpClient.openServer(server,port);
ftpClient.login(userName, userPassword);
ftpClient.binary();
return true;
}
catch(Exception e)
{
e.printStackTrace();
ftpClient=null;
return false;
}
}

public boolean cd(String dir){
boolean f = false;
try {
ftpClient.cd(dir);
} catch (IOException e) {
Logs.error(e.toString());
return f;
}
return true;
}

/**
* 上傳文件到FTP伺服器
* @param localPathAndFileName 本地文件目錄和文件名
* @param ftpFileName 上傳後的文件名
* @param ftpDirectory FTP目錄如:/path1/pathb2/,如果目錄不存在回自動創建目錄
* @throws Exception
*/
public boolean upload(String localDirectoryAndFileName,String ftpFileName,String ftpDirectory)throws Exception {
if(!open())
return false;
FileInputStream is=null;
TelnetOutputStream os=null;
try
{
char ch = ' ';
if (ftpDirectory.length() > 0)
ch = ftpDirectory.charAt(ftpDirectory.length() - 1);
for (; ch == '/' || ch == '\\'; ch = ftpDirectory.charAt(ftpDirectory.length() - 1))
ftpDirectory = ftpDirectory.substring(0, ftpDirectory.length() - 1);

int slashIndex = ftpDirectory.indexOf(47);
int backslashIndex = ftpDirectory.indexOf(92);
int index = slashIndex;
String dirall = ftpDirectory;
if (backslashIndex != -1 && (index == -1 || index > backslashIndex))
index = backslashIndex;
String directory = "";
while (index != -1) {
if (index > 0) {
String dir = dirall.substring(0, index);
directory = directory + "/" + dir;
ftpClient.sendServer("XMKD " + directory + "\r\n");
ftpClient.readServerResponse();
}
dirall = dirall.substring(index + 1);
slashIndex = dirall.indexOf(47);
backslashIndex = dirall.indexOf(92);
index = slashIndex;
if (backslashIndex != -1 && (index == -1 || index > backslashIndex))
index = backslashIndex;
}
ftpClient.sendServer("XMKD " + ftpDirectory + "\r\n");
ftpClient.readServerResponse();

os = ftpClient.put(ftpDirectory + "/"
+ ftpFileName);
File file_in = new File(localDirectoryAndFileName);
is = new FileInputStream(file_in);
byte bytes[] = new byte[1024];
int i;
while ((i = is.read(bytes)) != -1)
os.write(bytes, 0, i);
//清理垃圾

return true;
}
catch(Exception e)
{
e.printStackTrace();
return false;
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();
}
}
/**
* 從FTP伺服器上下載文件並返回下載文件長度
* @param ftpDirectoryAndFileName
* @param localDirectoryAndFileName
* @return
* @throws Exception
*/
public long download(String ftpDirectoryAndFileName,String localDirectoryAndFileName)throws Exception
{
long result = 0;
if(!open())
return result;
TelnetInputStream is = null;
FileOutputStream os = null;
try
{
is = ftpClient.get(ftpDirectoryAndFileName);
java.io.File outfile = new java.io.File(localDirectoryAndFileName);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1)
{
os.write(bytes, 0, c);
result = result + c;
}
}
catch (Exception e)
{
throw e;
}
finally
{
if (is != null)
is.close();
if (os != null)
os.close();

}
return result;
}
/**
* 返回FTP目錄下的文件列表
* @param ftpDirectory
* @return
*/
public List<String> getFileNameList(String ftpDirectory)
{
List<String> list = new ArrayList<String>();
if(!open())
return list;
try
{
DataInputStream dis = new DataInputStream(ftpClient.nameList(ftpDirectory));
String filename = "";
while((filename=dis.readLine())!=null)
{
list.add(filename);
}
} catch (Exception e)
{
e.printStackTrace();
}
return list;
}
/**
* 刪除FTP上的文件
* @param ftpDirAndFileName
*/
public boolean deleteFile(String ftpDirAndFileName)
{
if(!open())
return false;
ftpClient.sendServer("DELE "+ftpDirAndFileName+"\r\n");
return true;
}
/**
* 刪除FTP目錄
* @param ftpDirectory
*/
public boolean deleteDirectory(String ftpDirectory)
{
if(!open())
return false;
ftpClient.sendServer("XRMD "+ftpDirectory+"\r\n");
return true;
}
/**
* 關閉鏈接
*/
public void close()
{
try
{
if(ftpClient!=null&&ftpClient.serverIsOpen())
ftpClient.closeServer();
}catch(Exception e)
{

}
}
}望採納,謝謝。

⑽ 如何創建HTP伺服器

首先我們在server機器上創建兩個用戶!這些用戶是用來登錄到FTP的!我的電腦右鍵->管理->本地用戶和組->用戶->「右鍵」新建用戶->輸入用戶名和密碼再點創建就行了!
在C盤新建文件夾「C:\FTP上傳」,並在此文件下新建「LocalUser」文件夾,並在此文件夾下創建「Public」(這個是公共文件夾)、「xxx」和「xxx1」文件夾(xxx和XXX1必須與創建的本地用戶相同)。在C盤新建文件夾「C:\FTP下載」!並在每個文件夾里放不同的文件,以便區分
安裝IIS組件!在開始菜單里—>控制面板-〉添加或刪除程序->添加/刪除windows組件->應用程序伺服器->詳細信息->IIS-〉詳細信息-〉FTP-〉確定-〉完成!這樣就把FTP安裝在伺服器上了!
配置FTP伺服器,創建上傳和下載服務!創建上傳伺服器:右鍵FTP站點->選擇新建FTP站點->描述可以根據自己的需要填寫->地址一般都是server的地址,埠就用默認的21吧->因為是提供上傳服務的,所以就用隔離用戶啦->它的目錄指向「C:\FTP上傳」->訪問許可權要鉤上「讀取」和「寫入」啦(圖片上錯了的~)->點擊完成就把上傳的服務創建好了!創建下載伺服器:因為21號埠已經被佔用所以我們就用2121埠!不隔離用戶!它的目錄指向「C:\FTP下載」!只有讀取許可權!就簡單的幾步我們就把一個FTP伺服器創建好了!
在XP主機上測試已經完成安裝的FTP伺服器!我們在XP主機上輸入FTP伺服器的IP地址FTP://192.168.1.2就能打開上傳的FTP頁面,輸入FTP://192.168.1.2:2121就能打開下載頁面了!我們還可以用之前創建的那兩個用戶去登錄FTP伺服器!因為只有上傳服務提供了用戶文件夾!所以只有登錄到上傳服務時才能打開用戶文件夾!這個文件夾是用專用的,其它用是無法對裡面的文件進行操作的!我們還能成功地在下載伺服器里下載文件到XP主機上了!