當前位置:首頁 » 文件管理 » javaftp生成文件

javaftp生成文件

發布時間: 2022-12-08 14:24:00

java中怎麼實現ftp文件傳輸

packagecom.quantongfu.ftp.ftp;

importjava.io.File;
importjava.io.FileInputStream;
importjava.io.IOException;
importjava.net.ServerSocket;
importjava.util.List;

importorg.apache.commons.net.ftp.FTPReply;
importorg.apache.log4j.Logger;
importorg.apache.log4j.net.SocketServer;

importcom.quantongfu.conf.FtpConf;

/**
*@項目名稱:telinSyslog
*@文件名稱:Ftp.java
*@創建日期:2015年9月14日下午3:22:08
*@功能描述:ftp實體類,用於連接,上傳
*@修訂記錄:
*/
publicclassFtp{
privatestaticLoggerlogger=Logger.getLogger(Ftp.class);
privateFTPClientftp;

/**
*
*@parampath
*上傳到ftp伺服器哪個路徑下
*@paramaddr
*地址
*@paramport
*埠號
*@paramusername
*用戶名
*@parampassword
*密碼
*@return
*@throwsException
*/
publicbooleanconnect()throwsException{
booleanresult=false;
ftp=newFTPClient();
intreply;
ftp.connect(FtpConf.FTP_HOST,FtpConf.FTP_PORT);
ftp.login(FtpConf.FTP_USER_NAME,FtpConf.FTP_PASSWORD);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
ftp.setDataTimeout(60000);
reply=ftp.getReplyCode();
if(!FTPReply.isPositiveCompletion(reply)){
ftp.disconnect();
returnresult;
}
if(FtpConf.IS_FTP_DIRECTORY){
ftp.changeWorkingDirectory(FtpConf.FTP_DIRECTORY);
}
result=true;
returnresult;
}

/**
*
*@paramfiles
*上傳的文件
*@throwsException
*/
publicbooleanupload(Filefile)throwsIOException{
FileInputStreaminput=null;
try{
input=newFileInputStream(file);
booleanb=ftp.storeFile(file.getName()+".tmp",input);
if(b){
b=ftp.rename(file.getName()+".tmp",file.getName());
}
returnb;
}catch(Exceptione){
e.printStackTrace();
returnfalse;
}finally{
if(input!=null){
input.close();
}
}
}

/**
*
*@paramfiles
*上傳的文件
*@throwsException
*/
publicbooleanupload(ServerSocketserver,Filefile)throwsException{
FileInputStreaminput=null;
try{
if(!file.exists()){
returntrue;
}
input=newFileInputStream(file);
booleanb=ftp.storeFile(server,file.getName()+".tmp",input);
if(b){
b=ftp.rename(file.getName()+".tmp",file.getName());
if(b){
file.delete();
}
}
returnb;
}catch(Exceptione){
logger.error("ftperror"+e.getMessage());
returnfalse;
}finally{
if(input!=null){
try{
input.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}
/*斷開連接*/
publicvoiddisConnect(){
try{
if(ftp!=null){
ftp.disconnect();
}
}catch(IOExceptione){
e.printStackTrace();
}

}
/*獲取連接*/
publicstaticFtpgetFtp(){
Ftpftp=newFtp();
try{
ftp.connect();
}catch(Exceptione){
logger.error("FTP連接異常"+e.getMessage());
e.printStackTrace();
}
returnftp;
}
/*重連*/
publicFtpreconnect(){
disConnect();
returngetFtp();
}
}

使用Apache FtpClient jar包,獲取jar : http://commons.apache.org/net/

㈡ 怎樣通過java定時將資料庫中的信息導出後生成xml文件,並通過ftp上傳到指定的位置

1、使用quarz或者jdk的timer來制定定時任務。
2、使用jdbc或者hibernate等方法獲取資料庫中信息。
3、使用xmlbeans或者dom4j等技術生成xml文件。
4、使用sun.net.ftp.FtpClient上傳到指定ftp伺服器。

㈢ 可以用java代碼在FTP上創建excel文件嗎

可以曲線救國啊,在本地建一個excel文件,再傳到指定目錄,OK,功能實現了

㈣ java commons-net如何在ftp上創建xml文件

一般不這么做, 一般是先在本地創建生成一個文件, 然後再將文件 put 到ftp上去
你想啊, 如果到時候出現了網路異常, 要是有個原始文件在本地,事後直接補上去即可
要是直接在ftp上創建的話,補的話還得再重新跑程序

不過既然你問了, 二般是可以實現的

㈤ java怎麼打開FTP伺服器上的文件

http的話就用
httpclient
。open後,可以返回一個
InputStream
。這個就是你要讀到
文件流

原理的話,參考你用瀏覽器打開這個鏈接顯示的內容。
這個返回的是一個HTML網頁,需要你解析出裡面的文字(一般來說取body中間的內容就行)
其實對於這種文件一般用FTP來下載的。樓上寫的那個不對,哈哈。
需要的話自己最好去查一下,怎麼用,我有代碼,不過告訴你的話也不太好?
URL
url
=
new
URL("http://你的地址");
URLConnection
connection

=
url.openConnection();
InputStream
is
=
connection.getInputStream();
BufferedReader
br
=
new
BufferedReader(new
InputStreamReader(is,"gb2312"));
下面就是解析這個字元串來,自己來吧

㈥ JAVA FTP創建文件

File file=new File("ftp://172.16.8.123/123/test.txt");
if(!file.exists()){
file.createNewFile();
}

這個樣子不行嗎?
由於沒有ftp伺服器 不能驗證 你試試吧

㈦ java 實現ftp上傳如何創建文件夾

這個功能我也剛寫完,不過我也是得益於同行,現在我也把自己的分享給大家,希望能對大家有所幫助,因為自己的項目不涉及到創建文件夾,也僅作分享,不喜勿噴謝謝!

interface:
packagecom.sunline.bank.ftputil;

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importorg.apache.commons.net.ftp.FTPClient;

publicinterfaceIFtpUtils{
/**
*ftp登錄
*@paramhostname主機名
*@paramport埠號
*@paramusername用戶名
*@parampassword密碼
*@return
*/
publicFTPClientloginFtp(Stringhostname,Integerport,Stringusername,Stringpassword);
/**
*上穿文件
*@paramhostname主機名
*@paramport埠號
*@paramusername用戶名
*@parampassword密碼
*@paramfpathftp路徑
*@paramlocalpath本地路徑
*@paramfileName文件名
*@return
*/
(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName);
/**
*批量下載文件
*@paramhostname
*@paramport
*@paramusername
*@parampassword
*@paramfpath
*@paramlocalpath
*@paramfileName源文件名
*@paramfilenames需要修改成的文件名
*@return
*/
publicbooleandownloadFileList(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName,Stringfilenames);
/**
*修改文件名
*@paramlocalpath
*@paramfileName源文件名
*@paramfilenames需要修改的文件名
*/
(Stringlocalpath,StringfileName,Stringfilenames);
/**
*關閉流連接、ftp連接
*@paramftpClient
*@parambufferRead
*@parambuffer
*/
publicvoidcloseFtpConnection(FTPClientftpClient,,BufferedInputStreambuffer);
}

impl:
packagecom.sunline.bank.ftputil;

importjava.io.BufferedInputStream;
importjava.io.BufferedOutputStream;
importjava.io.File;
importjava.io.FileInputStream;
importjava.io.FileOutputStream;
importjava.io.IOException;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPFile;
importorg.apache.commons.net.ftp.FTPReply;
importcommon.Logger;

{
privatestaticLoggerlog=Logger.getLogger(FtpUtilsImpl.class);
FTPClientftpClient=null;
Integerreply=null;

@Override
publicFTPClientloginFtp(Stringhostname,Integerport,Stringusername,Stringpassword){
ftpClient=newFTPClient();
try{
ftpClient.connect(hostname,port);
ftpClient.login(username,password);
ftpClient.setControlEncoding("utf-8");
reply=ftpClient.getReplyCode();
ftpClient.setDataTimeout(60000);
ftpClient.setConnectTimeout(60000);
//設置文件類型為二進制(避免解壓縮文件失敗)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
//開通數據埠傳輸數據,避免阻塞
ftpClient.enterLocalActiveMode();
if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
log.error("連接FTP失敗,用戶名或密碼錯誤");
}else{
log.info("FTP連接成功");
}
}catch(Exceptione){
if(!FTPReply.isPositiveCompletion(reply)){
try{
ftpClient.disconnect();
}catch(IOExceptione1){
log.error("登錄FTP失敗,請檢查FTP相關配置信息是否正確",e1);
}
}
}
returnftpClient;
}

@Override
@SuppressWarnings("resource")
(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName){
booleanflag=false;
ftpClient=loginFtp(hostname,port,username,password);
BufferedInputStreambuffer=null;
try{
buffer=newBufferedInputStream(newFileInputStream(localpath+fileName));
ftpClient.changeWorkingDirectory(fpath);
fileName=newString(fileName.getBytes("utf-8"),ftpClient.DEFAULT_CONTROL_ENCODING);
if(!ftpClient.storeFile(fileName,buffer)){
log.error("上傳失敗");
returnflag;
}
buffer.close();
ftpClient.logout();
flag=true;
returnflag;
}catch(Exceptione){
e.printStackTrace();
}finally{
closeFtpConnection(ftpClient,null,buffer);
log.info("文件上傳成功");
}
returnfalse;
}

@Override
publicbooleandownloadFileList(Stringhostname,Integerport,Stringusername,Stringpassword,Stringfpath,Stringlocalpath,StringfileName,Stringfilenames){
ftpClient=loginFtp(hostname,port,username,password);
booleanflag=false;
=null;
if(fpath.startsWith("/")&&fpath.endsWith("/")){
try{
//切換到當前目錄
this.ftpClient.changeWorkingDirectory(fpath);
this.ftpClient.enterLocalActiveMode();
FTPFile[]ftpFiles=this.ftpClient.listFiles();
for(FTPFilefiles:ftpFiles){
if(files.isFile()){
System.out.println("=================="+files.getName());
FilelocalFile=newFile(localpath+"/"+files.getName());
bufferRead=newBufferedOutputStream(newFileOutputStream(localFile));
ftpClient.retrieveFile(files.getName(),bufferRead);
bufferRead.flush();
}
}
ftpClient.logout();
flag=true;

}catch(IOExceptione){
e.printStackTrace();
}finally{
closeFtpConnection(ftpClient,bufferRead,null);
log.info("文件下載成功");
}
}
modifiedLocalFileName(localpath,fileName,filenames);
returnflag;
}

@Override
(Stringlocalpath,StringfileName,Stringfilenames){
Filefile=newFile(localpath);
File[]fileList=file.listFiles();
if(file.exists()){
if(null==fileList||fileList.length==0){
log.error("文件夾是空的");
}else{
for(Filedata:fileList){
Stringorprefix=data.getName().substring(0,data.getName().lastIndexOf("."));
Stringprefix=fileName.substring(0,fileName.lastIndexOf("."));
System.out.println("index==="+orprefix+"prefix==="+prefix);
if(orprefix.contains(prefix)){
booleanf=data.renameTo(newFile(localpath+"/"+filenames));
System.out.println("f============="+f);
}else{
log.error("需要重命名的文件不存在,請檢查。。。");
}
}
}
}
}


@Override
publicvoidcloseFtpConnection(FTPClientftpClient,,BufferedInputStreambuffer){
if(ftpClient.isConnected()){
try{
ftpClient.disconnect();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(null!=bufferRead){
try{
bufferRead.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
if(null!=buffer){
try{
buffer.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
}


publicstaticvoidmain(String[]args)throwsIOException{
Stringhostname="xx.xxx.x.xxx";
Integerport=21;
Stringusername="edwftp";
Stringpassword="edwftp";
Stringfpath="/etl/etldata/back/";
StringlocalPath="C:/Users/Administrator/Desktop/ftp下載/";
StringfileName="test.txt";
Stringfilenames="ok.txt";
FtpUtilsImplftp=newFtpUtilsImpl();
/*ftp.modifiedLocalFileName(localPath,fileName,filenames);*/
ftp.downloadFileList(hostname,port,username,password,fpath,localPath,fileName,filenames);
/*ftp.uploadLocalFilesToFtp(hostname,port,username,password,fpath,localPath,fileName);*/
/*ftp.modifiedLocalFileName(localPath);*/
}
}

㈧ 用JAVA編寫一個程序實現輸出我的電腦上建立的FTP伺服器根目錄下的所有文件名

使用File類中方法就可以實現
File[] listFiles() 返回目錄下所有的文件
File file=new File("你的ftp的根路徑");
File files[]=file.listFiles();
for(int i=0;i<files.length;i++){
System.out.println(files[i].getName() );
}
如果要獲取所有的文件和文件夾可以使用String[] list()方法。返回的是String類型的數組,其中所有文件和文件夾的相對路徑表示。

補充---
如果那樣的話那麼就需要在你的ftp伺服器上做一個socket服務端,你通過一個客戶端連接上去。然後伺服器端將獲取的文件列表數組傳遞給你,就可以了。如果想直接獲取別人的機器的文件列表是很難的,基本上是不可能的,當然是出於安全的考慮

㈨ 用java完成文件ftp上傳下載

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

㈩ java 實現ftp上傳如何創建文件夾

是許可權問題嗎?如果不是的話你就應該先確定一個文件夾的名字和路徑dirName然後
比如String dirName="/newDir";
File dir=new File(dirName);
if(!dir.exists())
dir.mkdir();

熱點內容
我的世界伺服器管理應該做什麼 發布:2024-04-23 19:37:19 瀏覽:146
伺服器是如何處理多個用戶的請求 發布:2024-04-23 19:35:26 瀏覽:957
寫安卓用什麼架構 發布:2024-04-23 18:51:14 瀏覽:413
安卓r11是什麼介面 發布:2024-04-23 18:42:51 瀏覽:762
公元紀年法的演算法 發布:2024-04-23 18:38:25 瀏覽:113
遠景s1什麼配置 發布:2024-04-23 18:12:11 瀏覽:498
系統程序媒體存儲設備 發布:2024-04-23 18:12:09 瀏覽:822
全民槍王得到禮包都是密碼多少 發布:2024-04-23 17:55:06 瀏覽:225
如何看伺服器是否有雙網卡 發布:2024-04-23 17:55:05 瀏覽:467
紅米刷機為什麼要密碼 發布:2024-04-23 17:52:30 瀏覽:670