Android FTP 서버 업로드 파일 공략(코드 상세 설명)
개발 중 에 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 서버 업로드 내용 은 이전 글 을 검색 하거나 아래 글 을 계속 찾 아 보 세 요.앞으로 도 많은 응원 부 탁 드 리 겠 습 니 다!
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Bitrise에서 배포 어플리케이션 설정 테스트하기이 글은 Bitrise 광고 달력의 23일째 글입니다. 자체 또는 당사 등에서 Bitrise 구축 서비스를 사용합니다. 그나저나 며칠 전 Bitrise User Group Meetup #3에서 아래 슬라이드를 발표했...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.