您的位置:首页 > 编程语言 > PHP开发

FTPClient上传和下载文件,中文乱码问题解决

2015-07-22 11:22 706 查看
使用类:

org.apache.commons.net.ftp.FTPClient

问题描述:

创建中文目录、上传中文文件名时,目录名及文件名中的中文显示乱码

解决方法:

在网上Google了一些资料, FTP协议里面,规定文件名编码为iso-8859-1,所以目录名或文件名需要转码。

所以网上很多人的解决方法为:

将中文的目录或文件名转为iso-8859-1编码的字符。参考代码:

String name="目录名或文件名";

name=new String(name.getBytes("GBK"),"iso-8859-1");

很多人改为上述操作后,发现上传后中文不再乱码了,就以为解决了问题

还有人处理方法为:

ftpClient.setControlEncoding("GBK");

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);

conf.setServerLanguageCode("zh");

上述的处理方法,我都试过,发现在我自己搭建的FTP服务器上,上传的文件中文是正常的,不是乱码,我当时以为中文问题就解决了,但是测试部的同事在将文件上传到他们搭建的FTP服务器上时,文件中文路径仍然是乱码,所以上述的解决方法是错误的。

上面的方法之所以错误的原因是因为是没有考虑ftp服务器的编码格式。我搭建的Ftp服务器(windows2003 server)支持GBK编码方式,所以上述的解决方法可以,但是测试部的同事搭建的Ftp服务器(serv-u)是支持UTF-8格式的,所以此时在客户端的编码方式是GBK的,而搭设的ftp服务器中的设置就已经是utf-8的编码,所以肯定还是会出现乱码的问题。

那么正确的解决方法时是什么呢,我们可以仿照FlashFXP、FileZilla等ftp 客户端连接工具,看看他们是如何实现的,下面的两张图就是FileZilla的配置信息和连接时的命令信息。




图1:FileZilla配置信息


图2:FileZilla连接时信息

从图2中我们可以看到原来它向服务器发送了OPTS UTF8 ON命令,来开启服务器对UTF-8的支持。所以我们也可以仿照[b]FileZilla那样向服务器发送该命令。如果服务器支持UTF-8我们就用UTTF-8,否则我们就用本地编码(GBK)来处理中文文件名。[/b]

下面是Java代码:

/** 本地字符编码 */

private static String LOCAL_CHARSET = "GBK";

// FTP协议里面,规定文件名编码为iso-8859-1

private static String SERVER_CHARSET = "ISO-8859-1";

private void connectFtpServer() {

if (ftpClient == null) {

ftpClient = new FTPClient();

}

if (ftpClient.isConnected()) {

return;

}

String host = getConfigValue(ADDRESS);

int port = Integer.valueOf(getConfigValue(PORT));

String user = getConfigValue(USER);

String password = getConfigValue(PASSWORD);

try {

ftpClient.connect(host, port);

if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {

if (ftpClient.login(user, password)) {

if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(

"OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).

LOCAL_CHARSET = "UTF-8";

}

ftpClient.setControlEncoding(LOCAL_CHARSET);

ftpClient.enterLocalPassiveMode();// 设置被动模式

ftpClient.setFileType(getTransforModule());// 设置传输的模式

return;

} else {

throw new FileStorageException(

"Connet ftpServer error! Please check user or password");

}

}

} catch (IOException e) {

disConnectServer();

throw new FileStorageException(

"Connet ftpServer error! Please check the Configuration");

}

}

上传文件时,文件名称需要做编码转换

fileName = new String(fileName.getBytes(LOCAL_CHARSET),

SERVER_CHARSET);

通过上述方法,就能解决了中文乱码的问题
-----------------------------------------------------------------------------以上为转载,出处:http://www.linuxidc.com/Linux/2014-10/107585.htm-----------------

servlet+jsp用FileZilla Server服务器完成文件上传及下载。

需注意的地方:上传:1.后台获取前台jsp页面选择的文件路径(包含中文)时,要对路径进行编码

《部分代码》 String filePath = request.getParameter("fileName");

String name = filePath.substring(filePath.lastIndexOf("\\")+1);//获得文件名

System.out.println(name+"-----------"+filePath);

//为中文文件设置编码格式,正确解析

name = new String(name.getBytes("iso-8859-1"),"utf-8");

filePath = new String(filePath.getBytes("iso-8859-1"),"utf-8");

File srcFile = new File(filePath);

fis = new FileInputStream(srcFile);

//设置上传目录

ftpClient.changeWorkingDirectory("/upload");

ftpClient.setBufferSize(1024);

//设置文件类型(二进制)

ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);

ftpClient.storeFile(new String(name.getBytes("gbk"), "iso-8859-1") , fis);

下载:2.提供两种下载方式

String fileName = request.getParameter("fileName");

fileName = new String(fileName.getBytes("iso-8859-1"),"utf-8");

FTPClient ftp = new FTPClient();

FileOutputStream fos = null;

String localPath = request.getRealPath("") + File.separator + "Download" + File.separator;

File localFile = new File(localPath + "" + fileName);

// 下载文件的路径

String path = localPath
+ "" + fileName;

/*

* 使用FTP下载:把FTP服务器上面的文件下到Tomcat工程目录下,指定下载路径。

*/

try{

ftp.connect("127.0.0.1");

ftp.login("shmily", "shmily");

String remoteFileName = "/Download/"+ "" + fileName;

ftp.changeWorkingDirectory(remoteFileName);

fos = new FileOutputStream(path);

ftp.retrieveFile(new String(remoteFileName.getBytes("gbk"), "iso-8859-1"), fos);

}catch (IOException e) {

e.printStackTrace();

throw new RuntimeException("FTP客户端出错!", e);

} finally {

IOUtils.closeQuietly(fos);

try {

ftp.disconnect();

} catch (IOException e) {

e.printStackTrace();

throw new RuntimeException("关闭FTP连接发生异常!", e);

}

}

/*

* 使用浏览器下载:将Tomcat工程路径下的文件下到本地,可选择下载路径。

*/

if (!localFile.exists())

localFile.createNewFile();

response.setContentType("application/x-download");// 设置为下载application/x-download

fileName = URLEncoder.encode(fileName, "UTF-8");

response.addHeader("Content-Disposition", "attachment;filename=" + fileName);// 下载文件时,显示的文件名

OutputStream output = response.getOutputStream();

FileInputStream fis = new FileInputStream(path);

byte[] b = new byte[10240];

int i = 0;

while ((i = fis.read(b)) > 0) {

output.write(b, 0, i);

}

output.flush();

fis.close();

output.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: