FTP server construction and learning of uploading and downloading

First, you need to set up the FTP service steps as follows:

1. Turn on the ftp service first on win7: After clicking OK here, it may take a while, and sometimes the system will prompt to restart after completion

2. Open Computer -- "Management --" Here we can see the service we just added (IIS)

3. Create an ftp site

4. After clicking Add FTP site, it is necessary to continue adding the necessary site information, here is: site name and the specified directory (folder) of the site after logging in to ftp, click Next

5. Just write your own IP for the address of the site. Port 21 is the default port of ftp. (The port is optional, try not to use the port number that is already occupied)

6. Fill in the login user here. Basically, it is the user on this computer. Anonymous is the Anonymous password. As long as it is in the format of the email address, you can log in. For example: "IEUser@", Baidu says you don't need to write it blank Yes, I haven't tried it, sometimes I can;

Access rights As the name suggests, for users on the computer

If you do n't say anything about reading and writing, beatclick Finish, and an FTP site is successfully established.

7. This is what it looks like after it is established. Right-click on the site you created and start it. (This is the site I built before)

8. User, the user can just use the current windows user of the system, or you can create one yourself. The specific method is User-->Right click to select a new user, and fill in your user name and password.

10. Such an ftp site has been created, you can enter in the browser or on the window: ftp://192.168.1.151:21 and press Enter to see your designated space. If there is a username and password, then Just enter the user password and click OK. We can also set permissions on the ftp site, for example: Click in to set the corresponding permissions

11. In this way, the ftp site is completed. In the first method, we may also need to open the service from time to time. We should also pay attention to whether the port number we are currently using is already occupied. If it is already occupied, we need to modify the port number. The method may require us to re-create an ftp site (you can leave a comment if you have any better suggestions). If it is your own computer, the service can be set to manual. If it is a company, then change it according to the corresponding needs. .

The second method is to be introduced below. The second method is relatively simple. It is to use the ready-made FileZillaServer software. This software is small and practical, with a total of 2M. Just go to Baidu to search, download, install and open it. Basically just use the default settings.

12. This interface is opened after opening. We use the local address. The default port is 14147. In fact, it is still 21. If you want to modify the port, when you log in to ftp for the first time, add the colon after the change you want to modify. The port number is fine, --> click connect

13. Add a login user in Edit-->Users, and give the user name, corresponding address, and corresponding permissions here

add password

14. Click OK, such an ftp service is also built, this can be more intuitive to see the log, in addition, you can also modify the log settings in Edit-->Settings, well, this is the second method, the second This method seems to be easier. The above are the two methods to build ftp service on win7. I am a novice, please give me more advice!

 

Some people will say why the files I upload or download are less than 1kb, or, the files I uploaded are damaged and cannot be opened, txt can still be opened, but others are not easy to say, or the uploaded pictures, There is a section of data loss at the end. To be honest, I also had this problem. Finally, I found that it was a version problem of the jar package. The ftp upload is this jar package of apache to add the jar package: commons-net-3.3.jar, this jar The version of the package is 3.3, and the transmission is no problem, but if you use commons-net-3.0.jar, there will be a functional bug in the 3.0 version, which will cause the data tail to be lost, and its transmission can only transmit integer kb file, which means that his transmission method uses the rounding of kb, for example: 166.7kb, after the transmission, there will only be 166.0kb, correspondingly, this bug may be corrected after version 3.0.1, so the million As soon as you meet it, don't worry, just change to a jar package with a higher version. I personally tested it, and it is very easy to use!

 

The above content is transferred from: https://blog.csdn.net/XRB_666/article/details/73913586

Here is the code to learn yes by yourself:

public class FTPClientTest {

    /**
     * 创建ftp上传文件的方法
     * @param url 主机地址
     * @param port 主机端口号
     * @param uname 用户名
     * @param pword 密码
     * @param pathname FTP服务器保存文件的路径
     * @param filename 文件名称
     * @param inputStream 输入流
     * @return
     */
    public static boolean uploadFile(String url,int port,String uname,String pword,String pathname,String filename
            ,InputStream inputStream){
        boolean isSuccess = false;
        FTPClient ftpClient = new FTPClient();  //创建FTPClient对象
        try {
            ftpClient.connect(url, port);  //通过ftpClient对象以及url和端口号创建连接
            ftpClient.login(uname, pword);  //进行登录
            int reply = ftpClient.getReplyCode();  //看返回的值是不是230,如果是,表示登陆成功
            
            //System.out.println("返回值是什么呢??----"+reply);
            /*if(!FTPReply.isNegativePermanent(reply)){  //满足这个条件则代表连接失败
                ftpClient.disconnect();
                return isSuccess;
            }*/
            boolean c = ftpClient.changeWorkingDirectory(pathname);  //设置工作空间
            
            //System.out.println(c);
            boolean b = ftpClient.storeFile(filename, inputStream);  //上传文件
            
            //System.out.println("上传成功了吗????----"+b);
            inputStream.close();  //关闭输入流
            ftpClient.logout();  //退出登录
            isSuccess = true;  
        } catch (SocketException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        return isSuccess;
    }
    
    /**
     * 创建ftp下载文件的方法
     * @param url ftp服务地址
     * @param username  ftp用户名
     * @param password  ftp用户密码  
     * @param filepath  ftp下载的文件名
     * @param remotepath  ftp文件的相对路径
     * @param port  ftp服务器的端口号
     * @param localpath  下载文件的保存位置
     * @return
     */
    public static boolean downFile(String url,String username,String password,String filename,String remotepath,
            int port,String localpath){
        boolean success = false;
        FTPClient ftp = new FTPClient();  //创建FTPClient对象
        try {
            ftp.connect(url, port);  //创建链接
            ftp.login(username, password);  //登录FTP服务
            int code = ftp.getReplyCode();  //获取返回的reply值   230代表连接成功
            ftp.changeWorkingDirectory(remotepath);  //转移到ftp服务器目录
            FTPFile[] files = ftp.listFiles();  //通过listFiles()获取所有的文件名
            for (FTPFile file : files) {
                if(file.getName().equals(filename)){  //找到相匹配的文件名进行下载
                    File localFile = new File(localpath+"/"+filename);
                    
                    OutputStream os = new FileOutputStream(localFile);
                    boolean b = ftp.retrieveFile(filename, os);  
                    System.out.println("下载成功了吗???---"+b);
                    success = true;
                }
            }
            
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return success;
    }
    
    public static void main(String [] args){
        try {
            FileInputStream fis = new FileInputStream(new File("D:/qcq公司资料/test.txt"));
            //boolean flag = uploadFile("192.168.2.90", 21, "qcq", "qcq", "test", "test1", fis);  //验证上传文件
            //System.out.println("----------"+flag);
            
            boolean b = downFile("192.168.2.90", "qcq", "qcq", "test1", "/test", 21, " d:/qcq company information " );   // Verify the downloaded file 
            System. out .println( " ---------- " + b);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
    }
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325883925&siteId=291194637