import java.io.File;이 코드를 실행하면 전송이 성공했음을 반환합니다.
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream을 사용합니다.
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FTPUploadFile {
public static void main(String[] args) {
서버 문자열 = "ServerAddress";
int 포트 = 21;
문자열 user = "사용자 이름";
문자열 비밀번호 = "yourPassword";
FTPClient ftpClient = 새로운 FTPClient();
{
ftpClient.connect(서버, 포트);
ftpClient.login(사용자, 비밀번호);
ftpClient.enterLocalPassiveMode();
ftpClient.setFileType(FTP. BINARY_FILE_TYPE);
// 접근법 1: InputStream
File file = new File("C:/plugins and styles.txt");
문자열 경로 = "플러그인 및 styles.txt";
InputStream inputStream = new FileInputStream(file);
System.out.println("업로드 시작");
//업로드 출력
boolean res = ftpClient.storeFile(path, inputStream);
//읽은 스트림을 닫습니다
inputStream.close();
if (res==true) {
System.out.println("""+path+" 파일이 성공적으로 다운로드되었습니다.");
}
// 접근법 2: OutputStream
file = new File("C:/track 1.wma");
경로 = "오디오/트랙 1.wma";
inputStream = new FileInputStream(파일);
System.out.println("업로드 시작");
OutputStream outputStream = ftpClient.storeFileStream(경로);
byte[] bytesIn = 새 바이트[4096];
int 버퍼 = 0;
//파일의 끝에 도달하지 않은 한
System.out.println("다운로드 중...");
int 전송 = 0;
int 퍼센트 = 0;
//파일의 끝에 도달하지 않는 한
while ((buffer = inputStream.read(bytesIn)) != -1) {
//4096바이트의 버퍼로 데이터를 읽습니다
outputStream.write(bytesIn, 0, buffer);
전송됨 += 버퍼;
퍼센트 = (int) (전송됨*100/file.length());
System.out.println(백분율+"%");
}
//쓰기 읽기 스트림 닫기
inputStream.close();
outputStream.close();
//결과 업로드
res = ftpClient.completePendingCommand();
if (res) {
System.out.println(""+path+" 파일이 성공적으로 다운로드되었습니다.");
}
} catch (IOException e) {
System.out.println(e.getMessage());
e.printStackTrace();
} 마지막으로 {
try {
if (ftpClient.isConnected()) {
//FTP
ftpClient.logout();
ftpClient.disconnect()를 호출합니다.
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
Start of upload
플러그인 및 styles.txt 파일이 성공적으로 다운로드되었습니다
업로드 시작
1%
2%
3%
.
.
97%
98%
98%
99%
99%
100%
1.wma 트랙 파일이 성공적으로 다운로드되었습니다
Please disable your ad blocker and refresh the window to use this website.