实现FTP文件上传与下载可以通过以下两种种方式实现,分别为:1、通过JDK自带的API实现;2、通过Apache提供的API是实现。
第一种方式
- package com.cloudpower.util;
-
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
-
- import sun.net.TelnetInputStream;
- import sun.net.TelnetOutputStream;
- import sun.net.ftp.FtpClient;
-
- public class Ftp {
-
- private String localfilename;
-
- private String remotefilename;
-
- private FtpClient ftpClient;
-
-
- public void connectServer(String ip, int port, String user,
- String password, String path) {
- try {
-
-
-
- ftpClient = new FtpClient(ip);
-
- ftpClient.login(user, password);
-
- ftpClient.binary();
- System.out.println("login success!");
- if (path.length() != 0){
-
- ftpClient.cd(path);
- }
- ftpClient.binary();
- } catch (IOException ex) {
- ex.printStackTrace();
- throw new RuntimeException(ex);
- }
- }
-
- public void closeConnect() {
- try {
- ftpClient.closeServer();
- System.out.println("disconnect success");
- } catch (IOException ex) {
- System.out.println("not disconnect");
- ex.printStackTrace();
- throw new RuntimeException(ex);
- }
- }
-
- public void upload(String localFile, String remoteFile) {
- this.localfilename = localFile;
- this.remotefilename = remoteFile;
- TelnetOutputStream os = null;
- FileInputStream is = null;
- try {
-
- os = ftpClient.put(this.remotefilename);
-
- File file_in = new File(this.localfilename);
- is = new FileInputStream(file_in);
-
- byte[] bytes = new byte[1024];
- int c;
- while ((c = is.read(bytes)) != -1) {
- os.write(bytes, 0, c);
- }
- System.out.println("upload success");
- } catch (IOException ex) {
- System.out.println("not upload");
- ex.printStackTrace();
- throw new RuntimeException(ex);
- } finally{
- try {
- if(is != null){
- is.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(os != null){
- os.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
-
- public void download(String remoteFile, String localFile) {
- TelnetInputStream is = null;
- FileOutputStream os = null;
- try {
-
- is = ftpClient.get(remoteFile);
- File file_in = new File(localFile);
- os = new FileOutputStream(file_in);
- byte[] bytes = new byte[1024];
- int c;
- while ((c = is.read(bytes)) != -1) {
- os.write(bytes, 0, c);
- }
- System.out.println("download success");
- } catch (IOException ex) {
- System.out.println("not download");
- ex.printStackTrace();
- throw new RuntimeException(ex);
- } finally{
- try {
- if(is != null){
- is.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- try {
- if(os != null){
- os.close();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
-
- public static void main(String agrs[]) {
-
- String filepath[] = { "/temp/aa.txt", "/temp/regist.log"};
- String localfilepath[] = { "C:\\tmp\\1.txt","C:\\tmp\\2.log"};
-
- Ftp fu = new Ftp();
-
- fu.connectServer("127.0.0.1", 22, "anonymous", "IEUser@", "/temp");
-
-
- for (int i = 0; i < filepath.length; i++) {
- fu.download(filepath[i], localfilepath[i]);
- }
-
- String localfile = "E:\\号码.txt";
- String remotefile = "/temp/哈哈.txt";
-
- fu.upload(localfile, remotefile);
- fu.closeConnect();
- }
- }
这种方式没啥可说的,比较简单,也不存在中文乱码的问题。貌似有个缺陷,不能操作大文件,有兴趣的朋友可以试试。
第二种方式
- public class FtpApche {
- private static FTPClient ftpClient = new FTPClient();
- private static String encoding = System.getProperty("file.encoding");
-
- public static boolean uploadFile(String url, int port, String username,
- String password, String path, String filename, InputStream input) {
- boolean result = false;
-
- try {
- int reply;
-
- ftpClient.connect(url);
-
-
- ftpClient.login(username, password);
- ftpClient.setControlEncoding(encoding);
-
- reply = ftpClient.getReplyCode();
- if (!FTPReply.isPositiveCompletion(reply)) {
- System.out.println("连接失败");
- ftpClient.disconnect();
- return result;
- }
-
-
- boolean change = ftpClient.changeWorkingDirectory(path);
- ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
- if (change) {
- result = ftpClient.storeFile(new String(filename.getBytes(encoding),"iso-8859-1"), input);
- if (result) {
- System.out.println("上传成功!");
- }
- }
- input.close();
- ftpClient.logout();
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ftpClient.isConnected()) {
- try {
- ftpClient.disconnect();
- } catch (IOException ioe) {
- }
- }
- }
- return result;
- }
-
-
- public void testUpLoadFromDisk() {
- try {
- FileInputStream in = new FileInputStream(new File("E:/号码.txt"));
- boolean flag = uploadFile("127.0.0.1", 21, "zlb","123", "/", "哈哈.txt", in);
- System.out.println(flag);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- }
-
-
-
- public static boolean downFile(String url, int port, String username,
- String password, String remotePath, String fileName,
- String localPath) {
- boolean result = false;
- try {
- int reply;
- ftpClient.setControlEncoding(encoding);
-
-
-
- ftpClient.connect(url, port);
-
- ftpClient.login(username, password);
-
- ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
-
- reply = ftpClient.getReplyCode();
-
- if (!FTPReply.isPositiveCompletion(reply)) {
- ftpClient.disconnect();
- System.err.println("FTP server refused connection.");
- return result;
- }
-
- ftpClient.changeWorkingDirectory(new String(remotePath.getBytes(encoding),"iso-8859-1"));
-
- FTPFile[] fs = ftpClient.listFiles();
- for (FTPFile ff : fs) {
- if (ff.getName().equals(fileName)) {
- File localFile = new File(localPath + "/" + ff.getName());
- OutputStream is = new FileOutputStream(localFile);
- ftpClient.retrieveFile(ff.getName(), is);
- is.close();
- }
- }
-
- ftpClient.logout();
- result = true;
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (ftpClient.isConnected()) {
- try {
- ftpClient.disconnect();
- } catch (IOException ioe) {
- }
- }
- }
- return result;
- }
-
-
- public void testDownFile() {
- try {
- boolean flag = downFile("127.0.0.1", 21, "zlb",
- "123", "/", "哈哈.txt", "D:/");
- System.out.println(flag);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
-
- public static void main(String[] args) {
- FtpApche fa = new FtpApche();
- fa.testDownFile();
- }
- }
这 种方式的话需要注意中文乱码问题啦,如果你设置不恰当,有可能上传的文件名会为乱码,有的时候根本就上传不上去,当然,也不会跟你提示,因为原本就没异常。
首先我们来说说为什么要进行转码:
因为在FTP协议里面,规定文件名编码为iso-8859-1,所以目录名或文件名需要转码。
接下来的问题是,我们应该将什么编码转换为此格式。因此,就有了第二种解决方案——把 GBK格式的转换为ISO-8859-1格式。而且,有的人还说,必须得这么转。其实,之所以他们能这么说,我觉得完全是巧合。它的真正原理是,既然FTP协议规定的编码格式是“ISO-8859-1”,那么我们确实得将格式转换一下,然后等服务器收到文件时再自动转换为系统自带的编码格式,因此,关键不是规定为什么格式,而是取决于FTP服务器的编码格式。因此,如果FTP系统的编码格式为“GBK”时,第二种方式肯定会成功;但是,如果系统的编码格式为“UTF-8”时,那就会仍然出现乱码啦。所以,我们只能通过代码先获取系统的编码格式,然后通过此编码格式转换为ISO-8859-1的编码格 式。获取方式如下:
private static String encoding = System.getProperty("file.encoding");
FTP文件上传与下载
原文:http://www.cnblogs.com/qingyuuu/p/4561874.html