JSch 프로그램을 사용하여 SFTP 서버에 파일을 업로드하고 다운로드하고 있습니다. 파일을 다운로드 할 때 파일에 정크 값이 포함되어 있습니다. 내가 MAC을 사용하고JSch 정크 값이 포함 된 SFTP 파일 다운로드
public ChannelSftp createSession(String sftpUserName, String sftpHost, int sftpPort) {
JSch jsch = new JSch();
Channel channel = null;
ChannelSftp c = null;
String privateKey = "~/.exec/id_rsa";
try {
if (session == null) {
session = jsch.getSession(sftpUserName, sftpHost, sftpPort);
//session.setPassword(sftpPassword);
jsch.addIdentity(privateKey);
session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
}
channel = session.openChannel("sftp");
channel.connect();
c = (ChannelSftp) channel;
} catch (Exception e) {
e.printStackTrace();
}
return c;
}
public Integer downloadFile(ChannelSftp channelSftp, String FileToBeCopied, String folderPath) {
int fileDownloaded = -1;
try {
//channelSftp.cd(folderPath);
OutputStream output = new FileOutputStream("/Users/" + System.getProperty("user.name") + "/Downloads/" + FileToBeCopied);
channelSftp.setFilenameEncoding("UTF-8");
channelSftp.get(FileToBeCopied, output);
fileDownloaded = 0;
} catch (FileNotFoundException e) {
fileDownloaded = 1;
e.printStackTrace();
} catch (SftpException e) {
fileDownloaded = 2;
e.printStackTrace();
} catch (Exception e) {
fileDownloaded = 3;
}
return fileDownloaded;
}
public boolean uploadFiles(ChannelSftp channelSftp, String workingFile, String ftpRemoteDefaultDirectory, String workingDirectory) {
boolean flag = false;
if (workingFile != null) {
if (!workingFile.equalsIgnoreCase("")) {
try {
channelSftp.cd(ftpRemoteDefaultDirectory);
try {
File f = new File(workingDirectory + workingFile);
//channelSftp.setFilenameEncoding("UTF-8");
channelSftp.put(new FileInputStream(f), f.getName());
flag = true;
} catch (Exception e) {
e.printStackTrace();
}
destroySession(channelSftp);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return flag;
}
: 아래
내가 쓴 코드입니다. 온라인으로 확인한 결과OutputStream
은 "UTF-8"을 사용하여 파일을 인코딩하므로 다시 할 필요가 없습니다.
SFTP 서버의 파일이 실제로 UTF-8 인코딩으로되어 있습니까? –
그래, 파일이 UTF-8 인코딩을 사용하는지 확인하는 방법은 UTF-8 인코딩 – user2727493
입니다. – user2727493