ftp.connect(ip, port);
//下面三行代码必须要,而且不能改变编码格式,否则不能正确下载中文文件
ftp.setControlEncoding("GBK");
FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");
增加这个设置试试............
解决方案 »
- 跪求:java.sql.SQLException: OALL8 处于不一致状态,违反协议,如何解决
- struts2 JSP 传值action
- can't covert [java.lang.String]to required type[org.jbpm.JbpmConfiguration]
- 这是什么错误啊
- http://localhost:8080/article/UserFiles/File/林三0(1).doc 不能打开?
- 帮个忙,谢谢!!!
- 安裝sql2000 driver for jdbc后還要怎樣配置才行﹖
- 100分,过来的高手们,给小弟介绍几本好书吧,到书店JAVA的书太多了,在线待
- struts高手进
- DOMParser开放源代码是不是已经没用啦???怎么会出现java.lang.VerifyError???
- tomcat6启动报错 Can't load log handler "1catalina.org.apache.juli.FileHandler"
- hibernate支持oracle的regexp_like等正则函数吗?
FTPFile[] remoteFiles = ftpClient.listFiles(remoteDir);
中,就不能得到该文件夹下的文件。必须通过转码,但如果是从程序中直接写的中文按照
remoteDir=new String(remoteDir.getBytes(“UTF-8”), "iso-8859-1");
转码就可以了,但从数据库中读取出来的,按照这个转,能得到该文件夹下的文件个数,
就是不能正常对该文件夹下的文件进行操作,从调试看得出的文件夹 是 ???? 乱码,因此不知道该如何转码。
如果是的话,个人观点:从程序中直接写汉字可以转而从数据库中取出来不可以转,说明从程序中直接写汉字,编码为UTF-8,而数据库中取出来的则不是UTF-8,所以会变乱码。
反之new String(remoteDir.getBytes(“UTF-8”), "iso-8859-1");是将iso-8859-1转为UTF-8也一样。
个人观点而已,没有测试。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import nc.itf.doc.DocDelegator;
import nc.vo.doc.doc_007.DirVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO; import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.apache.commons.net.ftp.FTP;
public class FtpTool {
private FTPClient ftp; private String romateDir = ""; private String userName = ""; private String password = ""; private String host = ""; private String port = "21"; public FtpTool(String url) throws IOException {
//String url="ftp://user:password@ip:port/ftptest/psd";
int len = url.indexOf("//");
String strTemp = url.substring(len + 2);
len = strTemp.indexOf(":");
userName = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1); len = strTemp.indexOf("@");
password = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
host = "";
len = strTemp.indexOf(":");
if (len < 0)//没有设置端口
{
port = "21";
len = strTemp.indexOf("/");
if (len > -1) {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
strTemp = "";
}
} else {
host = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
len = strTemp.indexOf("/");
if (len > -1) {
port = strTemp.substring(0, len);
strTemp = strTemp.substring(len + 1);
} else {
port = "21";
strTemp = "";
}
}
romateDir = strTemp;
ftp = new FTPClient();
ftp.connect(host, FormatStringToInt(port)); } public FtpTool(String host, int port) throws IOException {
ftp = new FTPClient();
ftp.connect(host, port);
} public String login(String username, String password) throws IOException {
this.ftp.login(username, password);
return this.ftp.getReplyString();
} public String login() throws IOException {
this.ftp.login(userName, password);
System.out.println("ftp用户: " + userName);
System.out.println("ftp密码: " + password);
if (!romateDir.equals(""))
System.out.println("cd " + romateDir);
ftp.changeWorkingDirectory(romateDir);
return this.ftp.getReplyString();
} public boolean upload(String pathname, String filename) throws IOException, BusinessException { int reply;
int j;
String m_sfilename = null;
filename = CheckNullString(filename);
if (filename.equals(""))
return false;
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
System.out.println("FTP server refused connection.");
System.exit(1);
}
FileInputStream is = null;
try {
File file_in;
if (pathname.endsWith(File.separator)) {
file_in = new File(pathname + filename);
} else {
file_in = new File(pathname + File.separator + filename);
}
if (file_in.length() == 0) {
System.out.println("上传文件为空!");
return false;
}
//产生随机数最大到99
j = (int)(Math.random()*100);
m_sfilename = String.valueOf(j) + ".pdf"; // 生成的文件名
is = new FileInputStream(file_in);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.storeFile(m_sfilename, is);
ftp.logout();
} finally {
if (is != null) {
is.close();
}
}
System.out.println("上传文件成功!");
return true;
}
public boolean delete(String filename) throws IOException { FileInputStream is = null;
boolean retValue = false;
try {
retValue = ftp.deleteFile(filename);
ftp.logout();
} finally {
if (is != null) {
is.close();
}
}
return retValue; } public void close() {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
} public static int FormatStringToInt(String p_String) {
int intRe = 0;
if (p_String != null) {
if (!p_String.trim().equals("")) {
try {
intRe = Integer.parseInt(p_String);
} catch (Exception ex) { }
}
}
return intRe;
} public static String CheckNullString(String p_String) {
if (p_String == null)
return "";
else
return p_String;
} public boolean downfile(String pathname, String filename) { String outputFileName = null;
boolean retValue = false;
try {
FTPFile files[] = ftp.listFiles();
int reply = ftp.getReplyCode(); ////////////////////////////////////////////////
if (!FTPReply.isPositiveCompletion(reply)) {
try {
throw new Exception("Unable to get list of files to dowload.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} /////////////////////////////////////////////////////
if (files.length == 0) {
System.out.println("No files are available for download.");
}else {
for (int i=0; i <files.length; i++) {
System.out.println("Downloading file "+files[i].getName()+"Size:"+files[i].getSize());
outputFileName = pathname + filename + ".pdf";
//return outputFileName;
File f = new File(outputFileName); //////////////////////////////////////////////////////
retValue = ftp.retrieveFile(outputFileName, new FileOutputStream(f)); if (!retValue) {
try {
throw new Exception ("Downloading of remote file"+files[i].getName()+" failed. ftp.retrieveFile() returned false.");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
} /////////////////////////////////////////////////////////////
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return retValue;
} }
试试这个
怀疑listFile函数的问题,我试了下,那个结果用utf-8,iso-8859-1 gbk,均不能得到正确结果,不知打那个编码到底是什么,也许根本不是编码的问题
在listFile函数中加入ftp.setControlEncoding("UTF-8");即可。
呵呵。
无论是写死,还是动态调用都可以了。
哎呀,网上搜了一大堆,每个人的问题都不一样,实在不懂那么多编码为毛。
* 删除文件或文件夹
*
* @param path
* 待删除文件的绝对路径
* @return boolean
*/
public boolean deleteFile(String path) throws ServiceException { if (ftpClient != null) {
try {
//如果是文件直接删除
ftpClient.deleteFile(path); if (ftpClient.changeWorkingDirectory(path)) {
//是文件夹
FTPFile[] ftpFiles = ftpClient.listFiles();
if (ftpFiles == null || ftpFiles.length <= 0) {
//文件夹文空直接删除
ftpClient.removeDirectory(path);
return true;
}
for (FTPFile ftpFile : ftpFiles) {
//文件夹不为空,先删文件再删文件夹
ftpClient.deleteFile(path+"/"+ftpFile.getName());
ftpClient.removeDirectory(path);
}
}
} catch (IOException e) {
throw new ServiceException(e.getMessage(), e);
}
}
return true;
}