首先你要确定这个指定的文件夹中的文件是怎么生成的?如果是通过你写的程序生成的,就顺便把这个文件ftp到服务器上即可。如果是第三方主动加进来的,这要涉及到双方协商解决了,比如通过接口机制,触发接口,然后主动判断这个文件夹中有没有新文件,如果存在,就调用ftp到服务器上。

解决方案 »

  1.   

    怎么写
    监控进程用core java里的Thread类或Runnable接口实现,如果是FTP服务器用commons-net或jsch里的API,如果是SFTP服务器就用jsch。
    你不会是在找人要现成的源代码吗
      

  2.   

    用apache包:package com.test.project.ftp;import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.PrintWriter;
    import java.io.RandomAccessFile;import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.commons.net.PrintCommandListener;
    import org.apache.commons.net.ftp.FTP;
    import org.apache.commons.net.ftp.FTPClient;
    import org.apache.commons.net.ftp.FTPFile;
    import org.apache.commons.net.ftp.FTPReply;/**
     * 支持断点续传的FTP实用类 实现基本断点上传下载,支持上传下载进度
     * 
     * @author guishuanglin 2009-04-2
     */
    public class MyFTPClient {
    private Log logger = LogFactory.getLog(this.getClass());
    private String fileSplit = "/";
    private FTPClient ftpClient = new FTPClient(); public MyFTPClient() {
    // 设置将过程中使用到的命令输出到控制台
    this.ftpClient.addProtocolCommandListener(new PrintCommandListener(
    new PrintWriter(System.out)));
    } /**
     * 连接到FTP服务器
     * 
     * @param hostname
     *            主机名
     * @param port
     *            端口
     * @param username
     *            用户名
     * @param password
     *            密码
     * @return 是否连接成功
     * @throws IOException
     */
    public boolean connect(String hostname, int port, String username,
    String password, String charEncoding) throws IOException {
    ftpClient.connect(hostname, port);
    ftpClient.setControlEncoding(charEncoding);
    if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {
    if (ftpClient.login(username, password)) {
    return true;
    }
    }
    disconnect();
    return false;
    }
    /**
     * 从FTP服务器上下载文件,支持断点续传.
     * @param fileName 文件名,如:file.txt
     * @param localPath 本地文件路径
     *   使用/home/der/dir或是
     *       http://www.baidu.com/dir
     * @param remotePath 远程文件路径,不包括文件名
     * @return 
     * @throws IOException
     */
    public boolean download(String fileName, String localDir, String remoteDir)
    throws IOException {
    String localFile = localDir + this.fileSplit + fileName;
    String remoteFile = remoteDir + this.fileSplit + fileName;
    // 设置被动模式
    ftpClient.enterLocalPassiveMode();
    // 设置以二进制方式传输
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 检查远程文件是否存在
    FTPFile[] files = ftpClient.listFiles(remoteFile);
    if (files.length < 1) {
    logger.error("远程文件不存在");
    return false;
    }
    long remoteSize = files[0].getSize();
    File f = new File(localFile);
    // 本地存在文件,进行断点下载
    if (f.exists()) {
    long localSize = f.length();
    // 判断本地文件大小是否大于远程文件大小
    if (localSize >= remoteSize) {
    logger.error("本地文件大于远程文件,下载中止");
    return true;
    }
    // 进行断点续传,并记录状态
    FileOutputStream out = new FileOutputStream(f, true);
    ftpClient.setRestartOffset(localSize);
    InputStream in = ftpClient.retrieveFileStream(remoteFile); logger.info("下载文件开始");
    boolean result = this.downloadFile(in, remoteSize, out, localSize,
    ftpClient);
    if (result) {
    logger.info("下载文件完成");
    return true;
    } else {
    logger.error("下载文件失败");
    return false;
    }
    } else {
    OutputStream out = new FileOutputStream(f);
    InputStream in = ftpClient.retrieveFileStream(remoteFile); logger.info("下载文件开始");
    boolean result = this.downloadFile(in, remoteSize, out, 0,
    ftpClient);
    if (result) {
    logger.info("下载文件完成");
    return true;
    } else {
    logger.error("下载文件失败");
    return false;
    }
    }
    } private boolean downloadFile(InputStream in, long inSize, OutputStream out,
    long outSize, FTPClient ftpClient) throws IOException {
    long step = inSize / 100;
    long process = 0;
    long readbytes = 0L;
    //防止除0错误
    if(step==0)
    step = 1;
    // 断点续传
    if (outSize > 0) {
    System.out.println("文件存在,可进行断点续传.");
    ftpClient.setRestartOffset(outSize);
    process = outSize / step;
    readbytes = outSize;
    }
    byte[] bytes = new byte[1024];
    int c;
    while ((c = in.read(bytes)) != -1) {
    out.write(bytes, 0, c);
    readbytes += c;
    long nowProcess = readbytes / step;
    if (nowProcess > process) {
    process = nowProcess;
    if (process > 0 && process % 10 == 0) {
    if (process == 100) {
    System.out.println(process + "%");
    System.out.println("完成进度:" + process + "%");
    } else
    System.out.print(process + "%");
    } else {
    System.out.print(".");
    }
    }
    }
    in.close();
    out.close();
    return ftpClient.completePendingCommand();
    } /**
     * 上传文件到FTP服务器,支持断点续传
     * @param fileName 文件名,如:file.txt
     * @param localPath 本地文件路径
     *   使用/home/der/dir或是
     *       http://www.baidu.com/dir
     * @param remotePath 远程文件路径,不包括文件名
     * @return 
     * @throws IOException
     */
    public boolean upload(String fileName, String localDir, String remoteDir)
    throws IOException {
    String localFile = localDir + this.fileSplit + fileName;
    String remoteFile = remoteDir + this.fileSplit + fileName;
    // 设置PassiveMode传输
    ftpClient.enterLocalPassiveMode();
    // 设置以二进制流的方式传输
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 检查远程是否存在文件
    FTPFile[] files = ftpClient.listFiles(remoteFile);
    if (files.length >= 1) {
    long remoteSize = files[0].getSize();
    File f = new File(localFile);
    long localSize = f.length();
    if (remoteSize == localSize) {
    logger.info("远程文件已存在,上载完成");
    return true;
    } else if (remoteSize > localSize) {
    logger.error("远程文件大于本地文件,上载完成");
    return true;
    }
    // 尝试移动文件内读取指针,实现断点续传
    boolean result = uploadFile(remoteFile, f, ftpClient, remoteSize); // 如果断点续传没有成功,则删除服务器上文件,重新上传
    if (!result) {
    if (ftpClient.deleteFile(remoteFile)) {
    logger.error("远程文件存在,续传失败,已删除文件重转");
    return uploadFile(remoteFile, f, ftpClient, 0);
    } else {
    logger.error("远程文件存在,续传失败,删除文件进行重转,但删除失败.");
    return false;
    }
    } else {
    return result; }
    } else {
    return uploadFile(remoteFile, new File(localFile), ftpClient, 0);
    }
    } /**
     * 上传文件到服务器,新上传和断点续传
     * 
     * @param remotePath
     *            远程文件名,在上传之前已经将服务器工作目录做了改变
     * @param localPath
     *            本地文件File句柄,绝对路径
     * @param processStep
     *            需要显示的处理进度步进值
     * @param ftpClient
     *            FTPClient引用
     * @return
     * @throws IOException
     */
    private boolean uploadFile(String remotePath, File localPath,
    FTPClient ftpClient, long remoteSize) throws IOException {
    // 显示进度的上传
    long step = localPath.length() / 100;
    long process = 0;
    long localreadbytes = 0L;
    RandomAccessFile raf = new RandomAccessFile(localPath, "r");
    OutputStream out = ftpClient.appendFileStream(remotePath);
    //防止除0错误
    if(step==0)
    step = 1;
    // 断点续传
    if (remoteSize > 0) {
    System.out.println("文件存在,可进行断点续传.");
    ftpClient.setRestartOffset(remoteSize);
    process = remoteSize / step;
    raf.seek(remoteSize);
    localreadbytes = remoteSize;
    }
    byte[] bytes = new byte[1024];
    int c;
    while ((c = raf.read(bytes)) != -1) {
    out.write(bytes, 0, c);
    localreadbytes += c;
    if (localreadbytes / step != process) {
    process = localreadbytes / step;
    if (process > 0 && process % 10 == 0) {
    if (process == 100) {
    System.out.println(process + "%");
    System.out.println(" 上传进度:" + process + "%");
    } else
    System.out.print(process + "%");
    } else {
    System.out.print(".");
    }
    }
    }
    out.flush();
    raf.close();
    out.close();
    boolean result = ftpClient.completePendingCommand();
    if (result) {
    logger.info("上传文件完成");
    return true;
    } else {
    logger.error("上传文件失败");
    return false;
    }
    }
    /**
     * 断开与远程服务器的连接
     * 
     * @throws IOException
     */
    public void disconnect() throws IOException {
    if (ftpClient.isConnected()) {
    ftpClient.disconnect();
    }
    } /** 文件重名称,移动目录,根据目录用'/'代替 */
    public boolean rename(String from, String to){
    try {
    return ftpClient.rename(from, to);
    } catch (IOException e) {
    e.printStackTrace();
    return false;
    }
    }
    public static void main(String[] args) {
    MyFTPClient myFtp = new MyFTPClient();
    try {
                             //
    } catch (IOException e) {
    System.out.println("连接FTP出错:" + e.getMessage());
    }
    }
    }
      

  3.   

    上面是ftp,其它方法也有很多.
      

  4.   

    2014-01-03 星期五 
    16:01
    ftp> ftp> open 192.168.1.163
    Not connected.ftp> user Administrator 111
    Not connected.ftp> bin
    Interactive mode Off .ftp> prompt
    Not connected.ftp> cd C:\Documents and Settings\Administrator\桌面
    Local directory now C:\Inetpub\ftproot\web\image.ftp> lcd C:\Inetpub\ftproot\web\image
    Not connected.ftp> mget *
    Not connected.ftp> mdelete *
    bye
    报错的原因是什么