ftp下载数据出现乱码 一个txt文件
URL u3=new URL("ftp://user:password@111.111.111.11:11//vehicle.txt");
urlconn3=u3.openConnection();
in3 = urlconn3.getInputStream();
BufferedReader br3=new BufferedReader(new InputStreamReader(in3));
StringBuffer t3 = new StringBuffer();
while((line=br3.readLine())!=null){
rec=line.split("\t");
String tel=rec[0];
String hpzl=rec[1];
String hphm=rec[2];
String jf=rec[3];
String fsxx1=rec[4];
System.out.println(fsxx1);
String fsxx = new String(fsxx1.getBytes("GBK"), "ISO-8859-1");
// String address=rec[3];
System.out.println(tel+hpzl+hphm+jf+fsxx1);
}
乱码 ftp
------解决方案--------------------
用apache ftpclient吧,部分样例代码
/**
* 获取FTP服务器的客户端 连接对象 FTPClient Author:liuzhaoyang Date:2013-06-24
*
* @param ftpURL
* ftpURL
* @param userName
* ftp server 用户名用户名
* @param passWord
* ftp server 登录密码
* @return ftpClient对象
*/
private static FTPClient ftpClientConnection(String ftpURL, String userName, String passWord, String charSet)
throws IOException {
if (Objects.equal(userName, null)) {
userName = "anonymous";
}
if (Objects.equal(passWord, null)) {
passWord = "anonymous";
}
List<String> listCharSet = ImmutableList.of("utf8", "utf-8", "gbk", "gb2312");
if (!listCharSet.contains(StringUtils.lowerCase(charSet))) { // 如果charSet不是utf8,gbk,gb2312;就抛出异常
throw new RuntimeException("该FTP方法目前支持的文件编码有:utf8,gbk,gb2312");
}
String loginInfo = Objects.toStringHelper("").add("ftpURL", ftpURL).add("userName", userName)
.add("passWord", passWord).toString();
logger.info(loginInfo);
FTPClient ftpClient = null;
// try {
if (StringUtils.startsWithIgnoreCase(ftpURL, "ftp://")) {
// 判断是否以ftp://开头;格式:ftp://172.22.158.54/20130326/000000_0;或ftp://172.22.158.54:23/20130326/000000_0
ftpURL = StringUtils.substring(ftpURL, 6);
// 如果以ftp://开头,就进行截取;格式:172.22.158.54/20130326/000000_0;或172.22.158.54:23/20130326/000000_0
}
logger.debug("ftpURL is: " + ftpURL);
String hostNameAndPort = StringUtils.substringBefore(ftpURL, "/"); // 分割字符串,格式:172.22.158.54或172.22.158.54:23
logger.debug("hostName is: " + hostNameAndPort);
String hostName = StringUtils.substringBefore(hostNameAndPort, ":"); // 分割字符串,格式:172.22.158.54
String strFtpPort = StringUtils.substringAfter(hostNameAndPort, ":"); // 分割字符串,格式:23
if (Objects.equal(strFtpPort, "")) {
strFtpPort = "21"; // FTP 缺省端口
}
int ftpPort = Convert.toIntValue(strFtpPort);
logger.debug(Objects.toStringHelper("ftp info").add("hostName", hostName).add("ftpPort", ftpPort));
ftpClient = new FTPClient();
FTPClientConfig ftpClientConfig = new FTPClientConfig(FTPClientConfig.SYST_UNIX);
ftpClientConfig.setServerLanguageCode("zh");
ftpClient.configure(ftpClientConfig);
ftpClient.setConnectTimeout(SpringUtils.getDataSourceConnectTimeout());// 30s连接超时
ftpClient.setDataTimeout(SpringUtils.getDataSourceReadTimeout());
ftpClient.connect(hostName, ftpPort);
boolean isLogin = false;
isLogin = ftpClient.login(userName, passWord);
ftpClient.setBufferSize(5 * 1024); // buffer size 设置为5M
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // 设置文件类型为二进制,否则可能导致乱码无法打开
ftpClient.setControlEncoding(charSet);
if (Objects.equal(isLogin, true)) {
logger.info("FTP 登录成功,登录成功信息(ftp login success):" + loginInfo);
} else {
logger.warn("FTP 登录失败,登录失败信息(ftp login failure): " + loginInfo);