欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java实现FTP上传下载

程序员文章站 2022-06-02 14:10:41
...

 

Java实现FTP上传下载

 

 

用到jar包的Maven依赖:

<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
	<version>2.4</version>
</dependency>
<dependency>
	<groupId>commons-net</groupId>
	<artifactId>commons-net</artifactId>
	<version>3.2</version>
</dependency>

 

 

Demo:

package com.ydc.ln.codebase.java.app;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.io.IOUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

/**
 * @ClassName FtpDemo
 * @Description TODO
 * @author ydc
 * @date 下午10:03:36 2013-3-18
 * @version V1.0
 */
public class FtpDemo {
    public static String FTP_USERNAME = "ydc";

    public static String FTP_PASSWORD = "ydc123";

    public static String FTP_IP = "192.168.1.3";

    public static String UPLOAD_FILE_NAME = "codeList.xml";

    public static String UPLOAD_FILE_PATH = "F:/" + UPLOAD_FILE_NAME;

    public static String DOWNLOAD_FILE_NAME = "abc.jpg";

    public static String DOWNLOAD_FILE_PATH = "F:/" + DOWNLOAD_FILE_NAME;

    /**
     * FTP上传单个文件测试
     */
    public void upload() {
	FTPClient ftpClient = new FTPClient();
	FileInputStream fis = null;

	try {
	    ftpClient.connect(FTP_IP);
	    ftpClient.login(FTP_USERNAME, FTP_PASSWORD);

	    int reply = ftpClient.getReplyCode();
	    System.out.println(reply);
	    if (!FTPReply.isPositiveCompletion(reply)) {
		ftpClient.disconnect();
		return;
	    }

	    File srcFile = new File(UPLOAD_FILE_PATH);
	    fis = new FileInputStream(srcFile);
	    // 设置上传目录
	    ftpClient.changeWorkingDirectory("/admin/doc");
	    ftpClient.setBufferSize(1024);
	    ftpClient.setControlEncoding("GBK");
	    // 设置文件类型(二进制)
	    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
	    ftpClient.storeFile(UPLOAD_FILE_NAME, fis);
	    ftpClient.logout();
	} catch (IOException e) {
	    e.printStackTrace();
	    throw new RuntimeException("FTP客户端出错!", e);
	} finally {
	    IOUtils.closeQuietly(fis);
	    try {
		ftpClient.disconnect();
	    } catch (IOException e) {
		e.printStackTrace();
		throw new RuntimeException("关闭FTP连接发生异常!", e);
	    }
	}
    }

    /**
     * FTP下载单个文件测试
     */
    public void download() {
	FTPClient ftpClient = new FTPClient();
	FileOutputStream fos = null;

	try {
	    ftpClient.connect(FTP_IP);
	    ftpClient.login(FTP_USERNAME, FTP_PASSWORD);

	    fos = new FileOutputStream(DOWNLOAD_FILE_PATH);

	    ftpClient.setBufferSize(1024);
	    // 设置文件类型(二进制)
	    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
	    ftpClient.retrieveFile(DOWNLOAD_FILE_NAME, fos);
	} catch (IOException e) {
	    e.printStackTrace();
	    throw new RuntimeException("FTP客户端出错!", e);
	} finally {
	    IOUtils.closeQuietly(fos);
	    try {
		ftpClient.disconnect();
	    } catch (IOException e) {
		e.printStackTrace();
		throw new RuntimeException("关闭FTP连接发生异常!", e);
	    }
	}
    }

    /**
     * 向FTP服务器上的codeList.xml中写一个字符串
     * 
     */
    public void write() {
	try {
	    FTPClient ftpClient = getFtpClient();
	    InputStream in = new ByteArrayInputStream(
		    "append str+汉字追加".getBytes("UTF-8"));
	    boolean result = ftpClient.storeFile("codeList.xml", in);
	    System.out.println("向codeList.xml中写入字符串成功?" + result);
	    in.close();
	    ftpClient.logout();
	} catch (Exception e) {
	    e.printStackTrace();
	}
    }

    private FTPClient getFtpClient() {
	FTPClient ftpClient = new FTPClient();
	try {
	    ftpClient.connect(FTP_IP);
	    ftpClient.login(FTP_USERNAME, FTP_PASSWORD);
	    int reply = ftpClient.getReplyCode();
	    System.out.println(reply);
	    if (!FTPReply.isPositiveCompletion(reply)) {
		ftpClient.disconnect();
		return null;
	    }
	    ftpClient.setBufferSize(1024);
	    ftpClient.setControlEncoding("UTF-8");
	} catch (SocketException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}

	return ftpClient;
    }

}

 

 

 

 

 

用到的软件:

FTP服务器:Quick Easy FTP Server 4.0.0
FTP客户端:FlashFXP


Java实现FTP上传下载
 
Java实现FTP上传下载
 

 

 

 

参考:

http://lavasoft.blog.51cto.com/62575/93883/
http://blog.csdn.net/hbcui1984/article/details/2720204

--------------------------------------------------------------------------------------------------------------------------

@author Free Coding   http://ln-ydc.iteye.com