需求时从sftp服务器上下载一个数据文件到服务器上.
sftp下载就在网上找了一个工具类.我本地使用的是tomcat服务器,测试了很多遍也没有出现中文乱码的情况.但是到测试库里就出现了文件下载完内容中文乱码的情况,导致程序没法正常的解析.
本地的tomcat编码是utf8,测试服务器weblogic编码也是设置的utf8;
请问一下会是什么原因导致内容乱码的呢?
sftp下载文件的代码在下面
/**
* 下载文件
*
* @param directory
* 下载目录
* @param downloadFile
* 下载的文件
* @param saveFile
* 存在本地的路径
* @throws SftpException
* @throws FileNotFoundException
* @throws Exception
*/
public Boolean downloadFile(String filePath, String fileName, String localPath){
Boolean result = false;
try {
if(sftp == null || !sftp.isConnected()){
this.login();
}
String directory = basePath + filePath;
if (directory != null && !"".equals(directory)) {
sftp.cd(directory);
}
File localDec = new File(localPath);
if(!localDec.exists()){
localDec.mkdirs();
}
File localFile = new File(localPath + File.separator + fileName);
sftp.get(fileName, new FileOutputStream(localFile));
log.info("文件:{} 下载成功,路径 {}", fileName,localPath);
result = true;
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
logout();
}
return result;
}