ホームページ  >  記事  >  Java  >  FTPアップロードのサンプルコード

FTPアップロードのサンプルコード

PHP中文网
PHP中文网オリジナル
2017-06-20 09:51:251376ブラウズ

/**
*FTPアップロードツールクラス
*
*/
public class FtpUtil {
static Logger logger = Logger.getLogger(FtpUtil.class.getName());
private String ip;
private String ユーザー名;
private String パスワード;
private int port = -1;
private String path;
private OutputStream os = null;
private FileInputStream is = null;
private FTPClient ftpClient;
public FTPClient getftpClient() { return ftpClient; }
public void setftpClient(FTPClient ftpClient) {
this.ftpClient = ftpClient;
}
public FtpUtil(){

}
public FtpUtil(String サーバーIP, String ユーザー名, String パスワード) {
this.ip = serverIP;
this.username = ユーザー名;
this.password = パスワード;
}

public FtpUtil(文字列サーバーIP, intポート, 文字列ユーザー名, 文字列パスワード) {
this.ip = サーバーIP;
this.username = ユーザー名;
this.パスワード = パスワード;
this.port = ポート;
this.ftpClient= new FTPClient();
}
/**
*
* @return サーバーに接続するかどうか
*/
public boolean ftpLogin() {
boolean isLogin = false;  
FTPClientConfig ftpClientConfig = new FTPClientConfig();  
ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());  
this.ftpClient.setControlEncoding("GBK");  
this.ftpClient.configure(ftpClientConfig);  
try {
if (this.port > 0) {
this.ftpClient.connect(this.ip, this.port);    
}else {
this.ftpClient.connect(this.ip);    
}
int Reply = this.ftpClient.getReplyCode();    
if (!FTPReply.isPositiveCompletion(reply)) {
this.ftpClient.disconnect();
logger.info("登录FTP服务失败!");  
isLogin を返します。    
}
if(this.ftpClient.login(this.username, this.password)){
this.ftpClient.enterLocalPassiveMode(); // 設置传输协议
this.ftpClient.setFileType(ftpClient.BINARY_FILE_TYPE);
logger.info(this.username + "成功登陆FTPサービス务器");  
isLogin = true;
}else{
logger.info(this.username + "登录FTP服务失败!");
}
}catch (Exception e) {
e.printStackTrace();  
logger.error( e.getMessage());  
}
this.ftpClient.setBufferSize(1024 * 2);  
this.ftpClient.setDataTimeout(30 * 1000);  
isLogin を返します。  
}
/**
* @Exit サーバーリンクを閉じる
*/
public void ftpLogOut() {
if (null != this.ftpClient && this.ftpClient.isConnected()) {
try {
boolean reuslt = this.ftpClient.logout( );// 退出FTP服务器
if (reuslt) {
logger.info("成功退出服务器");  
}
}catch (IOException e) {
e.printStackTrace();  
logger.warn("退出FTPサービス务器异常!" + e.getMessage());    
}最後に {
try {
this.ftpClient.disconnect();
}catch (IOException e) {
e.printStackTrace();
logger.error("关闭FTPサーバーの接続常异!");    
}
}
}
}
/**
* Ftp ファイルをアップロードします
* @param localFile ローカルファイル
* @param romotUpLoadePath サーバーパスをアップロードします
*/
public boolean UploadFile(File localFile, String romotUpLoadePath) {
BufferedInputStream inStream = null;  
ブール値成功 = false;  
{
this.ftpClient.changeWorkingDirectory(romotUpLoadePath);
inStream = new BufferedInputStream(new FileInputStream(localFile)); を試してください。  
成功 = this.ftpClient.storeFile(localFile.getName(), inStream);    
if (success == true) {
System.out.println(localFile.getName() + "上传成功");///
return success;    
}
}catch (FileNotFoundException e) {
e.printStackTrace();  
}catch (IOException e) {
e.printStackTrace();  
}最後に {
if (inStream != null) {
try {
inStream.close();    
}catch (IOException e) {
e.printStackTrace();    
}
}
}
成功を返します。  
}

/**アップロードフォルダー
* @param localDirectory ローカルフォルダー
* @param RemoteDirectoryPath リモートパス
*/
public boolean UploadDirectory(String localDirectory,String RemoteDirectoryPath) {
File src = new File(localDirectory);  
System.out.println(src.getName());
try {
RemoteDirectoryPath = RemoteDirectoryPath + src.getName() + "/";    
boolean makeDirFlag = ftpClient.makeDirectory(remoteDirectoryPath);    
System.out.println("localDirectory : " + localDirectory);    
System.out.println("リモートディレクトリパス: " + リモートディレクトリパス);    
System.out.println("src.getName() : " + src.getName());    
System.out.println("リモートディレクトリパス: " + リモートディレクトリパス);    
System.out.println("makeDirFlag : " + makeDirFlag);  
// ftpClient.listDirectories();  
}catch (IOException e) {
e.printStackTrace();
}
File[] allFile = src.listFiles();
if(allFile.length==0||allFile.length>0){
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath().toString();    
UploadFile(新しいファイル(srcName), リモートディレクトリパス);    
}
}
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (allFile[currentFile].isDirectory()) {
U​​ploadDirectory(allFile[currentFile].getPath().toString() ,//递归
remoteDirectoryPath);    
}
}
}
/*for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (!allFile[currentFile].isDirectory()) {
String srcName = allFile[currentFile].getPath ().toString();    
UploadFile(新しいファイル(srcName), リモートディレクトリパス);    
}
}
for (int currentFile = 0;currentFile < allFile.length;currentFile++) {
if (allFile[currentFile].isDirectory()) {
U​​ploadDirectory(allFile[currentFile].getPath().toString() ,//递归
remoteDirectoryPath);    
}
} */
true を返します。  
}

以上がFTPアップロードのサンプルコードの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。