沈斌
2018-01-30 b4cabe178f1a36433270c79048beecfab0edc487
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package com.moral.controller;
 
import org.apache.commons.net.ftp.FTPClient;
import org.springframework.web.bind.annotation.*;
 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
@RestController
@RequestMapping("upgrade")
@CrossOrigin(origins = "*", maxAge = 3600)
public class UpgradeController {
 
    private FTPClient ftpClient;
    private String fileName = "Version.xml";
    private String ip = "121.40.92.176";
    private String userName = "ftp_user";
    private String userPwd = "qwer1234";
    private int port = 21;
    private String path = "/";
 
    @RequestMapping(value = "/index", method = RequestMethod.GET)
    public String index() throws IOException {
        ftpClient = new FTPClient();
        try {
            ftpClient.connect(ip, port);
            ftpClient.login(userName, userPwd);
            if (path != null && path.length() > 0) {
                ftpClient.changeWorkingDirectory(path);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            ftpClient.logout();
            ftpClient.disconnect();
        }
        return readFile();
    }
 
    public String readFile() {
        InputStream ins = null;
        StringBuilder builder = null;
        try {
            ins = ftpClient.retrieveFileStream(fileName);
            BufferedReader reader = new BufferedReader(new InputStreamReader(ins, "UTF-8"));
            String line;
            builder = new StringBuilder(150);
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
            reader.close();
            if (ins != null) {
                ins.close();
            }
            ftpClient.getReply();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return builder.toString();
    }
}