本帖最后由 myerpso 于 2010-07-22 22:33:02 编辑

解决方案 »

  1.   


    /**
     * 
     * @param url FTP服务器hostname
     * @param port FTP服务器端口
     * @param username FTP登录账号
     * @param password FTP登录密码
     * @param path FTP服务器保存目录
     * @param filename 上传到FTP服务器上的文件名
     * @param input 输入流
     * @return 成功返回true,否则返回false
     */
    public static boolean uploadFile(String url, int port, String username, String password, 
    String path, String filename, InputStream input) throws Exception {
    boolean success = false;
    FTPClient ftp = new FTPClient();
    try {
    int reply;
    ftp.connect(url, port);//连接FTP服务器
    //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
    ftp.login(username, password);//登录
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
    ftp.disconnect();
    logger.error("----------->>>连接ftp服务器失败");
    //throw new Exception("----------->>>连接ftp服务器失败");
    }
    logger.info("-----连接ftp服务器成功");
    ftp.changeWorkingDirectory(path);
    ftp.storeFile(filename, input);

    input.close();
    ftp.logout();
    success = true;
    logger.info("----------->>>文件上传成功");

    catch (Exception e) {
    e.printStackTrace();
    logger.error("----------->>>ftp上传文件时失败 " + e.getMessage());
    //throw new Exception(e);
    }
    finally {
    if (ftp.isConnected()) {
    try {
    ftp.disconnect();
    } catch (IOException ioe) {
    logger.error("----------->>>ftp连接关闭失败 " + ioe.getMessage());
    }
    }
    }
    return success;
    }断点调试,一行行执行下来,都不报错,就是中文文件传不上去,英文的就能传上去。
      

  2.   

    ftp.setControlEncoding("UTF-8");
    你没设置编码
      

  3.   

    我的可以上传中文的 
    ftpClient.ascii(); 我有这个设置  你加上试试
      

  4.   

    加上了这两句ftp.setFileType(FTP.ASCII_FILE_TYPE);

    ftp.setControlEncoding("UTF-8");还是不行
    另外找不到 ascii() 这个方法啊
    用的是 org.apache.commons.net.ftp.FTPClient
      

  5.   


    public static FTPClient connectServer(String server, int port) throws SocketException, IOException {
            FTPClient ftpClient = new FTPClient();        ftpClient.connect(server, port);        ftpClient.enterLocalPassiveMode();        ftpClient.setControlEncoding("GBK");        if(!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
                ftpClient.disconnect();
                ftpClient = null;
            }        return ftpClient;
        }    public static boolean loginFTP(FTPClient ftpClient, String userName, String password) throws IOException {
            boolean flag = false;        if(ftpClient != null && ftpClient.isConnected()) {
                flag = ftpClient.login(userName, password);
                ftpClient.setFileType(BINARY_FILE_TYPE);
            }        return flag;
        }public static boolean uploadFile(FTPClient ftpClient, java.io.File localFile, String remoteFileName, Long remoteSize) throws IOException {
            boolean flag = false;        if(ftpClient != null && ftpClient.isConnected() && localFile.exists()) {
                RandomAccessFile raf = new RandomAccessFile(localFile, "r");
                OutputStream out = ftpClient.appendFileStream(new String(remoteFileName.getBytes("GBK"), "ISO-8859-1"));            if(remoteSize > 0) {    //断点上传
                    ftpClient.setRestartOffset(remoteSize);
                    raf.seek(remoteSize);
                }            byte[] bytes = new byte[1024];
                int c;
                while((c = raf.read(bytes)) != -1) {
                    out.write(bytes, 0, c);
                }            out.flush();
                raf.close();
                out.close();            flag = ftpClient.completePendingCommand();
            }        return flag;
        }这是以前写的FTP方法,你试试看
      

  6.   

    new String(remoteFileName.getBytes("GBK"), "ISO-8859-1")
    点睛之笔!就是这句话!