Android FTP 서버 업로드 파일 공략(코드 상세 설명)

7428 단어 AndroidFTP업로드
1.머리말
개발 중 에 FTP 서버 에 파일 을 업로드 할 필요 가 있 으 므 로 먼저 가 져 와 야 합 니 다.
commons-net-3.3.jar 다음 에 api 를 이용 하여 관련 조작 을 하고 구체 적 인 기능 은 다음 과 같다.
Ftp 관련 코드

import android.util.Log;

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

import java.io.FileInputStream;

public class FTPClientUtils {

 private static final String TAG = "MainActivity";
 private FTPClient ftpClient = null; // FTP   

 /**
 *    FTP   
 *
 * @param host ftp     
 * @param username      
 * @param password     
 * @param port   
 * @return       
 */
 public boolean ftpConnect(String host, String username, String password, int port) {
 try {
  ftpClient = new FTPClient();
  ftpClient.connect(host,port);
  //         ,          
  if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
  boolean status = ftpClient.login(username, password);
  /*
   *         
   *             ,               。
   *        BINARY_FILE_TYPE     、       。
   */
  ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
  ftpClient.enterLocalPassiveMode();
  return status;
  }
 } catch (Exception e) {
  e.printStackTrace();
 }
 return false;
 }

 /**
 *   ftp     
 *
 * @return     
 */
 public boolean ftpDisconnect() {
 //      
 if (ftpClient == null) {
  return true;
 }
 //   ftp     
 try {
  ftpClient.logout();
  ftpClient.disconnect();
  return true;
 } catch (Exception e) {
  e.printStackTrace();
 }
 return false;
 }

 /**
 * ftp     
 *
 * @param srcFilePath      
 * @param desFileName     
 * @return       
 */
 public boolean ftpUpload(String srcFilePath, String desFileName) {
 boolean status = false;
 try {
  FileInputStream srcFileStream = new FileInputStream(srcFilePath);
  status = ftpClient.storeFile(desFileName, srcFileStream);
  srcFileStream.close();
  return status;
 } catch (Exception e) {
  e.printStackTrace();
 }
 return status;
 }

 /**
 * ftp     
 *
 * @param path      
 * @return       
 */
 public boolean ftpChangePath(String path) {
 boolean status = false;
 try {
  status = ftpClient.changeWorkingDirectory(path);
 } catch (Exception e) {
  e.printStackTrace();
 }
 return status;
 }
}
2.api 호출

boolean isConnect = mFtpClient.ftpConnect("   host", "   ", "  ", 21);//      21
  if (isConnect) {
  boolean isSuccessful = mFtpClient.ftpUpload("/sdcard/" + folderName + "/" + mPicturename, "/htdocs/pics/" + mPicturename);
  if (isSuccessful) {
   mFtpClient.ftpDisconnect();
  		//    
  } else {
   //    
  }
  } else {
  //       
  }
부록:자신 이 이전에 프로젝트 를 할 때 쓴 FTP 업로드 코드:

package com.kandao.yunbell.videocall; 
 
import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.UnsupportedEncodingException; 
import java.net.SocketException; 
 
import org.apache.commons.net.ftp.FTP; 
import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPReply; 
 
import com.kandao.yunbell.common.SysApplication; 
 
import android.content.Context; 
import android.util.Log; 
 
public class MyUploadThread extends Thread { 
 private String fileName;//      
 private String filePath;//        
 private String fileStoragePath;//           
 private String serverAddress;//       
 private String ftpUserName;// ftp   
 private String ftpPassword;// ftp   
 private Context mContext; 
 public MyUploadThread() { 
 super(); 
 // TODO Auto-generated constructor stub 
 } 
 
 public MyUploadThread(Context mContext,String fileName, String filePath, 
  String fileStoragePath,String serverAddress,String ftpUserName,String ftpPassword) { 
 super(); 
 this.fileName = fileName; 
 this.filePath = filePath; 
 this.fileStoragePath = fileStoragePath; 
 this.serverAddress = serverAddress; 
 this.ftpUserName = ftpUserName; 
 this.ftpPassword = ftpPassword; 
 this.mContext=mContext; 
 } 
 
 @Override 
 public void run() { 
 super.run(); 
 try { 
  FileInputStream fis=null; 
  FTPClient ftpClient = new FTPClient(); 
  String[] idPort = serverAddress.split(":"); 
  ftpClient.connect(idPort[0], Integer.parseInt(idPort[1])); 
  int returnCode = ftpClient.getReplyCode(); 
  Log.i("caohai", "returnCode,upload:"+returnCode); 
  boolean loginResult = ftpClient.login(ftpUserName, ftpPassword); 
  Log.i("caohai", "loginResult:"+loginResult); 
  if (loginResult && FTPReply.isPositiveCompletion(returnCode)) {//        
   
  //        
   
  if (((SysApplication) mContext).getIsVideo()) { 
   ((SysApplication) mContext).setIsVideo(false); 
   boolean ff=ftpClient.changeWorkingDirectory(fileStoragePath + "/video/"); 
   Log.i("caohai", "ff:"+ff); 
  }else{ 
  boolean ee=ftpClient.changeWorkingDirectory(fileStoragePath + "/photo/"); 
  Log.i("caohai", "ee:"+ee); 
  } 
  ftpClient.setBufferSize(1024); 
  // ftpClient.setControlEncoding("iso-8859-1"); 
  // ftpClient.enterLocalPassiveMode(); 
  ftpClient.setFileType(FTP.BINARY_FILE_TYPE); 
   fis = new FileInputStream(filePath + "/" 
   + fileName); 
   Log.i("caohai", "fileStoragePath00000:"+fileStoragePath); 
  String[] path = fileStoragePath.split("visitorRecord"); 
   
  boolean fs = ftpClient.storeFile(new String((path[1] 
   + "/photo/" + fileName).getBytes(), "iso-8859-1"), fis); 
  Log.i("caohai", "shifoushangchuanchenggong:"+fs); 
  fis.close(); 
  ftpClient.logout(); 
  //ftpClient.disconnect(); 
  } else {//        
  ftpClient.disconnect(); 
  } 
 } catch (NumberFormatException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
 } catch (SocketException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
 } catch (FileNotFoundException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
 } catch (UnsupportedEncodingException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
 } catch (IOException e) { 
  // TODO Auto-generated catch block 
  e.printStackTrace(); 
 } 
 
 } 
} 
총결산
안 드 로 이 드 FTP 서버 업로드 파일 공략 에 관 한 이 글 은 여기까지 소개 되 었 습 니 다.더 많은 안 드 로 이 드 FTP 서버 업로드 내용 은 이전 글 을 검색 하거나 아래 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!

좋은 웹페이지 즐겨찾기