FtpService.java 4.83 KB
Newer Older
eddie.woo's avatar
eddie.woo committed
1 2 3 4
package pwc.taxtech.atms.common.ftp;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
5 6 7 8
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
9 10
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
11
import pwc.taxtech.atms.exception.ServiceException;
eddie.woo's avatar
eddie.woo committed
12

13 14
import java.io.*;

15 16 17 18 19
@Service
public class FtpService {
    static String SYMBOL = "/";
    @Autowired
    FtpConfig config;
eddie.woo's avatar
eddie.woo committed
20

21
    String ftpRootPath;
eddie.woo's avatar
eddie.woo committed
22

23 24
    private String requestPrefix = "http://";

eddie.woo's avatar
eddie.woo committed
25 26
    public void upload(String filePath, String fileName, InputStream inputStream) throws Exception {
        String upPath;
27 28
        try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config)) {

frank.xa.zhang's avatar
frank.xa.zhang committed
29 30 31 32 33 34 35 36
            if (StringUtils.isBlank(filePath)) {
                upPath = ftpRootPath;
            } else {
                upPath = filePath;
            }
            if (!StringUtils.endsWith(upPath, SYMBOL)) {
                upPath = upPath + SYMBOL;
            }
37 38
            if (!isExist(upPath, wrapFtpClient.ftpClient)) {
                mkDir(upPath, wrapFtpClient.ftpClient);
frank.xa.zhang's avatar
frank.xa.zhang committed
39
            }
40
            wrapFtpClient.ftpClient.storeFile(upPath + fileName, inputStream);
eddie.woo's avatar
eddie.woo committed
41 42 43 44 45
        }
    }

    /**
     * 下载
46
     *
eddie.woo's avatar
eddie.woo committed
47 48 49 50 51
     * @param filePath 相对路径 + 文件名
     * @return InputStream
     * @throws Exception Exception
     */
    public InputStream download(String filePath) throws Exception {
52 53
        try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config); OutputStream out = new ByteArrayOutputStream();) {
            getRootPath(wrapFtpClient);
54 55 56
            if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty");
            wrapFtpClient.ftpClient.changeWorkingDirectory(ftpRootPath);
            InputStream in = wrapFtpClient.ftpClient.retrieveFileStream(filePath);
57 58 59 60 61 62
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = in.read(bytes)) != -1) {
                out.write(bytes, 0, len);
            }
            return new ByteArrayInputStream(((ByteArrayOutputStream) out).toByteArray());
frank.xa.zhang's avatar
frank.xa.zhang committed
63
        }
64

eddie.woo's avatar
eddie.woo committed
65 66 67 68 69 70 71 72 73 74
    }

    private void mkDir(String path, FTPClient ftpClient) throws IOException {
        if (StringUtils.isNotBlank(path)) {
            ftpClient.changeWorkingDirectory(ftpRootPath);
            String[] paths = path.split("/");
            for (String p : paths) {
                if (StringUtils.isNotBlank(p) && !StringUtils.equals(p, ".")) {
                    if (!ftpClient.changeWorkingDirectory(p)) {
                        ftpClient.makeDirectory(p);
75
                        if (!ftpClient.changeWorkingDirectory(p)) {
eddie.woo's avatar
eddie.woo committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
                            throw new IOException("changeWorkingDirectory error.");
                        }
                    }
                }
            }
        }
    }

    private boolean isExist(String path, FTPClient ftpClient) throws IOException {
        String pwd = ftpClient.printWorkingDirectory();
        try {
            return ftpClient.changeWorkingDirectory(path);
        } finally {
            ftpClient.changeWorkingDirectory(pwd);
        }
    }

93 94 95 96 97 98 99

    /**
     * 删除ftp文件
     *
     * @param filePath
     */
    public void delete(String filePath) throws Exception {
100 101
        try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config);) {
            getRootPath(wrapFtpClient);
102 103 104 105
            if (StringUtils.isBlank(filePath)) {
                return;
            }
            if (!isExist(filePath, wrapFtpClient.ftpClient)) {
frank.xa.zhang's avatar
frank.xa.zhang committed
106 107
                return;
            } else {
108
                wrapFtpClient.ftpClient.deleteFile(filePath);
frank.xa.zhang's avatar
frank.xa.zhang committed
109
            }
110 111
        }
    }
frank.xa.zhang's avatar
frank.xa.zhang committed
112

113 114 115
    private void getRootPath(WrapFtpClient wrapFtpClient) throws Exception {
        if (ftpRootPath == null || ftpRootPath.isEmpty())
            ftpRootPath = wrapFtpClient.ftpClient.printWorkingDirectory();
frank.xa.zhang's avatar
frank.xa.zhang committed
116
    }
117

118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    public InputStream getFtpFileWithStaticUrl(String fileUrl) throws IOException {
        if (StringUtils.isBlank(fileUrl)) {
            return null;
        }

        if (StringUtils.isNotBlank(config.getFtpHost())) {
            CloseableHttpClient client = HttpClients.createDefault();
            HttpGet httpGet = new HttpGet(requestPrefix + config.getFtpHost() + "/" + fileUrl);
            CloseableHttpResponse response = null;
            try {
                response = client.execute(httpGet);
                if (response.getStatusLine().getStatusCode()==200) {
                    return response.getEntity().getContent();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                //client.close();
                //response.close();
            }
        }
        return null;
    }

eddie.woo's avatar
eddie.woo committed
142
}