用win8电脑写java的ftp程序遇到的一些问题

1.搭建ftp环境遇到的问题:

刚开始在网上搜到一些方法,但是由于是win8的电脑,并没有看到预想中的sII网络管理和用户分组管理,后来经过搜索,在 控制面板-所有控制面板项-程序和功能-启用或关闭功能

中找到.Net Framwork3.5和勾选上了以下选项,解决了IIS问题。

 

 

 

 

 

 

 

 

2.打开IIS管理器后,然后根据网上的方法建立站点,将自动启动站点,然后要添加FTP发布,ip地址选择下拉框中的ip,默认端口就行了,按照下面方式选择。

 

3.添加FTP授权规则,可以指定现有用户,也可以先去添加用户

4.打开eclipse或者eclipse,导入jar包commons-net-ftp-2.0.jar

  1 package com.oyz.util;
  2 
  3 import java.io.File;
  4 import java.io.FileOutputStream;
  5 import java.io.IOException;
  6 import java.io.InputStream;
  7 import java.io.OutputStream;
  8 //import java.io.ByteArrayInputStream;
  9 //import java.io.UnsupportedEncodingException;
 10 
 11 import org.apache.commons.net.ftp.FTPClient;
 12 import org.apache.commons.net.ftp.FTPFile;
 13 import org.apache.commons.net.ftp.FTPReply;
 14 /**
 15  * FTP文件上传下载工具类
 16  * 注意:若文件路径不存在,则会创建路径
 17  * @author oyz
 18  *
 19  */
 20 public class FTP {
 21     /**
 22      * FTP公共信息
 23      * @author lenovo CHN
 24      *
 25      */
 26     public class FTPInform{
 27         public String url;
 28         public  int port; 
 29         public String username;
 30         public String password;
 31     }
 32     
 33     private FTPInform ftpInfo;
 34     /**
 35      * 构造函数
 36      * @param ftpInfo ftp信息头
 37      */
 38     public FTP(FTPInform ftpInfo) {
 39         setFtpInfo(ftpInfo);
 40     }
 41     /**
 42      * 构造函数
 43      * @param url FTP服务器ip
 44      * @param port FTP服务器端口
 45      * @param username FTP登录账号
 46      * @param password FTP登录密码
 47      */
 48     public FTP(String url,int port,String username,String password) {
 49         FTPInform ftpInfo=new FTPInform();
 50         ftpInfo.url=url;
 51         ftpInfo.port=port;
 52         ftpInfo.username=username;
 53         ftpInfo.password=password;
 54         setFtpInfo(ftpInfo);
 55     }
 56 
 57     public FTPInform getFtpInfo() {
 58         return ftpInfo;
 59     }
 60     public void setFtpInfo(FTPInform ftpInfo) {
 61         this.ftpInfo = ftpInfo;
 62     }
 63     
 64     public boolean downFile(String remotePath,String fileName,String localPath){
 65         FTPInform ftpInfo0=getFtpInfo();
 66         return downFile(ftpInfo0.url,ftpInfo0.port,ftpInfo0.username,ftpInfo0.password,remotePath,fileName,localPath);
 67     }
 68     public boolean uploadFile(String remotePath,String fileName,InputStream in){
 69         FTPInform ftpInfo0=getFtpInfo();
 70         return uploadFile(ftpInfo0.url,ftpInfo0.port,ftpInfo0.username,ftpInfo0.password,remotePath,fileName,in);
 71     }
 72     /**
 73     * Description: 从FTP服务器下载文件
 74     * @Version1.0
 75     * @param url FTP服务器ip
 76     * @param port FTP服务器端口
 77     * @param username FTP登录账号
 78     * @param password FTP登录密码
 79     * @param remotePath FTP服务器上的相对路径
 80     * @param fileName 要下载的文件名
 81     * @param localPath 下载后保存到本地的路径
 82     * @return
 83     */ 
 84 
 85     public static boolean downFile(String url, int port,String username, String password, String remotePath,String fileName,String localPath) { 
 86         boolean success = false; 
 87         FTPClient ftp = new FTPClient(); 
 88         File f=new File(localPath);
 89         if((!f.exists())&&localPath.indexOf('.')<0){
 90             f.mkdir();
 91         }
 92         try { 
 93             int reply; 
 94             ftp.connect(url, port); 
 95             //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 
 96             ftp.login(username, password);//登录 
 97             reply = ftp.getReplyCode(); 
 98             if (!FTPReply.isPositiveCompletion(reply)) { 
 99                 ftp.disconnect(); 
100                 return success; 
101             } 
102             ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录 
103             FTPFile[] fs = ftp.listFiles(); 
104             for(FTPFile ff:fs){ 
105                 if(ff.getName().equals(fileName)){ 
106                     File localFile = new File(localPath+"/"+ff.getName()); 
107                      
108                     OutputStream is = new FileOutputStream(localFile);  
109                     ftp.retrieveFile(ff.getName(), is); 
110                     is.close(); 
111                 } 
112             } 
113              
114             ftp.logout(); 
115             success = true; 
116         } catch (IOException e) { 
117             e.printStackTrace(); 
118         } finally { 
119             if (ftp.isConnected()) { 
120                 try { 
121                     ftp.disconnect(); 
122                 } catch (IOException ioe) { 
123                     ioe.printStackTrace();
124                 } 
125             } 
126         } 
127         return success; 
128     }
129     /**
130     * Description: 向FTP服务器上传文件
131     * @Version1.0(cuihongbao@d-heaven.com)创建
132     * @param url FTP服务器hostname
133     * @param port FTP服务器端口
134     * @param username FTP登录账号
135     * @param password FTP登录密码
136     * @param path FTP服务器保存目录
137     * @param filename 上传到FTP服务器上的文件名
138     * @param input 输入流
139     * @return 成功返回true,否则返回false
140     */ 
141     public static boolean uploadFile(String url,int port,String username, String password, String path, String filename, InputStream input) { 
142         boolean success = false; 
143         FTPClient ftp = new FTPClient(); 
144         try { 
145             int reply; 
146             ftp.connect(url, port);//连接FTP服务器 
147             //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器 
148             ftp.login(username, password);//登录 
149             reply = ftp.getReplyCode(); 
150             if (!FTPReply.isPositiveCompletion(reply)) { 
151                 ftp.disconnect(); 
152                 return success; 
153             } 
154             boolean k=ftp.changeWorkingDirectory(path); 
155             if(!k){
156                 ftp.makeDirectory(path);
157                 k=ftp.changeWorkingDirectory(path);
158             }
159             ftp.storeFile(filename, input);          
160              
161             input.close(); 
162             ftp.logout(); 
163             success = true; 
164         } catch (IOException e) { 
165             e.printStackTrace(); 
166         }catch (Exception e) {
167             e.printStackTrace(); 
168         } 
169         finally { 
170             if (ftp.isConnected()) { 
171                 try { 
172                     ftp.disconnect(); 
173                 } catch (IOException ioe) { 
174                 } 
175             } 
176         } 
177         return success; 
178     }
179 //    public static void main(String[] args) {
180 //        FTP ftp=new FTP("*.*.*.*", 21, "name", "pwd");
181 //        
182         
183         ftp.downFile("./zs","test.txt","F://z");
184 //
185 //        try {
186 //            InputStream input = new ByteArrayInputStream("test ftp".getBytes("utf-8"));
187 //            boolean flag = ftp.uploadFile("./zz", "tes2.txt", input);
188 //            System.out.println(flag);
189 //        } catch (UnsupportedEncodingException e) {
190 //            e.printStackTrace();
191 //        }
192 //    }
193 }

 

 然后运行调试一下就ok了,但是如果要让其他电脑使用ftp,则可能需要关闭防火墙才行。

转载于:https://www.cnblogs.com/dashengyibo/p/4680849.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值