`

ftp上传

    博客分类:
  • FTP
 
阅读更多

1.使用sun.net.ftp.FtpClient 

package org.jasig.cas.a4.tools;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


import org.json.JSONObject;

import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;


/**
 * Title : AuditLog4AService
 * <p/>
 * Description : 4A审计日志非实时接口
 * <p/>
 * CopyRight : CopyRight (c) 2011
 * <p/>
 * Company : 亚信联创科技(中国)有限公司
 * <p/>
 * JDK Version Used : JDK 5.0 +
 * <p/>
 * Modification History :
 * <p/>
 * 
 * <pre>
 * </pre>
 * 

 */
public class Log4AService {
	
	
    /**
     * Description: 上传文件到FTP服务器
     * 
     * @param file
     *        上传文件
     */
    private static void uploadFile(File file) {
        String ftpServer = "10.1.252.96";
        String ftpUser = "smc";
        String ftpPwd = "smc";
        int ftpPort = 21;
       
        // 连接FTP服务器
        FtpClient ftpClient = new FtpClient();
        try {
            ftpClient.openServer(ftpServer, ftpPort);
            ftpClient.login(ftpUser, ftpPwd);
            ftpClient.cd("smc_bak"); 
            System.out.println("已登录到\"" + ftpClient.pwd() + "\"目录");  
            ftpClient.binary();
        } catch (IOException e) {
            throw new RuntimeException("FTP server connection failed. "
                    + e.getMessage(), e);
        }
        // 上传文件
        TelnetOutputStream tos = null;
        FileInputStream fis = null;
        try {
            fis = new FileInputStream(file);
            String fileName = file.getName();
            tos = ftpClient.put(fileName);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = fis.read(bytes)) != -1) {
                tos.write(bytes, 0, c);
            }
        } catch (FileNotFoundException e) {
            throw new RuntimeException("File is not exist. " + e.getMessage(),
                e);
        } catch (IOException e) {
            throw new RuntimeException("Error uploading files. "
                    + e.getMessage(), e);
        } finally {
            try {
                if (null != tos) {
                    tos.close();
                }
                if (null != fis) {
                    fis.close();
                }
            } catch (IOException ex) {
                throw new RuntimeException(
                    "An error occurred while the stream is closed. "
                            + ex.getMessage(), ex);
            }
        }
    }
    public static void main(String[] args) {
    	File file = new File("c:/temp/1.txt");
		uploadFile(file );
	}

}

 

2.ftp4j 上传

package org.jasig.cas.a4.tools;

import it.sauronsoftware.ftp4j.FTPClient;
import it.sauronsoftware.ftp4j.FTPFile;

import java.io.File;
import java.io.InputStream;

import org.apache.log4j.Logger;


public class FTPUtil {
	
	private Logger logger = Logger.getLogger(FTPUtil.class);
	
	private FTPClient client = null;
	private String ipAddress = null;
	private Integer port = null;
	private String username = null;
	private String password = null;
	public Boolean connect() throws Exception{
		Boolean bl = false;
		if(ipAddress!=null && port!=null && username!=null && password!=null){
			client = new FTPClient();  
			client.setCharset("utf-8");  
			client.setType(FTPClient.TYPE_BINARY);//二进制
			try {
				client.connect(ipAddress, port);
				client.login(username, password);
				bl = true;
			} catch (Exception e) {
				bl = false;
				logger.error("FTP connect failed!", e);
				throw new Exception("FTP connect failed!");
			} 
			
		}
		return bl;
	}

	public Boolean upload(File file) throws Exception{
		Boolean result = false;
		if(file!=null && file.exists()){
			try {
//				FTPUploadListener listener = new FTPUploadListener();
				client.upload(file);
			} catch (Exception e) {
				logger.error("FTP upload failed!"+file.getAbsolutePath(), e);
				throw new Exception("FTP upload failed! fileName:"+file.getName());
			}
		}else{
			logger.info("file not exists!");
		}
		return result;
	}
public static void main(String[] args) throws Exception {
		  FTPUtil ftp = new FTPUtil();
//		 ftp.init("10.1.253.117", 21, "bam", "bam");
		 ftp.init("10.1.252.96", 21, "smc", "smc");
		 File file = new File("c:/temp/1.txt");
		 System.out.println(file.exists());
		 ftp.connect();
		 ftp.upload(file);
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics