日期:2014-05-17  浏览次数:20728 次

java 用FTPClient 下载文件时不显示总大小?

FTPClient ftp = new FTPClient();

ftp.setControlEncoding("utf-8");
OutputStream outputStream = null;
try {
    int reply;
    // 连接FTP服务器
    // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
    ftp.connect("192.168.1.111");
    // 登录ftp
    ftp.login("zhanghao", "mima");
    
    reply = ftp.getReplyCode();
    if (!FTPReply.isPositiveCompletion(reply)) {
     ftp.disconnect();
    }    
    
    response.setHeader("Content-Disposition","attachment; filename=\""+new String(filename.getBytes("gbk"),"iso8859-1")+"\"");
 
                    // 将文件保存到输出流outputStream中
                    InputStream in = ftp.retrieveFileStream(ftppath);
                    
                    FTPListParseEngine engine = ftp.initiateListParsing(ftppath);  
                
                    outputStream = response.getOutputStream();
 
                    int len = 0;
                    long size = 0;
                    byte[] bt = new byte[1024];
                    while ((len = in.read(bt)) > 0) {
 
                        outputStream.write(bt, 0, len); 
                        size = size + len;
                    }
       
                outputStream.flush();
                outputStream.close(); 
        ftp.logout();
        response.setStatus(HttpServletResponse.SC_OK ); 
        response.flushBuffer();
   } catch ( 关于序列化的异常!初学者