第2篇 将本地文件上传到远程FTP服务器和从远程FTP服务器上下载文件到本地
1、首先需要在本地设置一个FTP站点
(1)我的电脑——》管理——》本地用户和组——》用户——》新增用户,即设置FTP登录的用户名和密码
(2)其次是在C盘新建文件夹“FTP上传”和“FTP下载”两个文件夹!并在每个文件夹里放不同的文件,以便区分!
(3)之后是安装IIS组件,开始——》控制面板——》程序——》程序和功能——》打开或关闭windows功能
(4)最后就是配置FTP服务器,创建上传和下载服务!创建上传服务器:右键网站->选择添加FTP站点->描述可以根据自己的需要填写->地址一般都是自己的IP地址,端口默认使用21->物理路径指向“C:\FTP上传”->访问权限要钩上“读取”和“写入”->点击完成就把上传的服务创建好了!创建下载服务器:因为21号端口已经被占用所以我们就用2121端口!它的物理路径指向“C:\FTP下载”!只有读取权限!!具体的步骤就看图吧。
一、上传服务器图解
二、下载服务器图解
(5)最后就可以测试刚才建立的ftp服务器是否建立成功了。在浏览器上输入以下地址ftp://设置站点时ip(在此为本地ip) 即可打开具有上传功能的FTP页面,输入ftp://设置站点时ip(在此为本地ip):2121即可打开只有下载功能的页面了!当然,登录之前还需要你输入开始建立的那个账号及密码:用户名为:。密码为:
2、相关代码
package com.jd.jr.trade.xjk.test; import sun.net.TelnetInputStream; import sun.net.TelnetOutputStream; import sun.net.ftp.FtpClient; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; /** * Created by wangxingjie on 2016/12/2. */ public class TestFileFTP { /** * 本地文件名 */ private String localfilename; /** * 远程文件名 */ private String remotefilename; /** * FTP客户端 */ private FtpClient ftpClient; /** * 服务器连接 * @param ip 服务器IP * @param port 服务器端口 * @param user 用户名 * @param password 密码 * @param path 服务器路径 * @author 周玲斌 * @date 2012-7-11 */ public void connectServer(String ip, int port, String user, String password, String path) { try { /* ******连接服务器的两种方法*******/ //第一种方法 ftpClient = new FtpClient(); ftpClient.openServer(ip, port); //第二种方法 // ftpClient = new FtpClient(ip); ftpClient.login(user, password); // 设置成2进制传输 ftpClient.binary(); System.out.println("login success!"); if (path.length() != 0){ //把远程系统上的目录切换到参数path所指定的目录 ftpClient.cd(path); } ftpClient.binary(); } catch (IOException ex) { ex.printStackTrace(); throw new RuntimeException(ex); } } /** * 关闭连接 * @author 周玲斌 * @date 2012-7-11 */ 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); } } /** * 上传文件 * @param localFile 本地文件 * @param remoteFile 远程文件 * @author 周玲斌 * @date 2012-7-11 */ 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(); } } } } /** * 下载文件 * @param remoteFile 远程文件路径(服务器端) * @param localFile 本地文件路径(客户端) * @author 周玲斌 * @date 2012-7-11 */ public void download(String remoteFile, String localFile) { TelnetInputStream is = null; FileOutputStream os = null; try { //获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。 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"}; TestFileFTP fu = new TestFileFTP(); /* * 使用默认的端口号、用户名、密码以及根目录连接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(); } }