package pwc.taxtech.atms.common.ftp; import org.apache.commons.lang3.StringUtils; import org.apache.commons.net.ftp.FTPClient; 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; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import pwc.taxtech.atms.exception.ServiceException; import java.io.*; @Service public class FtpService { static String SYMBOL = "/"; @Autowired FtpConfig config; String ftpRootPath; private String requestPrefix = "http://"; public void upload(String filePath, String fileName, InputStream inputStream) throws Exception { String upPath; try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config)) { if (StringUtils.isBlank(filePath)) { upPath = ftpRootPath; } else { upPath = filePath; } if (!StringUtils.endsWith(upPath, SYMBOL)) { upPath = upPath + SYMBOL; } if (!isExist(upPath, wrapFtpClient.ftpClient)) { mkDir(upPath, wrapFtpClient.ftpClient); } wrapFtpClient.ftpClient.storeFile(upPath + fileName, inputStream); } } /** * 下载 * * @param filePath 相对路径 + 文件名 * @return InputStream * @throws Exception Exception */ public InputStream download(String filePath) throws Exception { try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config); OutputStream out = new ByteArrayOutputStream();) { getRootPath(wrapFtpClient); if (StringUtils.isBlank(filePath)) throw new ServiceException("file path should not empty"); wrapFtpClient.ftpClient.changeWorkingDirectory(ftpRootPath); InputStream in = wrapFtpClient.ftpClient.retrieveFileStream(filePath); 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()); } } 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); if (!ftpClient.changeWorkingDirectory(p)) { 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); } } /** * 删除ftp文件 * * @param filePath */ public void delete(String filePath) throws Exception { try (WrapFtpClient wrapFtpClient = new WrapFtpClient(config);) { getRootPath(wrapFtpClient); if (StringUtils.isBlank(filePath)) { return; } if (!isExist(filePath, wrapFtpClient.ftpClient)) { return; } else { wrapFtpClient.ftpClient.deleteFile(filePath); } } } private void getRootPath(WrapFtpClient wrapFtpClient) throws Exception { if (ftpRootPath == null || ftpRootPath.isEmpty()) ftpRootPath = wrapFtpClient.ftpClient.printWorkingDirectory(); } 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() + ":1886/" + 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; } }