解决从ftp上下载音频文件大小改变的问题,ftp文件大小,甲客户那边老是说录音的音
分享于 点击 37338 次 点评:155
解决从ftp上下载音频文件大小改变的问题,ftp文件大小,甲客户那边老是说录音的音
甲客户那边老是说录音的音质很差劲,但是丁客户的录音的音质无差别。用的是同一个算法,就只FTP服务器的系统不同。这个问题纠缠了我很久。经过多次试验终于发现,都是从ftp下载下来的文件比源文件要大一点。只要设置ftp下载的编码为GBK就行了。请看下面一段代码:
import java.io.IOException;import org.apache.commons.net.ftp.FTPClient;import org.apache.commons.net.ftp.FTPClientConfig;import org.apache.commons.net.ftp.FTPFile;import com.eshore.ccc.CCCLogger;public class FTP { private String serverIp = "59.41.223.227"; private int port = 21; private String username ="upload"; private String password = "suntekgc"; private String rootpath = "/"; private String ftptype = "1"; private FTPClient ftpClient; public FTP() { } public FTP(String serverIp, int port, String username, String password, String rootpath, String ftptype) { this.serverIp = serverIp; this.port = port; this.username = username; this.password = password; this.rootpath = rootpath; if(this.rootpath.equals("")) this.rootpath = "/"; this.ftptype = ftptype; } private boolean connectFTPServer() { this.ftpClient = new FTPClient(); try { //String FTPSERV_TYPE = AppHandle.getHandle("ccf").getProperty("FTPSERV_TYPE", "2"); FTPClientConfig conf = null; if(this.ftptype.equals("1")) conf = new FTPClientConfig(FTPClientConfig.SYST_NT); else conf = new FTPClientConfig(FTPClientConfig.SYST_UNIX); ftpClient.configure(conf); ftpClient.setDataTimeout(60000); ftpClient.setControlEncoding("GBK"); ftpClient.setDefaultTimeout(60000); ftpClient.connect(this.serverIp, port); boolean b = ftpClient.login(username, password); if(b) CCCLogger.logger.info("登录FTP服务器成功."); else CCCLogger.logger.info("登录FTP服务器失败."); return b; } catch (IOException e) { CCCLogger.logger.error("登录FTP服务器失败", e); return false; } }public boolean remoteV3ToLocal(String voxFile, String localFile) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try { if (!connectFTPServer()) return false; ftpClient.enterLocalPassiveMode(); ftpClient.setFileTransferMode(org.apache.commons.net.ftp.FTP.STREAM_TRANSFER_MODE); ftpClient.setFileType(org.apache.commons.net.ftp.FTP.BINARY_FILE_TYPE); // 转到指定下载目录 String remoteDir = "/"; String remoteFileName = voxFile; if (voxFile.contains("/")) { remoteDir = voxFile.substring(0, voxFile.lastIndexOf("/")); remoteFileName = voxFile.substring(voxFile.lastIndexOf("/") + 1); } if(this.rootpath.endsWith("/")) remoteDir = this.rootpath + remoteDir; else remoteDir = this.rootpath + "/" + remoteDir; FTPFile file = null; try { if(ftpClient.changeWorkingDirectory(remoteDir)) CCFLogger.logger.info("进入FTP目录:" + remoteDir); else { CCFLogger.logger.info("进入FTP目录:" + remoteDir + "失败"); return false; } FTPFile[] remoteFiles = ftpClient.listFiles(); // 遍历所有文件,找到指定的文件 for (int i = 0; i < remoteFiles.length; i++) { if (remoteFiles[i].getName().equals(remoteFileName)) { file = remoteFiles[i]; CCFLogger.logger.info("从FTP上找到了文件:" + remoteFileName); break; } } } catch (Exception e1) { e1.printStackTrace(); } if(file == null) { CCFLogger.logger.info("没有从FTP上找到文件:" + remoteFileName); return false; } File outFile = new File(localFile); bis = new BufferedInputStream(ftpClient.retrieveFileStream(file.getName())); bos = new BufferedOutputStream(new FileOutputStream(outFile)); byte[] szBuf = new byte[128 * 1024]; int dwRead = 0; while (( dwRead = bis.read(szBuf, 0, 128 * 1024)) != -1) { try { bos.write(szBuf, 0,dwRead); bos.flush(); } catch (Exception e) { } } if (ftpClient.isConnected()) { CCFLogger.logger.info("断开FTP连接!"); try { ftpClient.disconnect(); } catch (IOException ioe) {} } try { if (bis != null) bis.close(); if (bos != null) bos.close(); } catch (IOException e) {} return true; } catch (Exception e) { CCFLogger.logger.error("从FTP下载文件到服务器失败!", e); return false; } finally { try { if (bis != null) bis.close(); if (bos != null) bos.close(); } catch (IOException e) {} } }}//该片段来自于http://byrx.net
用户点评