• 图1:FileZilla配置信息"> 图1:FileZilla配置信息

    https://blog.csdn.net/dhu86380/article/details/102064930
    http://www.linuxidc.com/Linux/2014-10/107585.htm

    传输编码必须为 ISO-8859-1
    内容编码根据服务器支持匹配

    image.png
    image.png

    最近在做文件服务器的相关东西,在原有的磁盘存储的基础上,增加了Ftp的存储方式,客户端选用的是Apache的FtpClient。 今天在测试的时候,发现中文的路径后者文件名不支持,查阅了相关资料后终于找到了解决的方法。
    使用类:
    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配置信息

    1. <br /> **图2:FileZilla连接时信息**<br />**从图2中我们可以看到原来它向服务器发送了OPTS UTF8 ON命令,来开启服务器对UTF-8的支持。所以我们也可以仿照**FileZilla**那样向服务器发送该命令。如果服务器支持UTF-8我们就用UTTF-8,否则我们就用本地编码(GBK)来处理中文文件名。**<br />**下面是Java代码:**<br />/** 本地字符编码 */<br />private static String LOCAL_CHARSET = "GBK";<br /> <br />// FTP协议里面,规定文件名编码为iso-8859-1<br />private static String SERVER_CHARSET = "ISO-8859-1";<br /> <br />private void connectFtpServer() {<br />if (ftpClient == null) {<br />ftpClient = new FTPClient();<br />}<br />if (ftpClient.isConnected()) {<br />return;<br />}<br />String host = getConfigValue(ADDRESS);<br />int port = Integer.valueOf(getConfigValue(PORT));<br />String user = getConfigValue(USER);<br />String password = getConfigValue(PASSWORD);<br />try {<br />ftpClient.connect(host, port);<br />if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {<br />if (ftpClient.login(user, password)) {<br />if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(<br />"OPTS UTF8", "ON"))) {// 开启服务器对UTF-8的支持,如果服务器支持就用UTF-8编码,否则就使用本地编码(GBK).<br />LOCAL_CHARSET = "UTF-8";<br />}<br />ftpClient.setControlEncoding(LOCAL_CHARSET);<br />ftpClient.enterLocalPassiveMode();// 设置被动模式<br />ftpClient.setFileType(getTransforModule());// 设置传输的模式<br />return;<br />} else {<br />throw new FileStorageException(<br />"Connet ftpServer error! Please check user or password");<br />}<br />}<br />} catch (IOException e) {<br />disConnectServer();<br />throw new FileStorageException(<br />"Connet ftpServer error! Please check the Configuration");<br />}<br />}<br />上传文件时,文件名称需要做编码转换<br />fileName = new String(fileName.getBytes(LOCAL_CHARSET),<br />SERVER_CHARSET);<br />通过上述方法,就能解决了中文乱码的问题。

    package com.toyor.utils;

    import net.sf.json.JSONArray;
    import net.sf.json.JSONObject;
    import org.apache.commons.lang.StringUtils;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPReply;

    import javax.servlet.http.HttpServletRequest;
    import java.io.;
    import java.net.SocketException;
    import java.util.ArrayList;
    import java.util.Arrays;
    *import
    java.util.List;

    public class FtpUtil {
    //qzf ftp
    private static String ueditorDir = “ueditor”;
    private static String localCharSet = “GBK”;
    private static String serverCharSet = “ISO-8859-1”;
    /*
    获取FTPClient对象

    @param ftpHost FTP主机服务器
    *
    @param ftpPassword FTP 登录密码
    *
    @param ftpUserName FTP登录用户名
    *
    @param ftpPort FTP端口 默认为21
    *
    @return
    */
    public static FTPClient getFTPClient(String ftpHost, String ftpUserName,
    String ftpPassword, int ftpPort) {
    FTPClient ftpClient = new FTPClient();
    try {
    ftpClient = new FTPClient();
    ftpClient.connect(ftpHost, ftpPort);// 连接FTP服务器
    _ftpClient.login(ftpUserName, ftpPassword);
    // 登陆FTP服务器
    if (!FTPReply._isPositiveCompletion(ftpClient.getReplyCode())) {
    System.out.println(“未连接到FTP,用户名或密码错误。”);
    ftpClient.disconnect();
    } else {
    System.out.println(“FTP连接成功。”);
    }
    } catch (SocketException e) {
    e.printStackTrace();
    System.out.println(“FTP的IP地址可能错误,请正确配置。”);
    } catch (IOException e) {
    e.printStackTrace();
    System.out.println(“FTP的端口错误,请正确配置。”);
    }
    return ftpClient;
    }
    public static FTPClient getFTPClient() {
    String ftpHost = SystemConfigUtil.getProperty(“imgFtpIp”);
    String ftpPort = SystemConfigUtil.getProperty(“imgFtpPort”);
    String ftpUserName = SystemConfigUtil.getProperty(“imgFtpUserName”);
    String ftpPassword = SystemConfigUtil.getProperty(“imgFtpPwd”);
    return getFTPClient(ftpHost, ftpUserName, ftpPassword, Integer.valueOf(ftpPort));
    }
    /
    从FTP服务器下载文件

    @param ftpHost FTP IP地址 * @param ftpUserName FTP 用户名 @param ftpPassword FTP用户名密码_ @param ftpPort FTP端口 @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
    @param localPath 下载到本地的位置 格式:H:/download
    * @param fileName 文件名称
    /
    _public static void downloadFtpFile(String ftpHost, String ftpUserName,
    String ftpPassword, *int
    ftpPort, String ftpPath, String localPath,
    String fileName) {

    1. FTPClient ftpClient = **null**;
    2. **try **{<br /> ftpClient = _getFTPClient_(ftpHost, ftpUserName, ftpPassword, ftpPort);<br /> ftpClient.setControlEncoding(**"UTF-8"**); _// 中文支持<br /> _ftpClient.setFileType(FTPClient.**BINARY_FILE_TYPE**);<br /> ftpClient.enterLocalPassiveMode();<br /> ftpClient.changeWorkingDirectory(ftpPath);
    3. File localFile = **new **File(localPath + File.**separatorChar **+ fileName);<br /> OutputStream os = **new **FileOutputStream(localFile);<br /> ftpClient.retrieveFile(fileName, os);<br /> os.close();<br /> ftpClient.logout();
    4. } **catch **(FileNotFoundException e) {<br /> System.**out**.println(**"没有找到" **+ ftpPath + **"文件"**);<br /> e.printStackTrace();<br /> } **catch **(SocketException e) {<br /> System.**out**.println(**"连接FTP失败."**);<br /> e.printStackTrace();<br /> } **catch **(IOException e) {<br /> e.printStackTrace();<br /> System.**out**.println(**"文件读取错误。"**);<br /> e.printStackTrace();<br /> }

    }

    /*
    Description: 向FTP服务器上传文件
    *
    @param ftpHost FTP服务器hostname
    *
    @param ftpUserName 账号
    *
    @param ftpPassword 密码
    *
    @param ftpPort 端口
    *
    @param ftpPath FTP服务器中文件所在路径 格式: ftptest/aa
    *
    @param fileName ftp文件名称
    *
    @param input 文件流
    *
    @return 成功返回true,否则返回false
    */
    public static boolean uploadFile(String ftpHost, String ftpUserName,
    String ftpPassword, int ftpPort, String ftpPath,
    String fileName,InputStream input) {
    boolean success = false;
    FTPClient ftpClient = null;
    try {
    int reply;
    ftpClient = getFTPClient(ftpHost, ftpUserName, ftpPassword, ftpPort);
    reply = ftpClient.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
    ftpClient.disconnect();
    return success;
    }
    if (FTPReply.isPositiveCompletion(ftpClient.sendCommand(
    “OPTS UTF8”, “ON”))) {
    localCharSet = “UTF-8”;
    }
    ftpClient.setControlEncoding(localCharSet);
    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
    ftpClient.enterLocalPassiveMode();
    if (!ftpClient.changeWorkingDirectory(ftpPath)) {
    //如果目录不存在创建目录
    _String[] dirs = ftpPath.split(“/“);
    String tempPath = “”;
    for (String dir : dirs) {
    if (null == dir || “”.equals(dir)) continue;
    //tempPath += “/“ + dir;
    tempPath = dir ;
    tempPath = new String(tempPath.getBytes(_localCharSet
    ),serverCharSet);
    if (!ftpClient.changeWorkingDirectory(tempPath)) {
    if (!ftpClient.makeDirectory(tempPath)) {
    return success;
    } else {
    ftpClient.changeWorkingDirectory(tempPath);
    }
    }
    }
    }
    fileName = new String(fileName.getBytes(localCharSet),serverCharSet);

    ftpClient.storeFile(fileName, input);

    1. input.close();<br /> ftpClient.logout();<br /> success = **true**;<br /> } **catch **(IOException e) {<br /> e.printStackTrace();<br /> } **finally **{<br /> **if **(ftpClient.isConnected()) {<br /> **try **{<br /> ftpClient.disconnect();<br /> } **catch **(IOException ioe) {<br /> }<br /> }<br /> }<br /> **return **success;<br /> }<br /> _//上传图片(web MultipartFile 上传)<br /> _**public static boolean **uploadFileMultiPart(String toyorno, String pathKey, String fileName,InputStream input) {<br /> String imgFtpUpload = SystemConfigUtil._getProperty_(**"imgFtpUpload"**);<br /> _//String ftpPath = imgFtpUpload + "/"+ toyorno+"/"+(new ImgFilePath()).getProperty(pathKey); // del by lance<br /> _String ftpPath = imgFtpUpload + toyorno+**"/"**+(**new **ImgFilePath()).getProperty(pathKey); _// add by lance<br /> _**boolean **success = **false**;<br /> FTPClient ftpClient = **null**;<br /> **try **{<br /> **int **reply;<br /> ftpClient = _getFTPClient_();<br /> reply = ftpClient.getReplyCode();<br /> **if **(!FTPReply._isPositiveCompletion_(reply)) {<br /> ftpClient.disconnect();<br /> **return **success;<br /> }<br /> **if **(FTPReply._isPositiveCompletion_(ftpClient.sendCommand(<br /> **"OPTS UTF8"**, **"ON"**))) {<br /> _localCharSet _= **"UTF-8"**;<br /> }<br /> ftpClient.setControlEncoding(_localCharSet_);<br /> ftpClient.setFileType(FTPClient.**BINARY_FILE_TYPE**);<br /> ftpClient.enterLocalPassiveMode();<br /> **if **(!ftpClient.changeWorkingDirectory(ftpPath)) {<br /> _//如果目录不存在创建目录<br /> _String[] dirs = ftpPath.split(**"/"**);<br /> String tempPath = **""**;<br /> **for **(String dir : dirs) {<br /> **if **(**null **== dir || **""**.equals(dir)) **continue**;<br /> _//tempPath += "/" + dir;<br /> _tempPath = dir ;<br /> tempPath = **new **String(tempPath.getBytes(_localCharSet_),_serverCharSet_);<br /> **if **(!ftpClient.changeWorkingDirectory(tempPath)) {<br /> **if **(!ftpClient.makeDirectory(tempPath)) {<br /> **return **success;<br /> } **else **{<br /> ftpClient.changeWorkingDirectory(tempPath);<br /> }<br /> }<br /> }<br /> }<br /> fileName = **new **String(fileName.getBytes(_localCharSet_),_serverCharSet_);<br /> ftpClient.storeFile(fileName, input);<br /> input.close();<br /> ftpClient.logout();<br /> success = **true**;<br /> } **catch **(IOException e) {<br /> e.printStackTrace();<br /> } **finally **{<br /> **if **(ftpClient.isConnected()) {<br /> **try **{<br /> ftpClient.disconnect();<br /> } **catch **(IOException ioe) {<br /> }<br /> }<br /> }<br /> **return **success;<br /> }<br /> _//上传图片(手机先上传临时文件,再上传ftp)<br /> _**public static boolean **uploadFileMobile(String pathKey , String fileName, HttpServletRequest request) {<br /> **if**(StringUtils._isEmpty_(fileName)) **return false**;<br /> String tempDir = (**new **ImgFilePath()).getProperty(**"temp"**).trim();<br /> **if**(StringUtils._isNotEmpty_(tempDir) && !tempDir.endsWith(**"/"**)){<br /> tempDir += **"/"**;<br /> }<br /> String tmpFileName = tempDir + fileName;<br /> File tmpFile = **new **File(tmpFileName);<br /> **if**(!tmpFile.exists()) **return false**;<br /> String toyorno = RequestHolder._getToyorno_();
    2. String imgFtpUpload = SystemConfigUtil._getProperty_(**"imgFtpUpload"**);<br /> String ftpPath = imgFtpUpload + **"/"**+ toyorno+**"/"**+(**new **ImgFilePath()).getProperty(pathKey);<br /> **boolean **success = **false**;<br /> FTPClient ftpClient = **null**;<br /> **try **{<br /> **int **reply;<br /> ftpClient = _getFTPClient_();<br /> reply = ftpClient.getReplyCode();<br /> **if **(!FTPReply._isPositiveCompletion_(reply)) {<br /> ftpClient.disconnect();<br /> **return **success;<br /> }<br /> **if **(FTPReply._isPositiveCompletion_(ftpClient.sendCommand(<br /> **"OPTS UTF8"**, **"ON"**))) {<br /> _localCharSet _= **"UTF-8"**;<br /> }<br /> ftpClient.setControlEncoding(_localCharSet_);<br /> ftpClient.setFileType(FTPClient.**BINARY_FILE_TYPE**);<br /> ftpClient.enterLocalPassiveMode();<br /> **if **(!ftpClient.changeWorkingDirectory(ftpPath)) {<br /> _//如果目录不存在创建目录<br /> _String[] dirs = ftpPath.split(**"/"**);<br /> String tempPath = **""**;<br /> **for **(String dir : dirs) {<br /> **if **(**null **== dir || **""**.equals(dir)) **continue**;<br /> _//tempPath += "/" + dir;<br /> _tempPath = dir ;<br /> tempPath = **new **String(tempPath.getBytes(_localCharSet_),_serverCharSet_);<br /> **if **(!ftpClient.changeWorkingDirectory(tempPath)) {<br /> **if **(!ftpClient.makeDirectory(tempPath)) {<br /> **return **success;<br /> } **else **{<br /> ftpClient.changeWorkingDirectory(tempPath);<br /> }<br /> }<br /> }<br /> }
    3. FileInputStream input = **new **FileInputStream(tmpFile);
    4. fileName = **new **String(fileName.getBytes(_localCharSet_),_serverCharSet_);<br /> ftpClient.storeFile(fileName, input);<br /> input.close();<br /> ftpClient.logout();<br /> tmpFile.delete();<br /> success = **true**;<br /> } **catch **(IOException e) {<br /> e.printStackTrace();<br /> } **finally **{<br /> **if **(ftpClient.isConnected()) {<br /> **try **{<br /> ftpClient.disconnect();<br /> } **catch **(IOException ioe) {<br /> }<br /> }<br /> }<br /> **return **success;<br /> }<br /> _//上传图片(ueditor上传后,再上传ftp)<br /> _**public static **String uploadFileUeditor(String rootPath,String result, HttpServletRequest request) {<br /> JSONObject jsonObject = **null**;<br /> **try **{<br /> jsonObject = JSONObject._fromObject_(result);<br /> }<br /> **catch **(Exception e){<br /> System.**out**.println(e.getMessage());<br /> }<br /> **if**(jsonObject == **null**){<br /> System.**out**.println(**"ueditor上传出错!"**);<br /> **return ""**;<br /> }<br /> String imgtype = request.getParameter(**"imgtype"**);<br /> String keyno = request.getParameter(**"keyno"**);<br /> String action = request.getParameter(**"action"**);<br /> _//ftp 上传相对路径(toyorno的子目录)<br /> _String path = _ueditorDir_+**"/"**+imgtype+**"/"**+keyno+**"/"**;<br /> _//ueditor上传的图片全路径<br /> _String ueditorFullFileName = rootPath + jsonObject.get(**"url"**).toString().substring(1);
    5. String toyorno = RequestHolder._getToyorno_();<br /> **if**(StringUtils._isEmpty_(toyorno)){<br /> System.**out**.println(**"上传出错,toyorno不能为空!"**);<br /> **return ""**;<br /> }<br /> **if**(StringUtils._isEmpty_(keyno)){<br /> System.**out**.println(**"上传出错,keyno不能为空!"**);<br /> **return ""**;<br /> }<br /> String imgFtpUpload = SystemConfigUtil._getProperty_(**"imgFtpUpload"**);<br /> String ftpPath = imgFtpUpload + **"/"**+ toyorno+**"/"**+path;<br /> **boolean **success = **false**;<br /> FTPClient ftpClient = **null**;<br /> **try **{<br /> **int **reply;<br /> ftpClient = _getFTPClient_();<br /> reply = ftpClient.getReplyCode();<br /> **if **(!FTPReply._isPositiveCompletion_(reply)) {<br /> ftpClient.disconnect();<br /> **return ""**;<br /> }<br /> **if **(FTPReply._isPositiveCompletion_(ftpClient.sendCommand(<br /> **"OPTS UTF8"**, **"ON"**))) {<br /> _localCharSet _= **"UTF-8"**;<br /> }<br /> ftpClient.setControlEncoding(_localCharSet_);<br /> ftpClient.setFileType(FTPClient.**BINARY_FILE_TYPE**);<br /> ftpClient.enterLocalPassiveMode();<br /> **if **(!ftpClient.changeWorkingDirectory(ftpPath)) {<br /> _//如果目录不存在创建目录<br /> _String[] dirs = ftpPath.split(**"/"**);<br /> String tempPath = **""**;<br /> **for **(String dir : dirs) {<br /> **if **(**null **== dir || **""**.equals(dir)) **continue**;<br /> _//tempPath += "/" + dir;<br /> _tempPath = dir ;<br /> tempPath = **new **String(tempPath.getBytes(_localCharSet_),_serverCharSet_);<br /> **if **(!ftpClient.changeWorkingDirectory(tempPath)) {<br /> **if **(!ftpClient.makeDirectory(tempPath)) {<br /> **return ""**;<br /> } **else **{<br /> ftpClient.changeWorkingDirectory(tempPath);<br /> }<br /> }<br /> }<br /> }
    6. String tmpFileName = ueditorFullFileName;<br /> File tmpFile = **new **File(tmpFileName);<br /> FileInputStream input = **new **FileInputStream(tmpFile);
    7. ftpClient.storeFile(**new **String(tmpFile.getName().getBytes(_localCharSet_),_serverCharSet_), input);<br /> input.close();<br /> ftpClient.logout();<br /> tmpFile.delete();<br /> success = **true**;<br /> jsonObject.put(**"url"**, SystemConfigUtil._getProperty_(**"imgHttpServer"**).replace(SystemConfigUtil._getProperty_(**"imgFtpUpload"**),**""**) +ftpPath+tmpFile.getName());
    8. } **catch **(IOException e) {<br /> e.printStackTrace();<br /> } **finally **{<br /> **if **(ftpClient.isConnected()) {<br /> **try **{<br /> ftpClient.disconnect();<br /> } **catch **(IOException ioe) {<br /> }<br /> }<br /> }<br /> **return **jsonObject.toString();<br /> }<br /> _/**<br /> * 返回FTP目录下的文件列表<br /> * _**@param ftpDirectory<br /> **_* _**@return<br /> **_*/<br /> _**public static **List<String> getFileNameList(String ftpDirectory)<br /> {<br /> List<String> list = **new **ArrayList<String>();<br /> **int **reply;<br /> FTPClient ftpClient = _getFTPClient_();<br /> **try **{<br /> reply = ftpClient.getReplyCode();<br /> **if **(!FTPReply._isPositiveCompletion_(reply)) {<br /> ftpClient.disconnect();<br /> **return **list;<br /> }<br /> **try **{<br /> String[] listNames = ftpClient.listNames(ftpDirectory);<br /> list = Arrays._asList_(listNames);<br /> } **catch **(Exception e) {<br /> e.printStackTrace();<br /> }<br /> }**catch **(Exception e){
    9. }<br /> **return **list;<br /> }<br /> **public static **String getFileNameListForUeditor(HttpServletRequest request)<br /> {<br /> String imgtype = request.getParameter(**"imgtype"**);<br /> String keyno = request.getParameter(**"keyno"**);<br /> String action = request.getParameter(**"action"**);
    10. String ftpDirectory = SystemConfigUtil._getProperty_(**"imgFtpUpload"**)+ RequestHolder._getToyorno_()+**"/"**+_ueditorDir_+**"/"**+imgtype+**"/"**+keyno+**"/"**;<br /> List<String> list = _getFileNameList_(ftpDirectory);<br /> **if**(list.size()<=0) **return ""**;<br /> JSONObject jsonObjectResult = **new **JSONObject();<br /> jsonObjectResult.put(**"state"**,**"SUCCESS"**);<br /> jsonObjectResult.put(**"total"**,list.size());<br /> jsonObjectResult.put(**"start"**,0);<br /> JSONArray listfile = **new **JSONArray();<br /> **for**(String filename :list){<br /> JSONObject jsonObject = **new **JSONObject();<br /> jsonObject.put(**"state"**,**"SUCCESS"**);<br /> jsonObject.put(**"url"**, SystemConfigUtil._getProperty_(**"imgHttpServer"**).replace(SystemConfigUtil._getProperty_(**"imgFtpUpload"**),**""**) + filename);<br /> listfile.add(jsonObject);<br /> }<br /> jsonObjectResult.put(**"list"**,listfile);<br /> **return **jsonObjectResult.toString();<br /> }<br /> _//Ueditor<br />_ //删除ftp文件<br /> _**public static boolean **deleteFile(String pathKey , String fileName) {<br /> **if**(StringUtils._isEmpty_(fileName)) **return false**;<br /> String toyorno = RequestHolder._getToyorno_();<br /> String imgFtpUpload = SystemConfigUtil._getProperty_(**"imgFtpUpload"**);<br /> String ftpPath = imgFtpUpload + **"/"**+ toyorno+**"/"**+(**new **ImgFilePath()).getProperty(pathKey);<br /> **boolean **flag = **false**;<br /> FTPClient ftpClient = **null**;<br /> **try **{<br /> **int **reply;<br /> ftpClient = _getFTPClient_();<br /> reply = ftpClient.getReplyCode();<br /> **if **(!FTPReply._isPositiveCompletion_(reply)) {<br /> ftpClient.disconnect();<br /> **return false**;<br /> }<br /> **if **(FTPReply._isPositiveCompletion_(ftpClient.sendCommand(<br /> **"OPTS UTF8"**, **"ON"**))) {<br /> _localCharSet _= **"UTF-8"**;<br /> }<br /> ftpClient.setControlEncoding(_localCharSet_);<br /> _// 切换FTP目录<br /> _ftpClient.changeWorkingDirectory(ftpPath);<br /> fileName = **new **String(fileName.getBytes(_localCharSet_),_serverCharSet_);<br /> ftpClient.dele(fileName);<br /> ftpClient.logout();<br /> flag = **true**;<br /> System.**out**.println(**"删除文件成功"**);<br /> } **catch **(Exception e) {<br /> System.**out**.println(**"删除文件失败"**);<br /> e.printStackTrace();<br /> } **finally **{<br /> **if **(ftpClient.isConnected()) {<br /> **try **{<br /> ftpClient.disconnect();<br /> } **catch **(IOException e) {<br /> e.printStackTrace();<br /> }<br /> }<br /> }<br /> **return **flag;<br /> }<br />}