자바 최종 ftp 정지점 업로드 실현
6248 단어 자바
주로 apache 의 net 패 키 지 를 사용 하여 이 루어 집 니 다.인터넷 주소http://commons.apache.org/net/。구체 적 인 가방 의 다운로드 와 API 문 서 는 홈 페이지 를 보십시오.
정지점 업로드 란 업로드 과정 에서 전송 의 시작 위 치 를 설정 하 는 것 입 니 다.바 이 너 리 전송 을 설정 합 니 다.
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class ContinueFTP {
private FTPClient ftpClient = new FTPClient();
public ContinueFTP(){
//
this.ftpClient.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
}
/**
* FTP
* @param hostname
* @param port
* @param username
* @param password
* @return
* @throws IOException
*/
public boolean connect(String hostname,int port,String username,String password) throws IOException{
ftpClient.connect(hostname, port);
if(FTPReply.isPositiveCompletion(ftpClient.getReplyCode())){
if(ftpClient.login(username, password)){
return true;
}
}
disconnect();
return false;
}
/**
* FTP
* @param remote
* @param local
* @return
* @throws IOException
*/
public boolean download(String remote,String local) throws IOException{
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
boolean result;
File f = new File(local);
FTPFile[] files = ftpClient.listFiles(remote);
if(files.length != 1){
System.out.println(" ");
return false;
}
long lRemoteSize = files[0].getSize();
if(f.exists()){
OutputStream out = new FileOutputStream(f,true);
System.out.println(" :"+f.length());
if(f.length() >= lRemoteSize){
System.out.println(" , ");
return false;
}
ftpClient.setRestartOffset(f.length());
result = ftpClient.retrieveFile(remote, out);
out.close();
}else {
OutputStream out = new FileOutputStream(f);
result = ftpClient.retrieveFile(remote, out);
out.close();
}
return result;
}
/**
* FTP ,
* @param local ,
* @param remote , /home/directory1/subdirectory/file.ext Linux , ,
* @return
* @throws IOException
*/
public UploadStatus upload(String local,String remote) throws IOException{
// PassiveMode
ftpClient.enterLocalPassiveMode();
//
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
UploadStatus result;
//
String remoteFileName = remote;
if(remote.contains("/")){
remoteFileName = remote.substring(remote.lastIndexOf("/")+1);
String directory = remote.substring(0,remote.lastIndexOf("/")+1);
if(!directory.equalsIgnoreCase("/")&&!ftpClient.changeWorkingDirectory(directory)){
// ,
int start=0;
int end = 0;
if(directory.startsWith("/")){
start = 1;
}else{
start = 0;
}
end = directory.indexOf("/",start);
while(true){
String subDirectory = remote.substring(start,end);
if(!ftpClient.changeWorkingDirectory(subDirectory)){
if(ftpClient.makeDirectory(subDirectory)){
ftpClient.changeWorkingDirectory(subDirectory);
}else {
System.out.println(" ");
return UploadStatus.Create_Directory_Fail;
}
}
start = end + 1;
end = directory.indexOf("/",start);
//
if(end <= start){
break;
}
}
}
}
//
FTPFile[] files = ftpClient.listFiles(remoteFileName);
if(files.length == 1){
long remoteSize = files[0].getSize();
File f = new File(local);
long localSize = f.length();
if(remoteSize==localSize){
return UploadStatus.File_Exits;
}else if(remoteSize > localSize){
return UploadStatus.Remote_Bigger_Local;
}
// ,
InputStream is = new FileInputStream(f);
if(is.skip(remoteSize)==remoteSize){
ftpClient.setRestartOffset(remoteSize);
if(ftpClient.storeFile(remote, is)){
return UploadStatus.Upload_From_Break_Success;
}
}
// , ,
if(!ftpClient.deleteFile(remoteFileName)){
return UploadStatus.Delete_Remote_Faild;
}
is = new FileInputStream(f);
if(ftpClient.storeFile(remote, is)){
result = UploadStatus.Upload_New_File_Success;
}else{
result = UploadStatus.Upload_New_File_Failed;
}
is.close();
}else {
InputStream is = new FileInputStream(local);
if(ftpClient.storeFile(remoteFileName, is)){
result = UploadStatus.Upload_New_File_Success;
}else{
result = UploadStatus.Upload_New_File_Failed;
}
is.close();
}
return result;
}
/**
*
* @throws IOException
*/
public void disconnect() throws IOException{
if(ftpClient.isConnected()){
ftpClient.disconnect();
}
}
public static void main(String[] args) {
ContinueFTP myFtp = new ContinueFTP();
try {
myFtp.connect("192.168.21.171", 21, "test", "test");
System.out.println(myFtp.upload("E://VP6.flv", "/MIS/video/VP6.flv"));
myFtp.disconnect();
} catch (IOException e) {
System.out.println(" FTP :"+e.getMessage());
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Is Eclipse IDE dying?In 2014 the Eclipse IDE is the leading development environment for Java with a market share of approximately 65%. but ac...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.