當前位置:首頁 » 文件管理 » javaftp上傳下載

javaftp上傳下載

發布時間: 2022-05-07 02:40:46

java 實現ftp上傳下載,windows下和linux下游何區別

packagecom.weixin.util;

importjava.io.File;
importjava.io.FileOutputStream;
importjava.io.IOException;
importjava.io.InputStream;
importjava.io.OutputStream;
importjava.io.PrintWriter;
importjava.io.RandomAccessFile;

importorg.apache.commons.net.PrintCommandListener;
importorg.apache.commons.net.ftp.FTP;
importorg.apache.commons.net.ftp.FTPClient;
importorg.apache.commons.net.ftp.FTPFile;
importorg.apache.commons.net.ftp.FTPReply;

importcom.weixin.constant.DownloadStatus;
importcom.weixin.constant.UploadStatus;

/**
*支持斷點續傳的FTP實用類
*@version0.1實現基本斷點上傳下載
*@version0.2實現上傳下載進度匯報
*@version0.3實現中文目錄創建及中文文件創建,添加對於中文的支持
*/
publicclassContinueFTP{
publicFTPClientftpClient=newFTPClient();

publicContinueFTP(){
//設置將過程中使用到的命令輸出到控制台
this.ftpClient.addProtocolCommandListener(newPrintCommandListener(newPrintWriter(System.out)));
}

/**
*連接到FTP伺服器
*@paramhostname主機名
*@paramport埠
*@paramusername用戶名
*@parampassword密碼
*@return是否連接成功
*@throwsIOException
*/
publicbooleanconnect(Stringhostname,intport,Stringusername,Stringpassword)throwsIOException{
ftpClient.connect(hostname,port);
ftpClient.setControlEncoding("GBK");
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username,password)){
returntrue;
}
}
disconnect();
returnfalse;
}

/**
*從FTP伺服器上下載文件,支持斷點續傳,上傳百分比匯報
*@paramremote遠程文件路徑
*@paramlocal本地文件路徑
*@return上傳的狀態
*@throwsIOException
*/
publicDownloadStatusdownload(Stringremote,Stringlocal)throwsIOException{
//設置被動模式
ftpClient.enterLocalPassiveMode();
//設置以二進制方式傳輸
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
DownloadStatusresult;

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

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

//進行斷點續傳,並記錄狀態
FileOutputStreamout=newFileOutputStream(f,true);
ftpClient.setRestartOffset(localSize);
InputStreamin=ftpClient.retrieveFileStream(newString(remote.getBytes("GBK"),"iso-8859-1"));
byte[]bytes=newbyte[1024];
longstep=lRemoteSize/100;
longprocess=localSize/step;
intc;
while((c=in.read(bytes))!=-1){
out.write(bytes,0,c);
localSize+=c;
longnowProcess=localSize/step;
if(nowProcess>process){
process=nowProcess;
if(process%10==0)
System.out.println("下載進度:"+process);
//TODO更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
booleanisDo=ftpClient.completePendingCommand();
if(isDo){
result=DownloadStatus.Download_From_Break_Success;
}else{
result=DownloadStatus.Download_From_Break_Failed;
}
}else{
OutputStreamout=newFileOutputStream(f);
InputStreamin=ftpClient.retrieveFileStream(newString(remote.getBytes("GBK"),"iso-8859-1"));
byte[]bytes=newbyte[1024];
longstep=lRemoteSize/100;
longprocess=0;
longlocalSize=0L;
intc;
while((c=in.read(bytes))!=-1){
out.write(bytes,0,c);
localSize+=c;
longnowProcess=localSize/step;
if(nowProcess>process){
process=nowProcess;
if(process%10==0)
System.out.println("下載進度:"+process);
//TODO更新文件下載進度,值存放在process變數中
}
}
in.close();
out.close();
booleanupNewStatus=ftpClient.completePendingCommand();
if(upNewStatus){
result=DownloadStatus.Download_New_Success;
}else{
result=DownloadStatus.Download_New_Failed;
}
}
returnresult;
}

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

//檢查遠程是否存在文件
FTPFile[]files=ftpClient.listFiles(newString(remoteFileName.getBytes("GBK"),"iso-8859-1"));
if(files.length==1){
longremoteSize=files[0].getSize();
Filef=newFile(local);
longlocalSize=f.length();
if(remoteSize==localSize){
returnUploadStatus.File_Exits;
}elseif(remoteSize>localSize){
returnUploadStatus.Remote_Bigger_Local;
}

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

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

/**
*遞歸創建遠程伺服器目錄
*@paramremote遠程伺服器文件絕對路徑
*@paramftpClientFTPClient對象
*@return目錄創建是否成功
*@throwsIOException
*/
(Stringremote,FTPClientftpClient)throwsIOException{
UploadStatusstatus=UploadStatus.Create_Directory_Success;
Stringdirectory=remote.substring(0,remote.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(newString(directory.getBytes("GBK"),"iso-8859-1"))){
//如果遠程目錄不存在,則遞歸創建遠程伺服器目錄
intstart=0;
intend=0;
if(directory.startsWith("/")){
start=1;
}else{
start=0;
}
end=directory.indexOf("/",start);
while(true){
StringsubDirectory=newString(remote.substring(start,end).getBytes("GBK"),"iso-8859-1");
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else{
System.out.println("創建目錄失敗");
returnUploadStatus.Create_Directory_Fail;
}
}

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

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

/**
*上傳文件到伺服器,新上傳和斷點續傳
*@paramremoteFile遠程文件名,在上傳之前已經將伺服器工作目錄做了改變
*@paramlocalFile本地文件File句柄,絕對路徑
*@paramprocessStep需要顯示的處理進度步進值
*@paramftpClientFTPClient引用
*@return
*@throwsIOException
*/
publicUploadStatusuploadFile(StringremoteFile,FilelocalFile,FTPClientftpClient,longremoteSize)throwsIOException{
UploadStatusstatus;
//顯示進度的上傳
longstep=localFile.length()/100;
longprocess=0;
longlocalreadbytes=0L;
RandomAccessFileraf=newRandomAccessFile(localFile,"r");
OutputStreamout=ftpClient.appendFileStream(newString(remoteFile.getBytes("GBK"),"iso-8859-1"));
//斷點續傳
if(remoteSize>0){
ftpClient.setRestartOffset(remoteSize);
process=remoteSize/step;
raf.seek(remoteSize);
localreadbytes=remoteSize;
}
byte[]bytes=newbyte[1024];
intc;
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();
booleanresult=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;
}
returnstatus;
}

publicstaticvoidmain(String[]args){
ContinueFTPmyFtp=newContinueFTP();
try{
System.err.println(myFtp.connect("10.10.6.236",21,"5","jieyan"));
// myFtp.ftpClient.makeDirectory(newString("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.changeWorkingDirectory(newString("歌曲".getBytes("GBK"),"iso-8859-1"));
// myFtp.ftpClient.makeDirectory(newString("愛你等於愛自己".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(IOExceptione){
System.out.println("連接FTP出錯:"+e.getMessage());
}
}
}

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

使用rt.jar中的sun.net.FtpClient類來實現對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下載

檢查一下是否timeout時間設置過短。不要設置內存或者處理器限制。 還有在IIS的metabase資料庫中找一下FTP的設置,在那裡找配置文件修改最直接。
通過CFtpFileFind 得到文件的URL之後,然後通過CHttpFile::QueryInfo 得到文件大小。
求採納為滿意回答。

❺ java實現FTP文件上傳功能的原理是什麼

。。參照ftp協議。直接用socket發送就行了。。。ftp本來控制信息就不是很復雜的。

❻ java的ftp上傳和下載怎麼判斷文件夾是否存在,不存在就創建...

if(!file.exist)
{
file.mdr();
}

❼ java FTP怎麼上傳文件

要有IP號 埠號 密碼

❽ 如何在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) {;} } }

❾ Java實現FTP文件上傳下載遇到的問題,求助!

你好,Socket本身就有網路輸入輸出流的,不用創建多個Socket。
如果要上傳就用Socket的網路輸出流。
如果要下載就用Socket網路輸入流獲取伺服器傳送的數據。
伺服器可以獲取到已連接的Socket對象,所以它也可以獲取到網路輸入輸出流。

如果滿意,請採納,謝謝。

❿ java 實現ftp上傳的問題

我以前碰到過,不知道是不是和你的一樣的問題:

我的是英文名稱的文件可以上傳,中文名不行(也不報錯,運行正確,就是FTP文件伺服器上沒文件), 後來我發現是編碼問題,我把我的項目代碼貼上來:

package omsejb..upload;

import java.io.*;

import java.util.ArrayList;
import java.util.List;
import omsejb.system.OMSJDBCBase;
import tellhow.exception.sysexception.JDBCDAOSysException;

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

import javax.xml.rpc.ParameterMode;
import javax.xml.rpc.ServiceException;
import java.net.MalformedURLException;
import java.rmi.RemoteException;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.encoding.XMLType;

/**
* 上報文件(上傳文件,如:地調上報省調數據,採用E語言文件形式上報)
* @author ZouLiXing
*
*/
public class UpLoadFile extends OMSJDBCBase
{
private String ftpHost = "";
private int ftpPort;
private String ftpUserName = "";
private String ftpPassword = "";
private String webServicePoint = "";
private String webServiceName = "";
private String ftpPath="";
private String localPath="";

public UpLoadFile(){

UploadDaoEntity ude = new UploadDaoEntity();
ftpHost = ude.getDicValue("ftphost");
ftpPort = Integer.parseInt(ude.getDicValue("ftpport"));
ftpUserName = ude.getDicValue("ftpusername");
ftpPassword = ude.getDicValue("ftppassword");
webServicePoint = ude.getDicValue("webservicepoint");
webServiceName = ude.getDicValue("webservicename");
ftpPath = ude.getDicValue("ftppath");
localPath = ude.getDicValue("filepath");
}
/**
* true表示FTP上傳文件成功;否則失敗。
*
* @param hostName
* FTP地址
* @param port
* 埠 默認21
* @param userName
* @param password
* @param path
* @param servicefileName
* 上傳名
* @param input
* 本地文件輸入流
* @return
*/
public boolean ftpUpLoadEFile(String filename) {
FTPClient ftpClient = new FTPClient();
int reply;
try {
ftpClient.connect(ftpHost, ftpPort);
boolean l=ftpClient.login(ftpUserName, ftpPassword);
if(l){
System.out.println("login success(成功登陸FTP)!");
}else{
System.out.println("login success(登陸FTP不成功)!");
return false;
}
reply = ftpClient.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftpClient.disconnect();
return false;
}
File file = new File(localPath+filename);
InputStream input = new FileInputStream(file);
boolean b = ftpClient.storeFile(GBKToiso8859(filename), input);
input.close();
ftpClient.logout();
if(!b){
System.out.println("上傳文件不成功!");
return false;
}else{
System.out.println("上傳文件成功-_-!");
}
} catch (IOException ioe) {
ioe.printStackTrace();
return false;
} finally {
if (ftpClient.isConnected()) {
try {
ftpClient.disconnect();
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
return true;
}
/**
* 轉碼[GBK -> iso-8859-1] 不同的平台需要不同的轉碼
*
* @param obj
* @return
*/
private String GBKToiso8859(Object obj) {
try {
if (obj == null)
return "";
else
return new String(obj.toString().getBytes("GBK"), "iso-8859-1");
} catch (Exception e) {
return "";
}
}

public static void main(String[] agrs){
// UpLoadFile uf = new UpLoadFile();
//
// String ftpHost = "10.61.12.233";
// int ftpPort = 21;
// String userName = "omshz";
// String password = "omshz11";
// String ftpPath = "";
////
//// String fileName = "";
//// String filePath = "";
//// String selectID="";//選擇上傳的文件ID,多個文件用","隔開;
//// List lsFilePath = uf.getFilePath(selectID);
//// for(int i=0;i<lsFilePath.size();i++){
// //上傳E文件
//// filePath = lsFilePath.get(i).toString();
//// fileName = uf.getFileName(filePath);
// File file = new File("d:\\日購網電量上報_西安局_2006-03-06.YB");
// try {
// InputStream input = new FileInputStream(file);
// uf.ftpUpLoadEFile(ftpHost, ftpPort, userName, password, ftpPath,
// "日購網電量上報_西安局_2006-03-06.YB", input);
// } catch (FileNotFoundException fileNoe) {
// fileNoe.printStackTrace();
// }
//
}
}

熱點內容
hp存儲擴容 發布:2024-11-17 23:29:16 瀏覽:569
在ftp中put表示什麼 發布:2024-11-17 23:29:12 瀏覽:383
mvc多文件上傳 發布:2024-11-17 23:13:56 瀏覽:155
玩游戲硬碟緩存32m 發布:2024-11-17 23:03:42 瀏覽:525
藍光存儲系統 發布:2024-11-17 23:03:41 瀏覽:436
地平線4提示配置低於最低怎麼辦 發布:2024-11-17 22:54:38 瀏覽:610
注冊銀行卡賬戶密碼填什麼 發布:2024-11-17 22:54:35 瀏覽:537
java壓縮上傳圖片 發布:2024-11-17 22:26:59 瀏覽:627
plc編程課件 發布:2024-11-17 22:18:23 瀏覽:469
我的世界伺服器信號一直在檢測 發布:2024-11-17 22:09:52 瀏覽:547