HttpClientUtils.java
package com.rongsoft.common.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.compress.archivers.ArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveInputStream;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.config.ConnectionConfig;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeader;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.net.ssl.*;
import java.io.*;
import java.nio.charset.Charset;
import java.security.*;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
public class HttpClientUtils {
// 请求超时时间
private static int connectionRequestTimeout = 90000;
// 连接超时时间,默认10秒
private static int socketTimeout = 90000;
// 传输超时时间,默认30秒
private static int connectTimeout = 90000;
private static final Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
// 这个HOST验证
private static HostnameVerifier hostnameVerifier = new HostnameVerifier() {
@Override
public boolean verify(String arg0, SSLSession arg1) {
return true;
}
};
private static TrustManager truseAllManager = new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
// TODO Auto-generated method stub
}
@Override
public X509Certificate[] getAcceptedIssuers() {
// TODO Auto-generated method stub
return null;
}
};
public static CloseableHttpClient getCertHttpClient(String apiclientCertPath, String password) throws Exception {
CloseableHttpClient httpClient;
try {
// 指定读取证书格式为PKCS12
KeyStore keyStore = KeyStore.getInstance("PKCS12");
// 读取本机存放的PKCS12证书文件
InputStream instream = Thread.currentThread().getClass().getResourceAsStream(apiclientCertPath);
// 指定PKCS12的密码(商户ID)
keyStore.load(instream, password.toCharArray());
SSLContext sslcontext = SSLContexts.custom().loadKeyMaterial(keyStore, password.toCharArray()).build();
// Allow TLSv1 protocol only
SSLConnectionSocketFactory sslcsf = new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1"},
null, hostnameVerifier);
// 构建客户端
httpClient = HttpClientBuilder.create().setSSLSocketFactory(sslcsf).build();
return httpClient;
} catch (Exception e) {
throw new Exception(e);
}
}
public static byte[] getFile(String downloadURL,String username, String password) throws IOException {
CloseableHttpClient httpclient = null;
InputStream in = null;
byte[] bytes;
try {
if (StringUtils.isNotBlank(username)) {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpclient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
} else {
httpclient = HttpClients.custom().build();
}
HttpGet httpget = new HttpGet(downloadURL);
httpget.setConfig(RequestConfig.custom().setConnectionRequestTimeout(60000).setConnectTimeout(60000)
.setSocketTimeout(60000).build());
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
in = entity.getContent();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] buff = new byte[100]; // buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = in.read(buff, 0, 100)) > 0) {
byteArrayOutputStream.write(buff, 0, rc);
}
bytes = byteArrayOutputStream.toByteArray();
} finally {
// 关闭低层流。
if (in != null) {
in.close();
}
if (httpclient != null) {
httpclient.close();
}
}
return bytes;
}
public static void downloadFileWithBasicAuth(String serverUrl, String username, String password, String remoteFileName, String localFileName) throws Exception {
CloseableHttpClient httpClient = null;
OutputStream out = null;
InputStream in = null;
try {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
//goHttpServer要求这么做,拼接uri
HttpGet httpGet = new HttpGet(serverUrl + remoteFileName + "?download=true");
httpGet.addHeader("fileName", remoteFileName);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
in = entity.getContent();
long length = entity.getContentLength();
// if (length <= 0) {
// System.out.println("下载文件不存在!");
// return;
// }
File file = new File(localFileName);
if (!file.exists()) {
file.createNewFile();
}
out = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int readLength = 0;
while ((readLength = in.read(buffer)) > 0) {
byte[] bytes = new byte[readLength];
System.arraycopy(buffer, 0, bytes, 0, readLength);
out.write(bytes);
}
out.flush();
} catch (Exception e) {
throw new Exception(e);
} finally {
if (null != httpClient) {
try {
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void uploadStringWithBasicAuth(String serverUrl, String username, String password, String fileName, String content) throws Exception {
HttpResponse response = null;
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
HttpEntity resEntity = null;
try {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addBinaryBody("file", content.getBytes("UTF-8"), ContentType.TEXT_PLAIN, fileName);
HttpEntity reqEntity = builder.build();
httpPost = new HttpPost(serverUrl);
httpPost.setEntity(reqEntity);
httpPost.addHeader(new BasicHeader(HttpHeaders.EXPECT, "100-continue"));
httpPost.setConfig(RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build());
response = httpClient.execute(httpPost);
String code = response.getStatusLine().getStatusCode() + "";
if ("200".equals(code)) {
} else {
// 获取响应对象
resEntity = response.getEntity();
if (resEntity != null) {
throw new Exception("文件上传失败:" + EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
}
}
} catch (Exception e) {
throw new Exception(e);
} finally {
if (null != httpPost) {
httpPost.abort();
}
if (null != httpClient) {
httpClient.getConnectionManager().shutdown();
}
if (null != resEntity) {
// 销毁
try {
EntityUtils.consume(resEntity);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void uploadFileWithBasicAuth(String serverUrl, String username, String password, String fileName) throws Exception {
HttpResponse response = null;
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
HttpEntity resEntity = null;
try {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
FileBody bin = new FileBody(new File(fileName));
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.addPart("file", bin);
HttpEntity reqEntity = builder.build();
httpPost = new HttpPost(serverUrl);
httpPost.setEntity(reqEntity);
httpPost.addHeader(new BasicHeader(HttpHeaders.EXPECT, "100-continue"));
httpPost.setConfig(RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build());
response = httpClient.execute(httpPost);
String code = response.getStatusLine().getStatusCode() + "";
if ("200".equals(code)) {
} else {
// 获取响应对象
resEntity = response.getEntity();
if (resEntity != null) {
throw new Exception("文件上传失败:" + EntityUtils.toString(resEntity, Charset.forName("UTF-8")));
}
}
} catch (Exception e) {
throw new Exception(e);
} finally {
if (null != httpPost) {
httpPost.abort();
}
if (null != httpClient) {
httpClient.getConnectionManager().shutdown();
}
if (null != resEntity) {
// 销毁
try {
EntityUtils.consume(resEntity);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static HttpResponse sendPostStrWithCert(String apiclientCertPath, String password, String targetUrl,
String str, String enCoding, String contentType) throws Exception {
HttpResponse response = null;
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
try {
httpClient = getCertHttpClient(apiclientCertPath, password);
if (null != httpClient) {
httpPost = new HttpPost(targetUrl);
httpPost.setConfig(RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build());
HttpEntity httpEntity = new StringEntity(str, enCoding);
httpPost.addHeader("Content-Type", contentType);
httpPost.setEntity(httpEntity);
response = httpClient.execute(httpPost);
}
} catch (Exception e) {
throw new Exception(e);
} finally {
if (null != httpPost) {
httpPost.abort();
}
try {
if (null != httpClient) {
httpClient.close();
}
} catch (IOException e) {
throw new Exception(e);
}
}
return response;
}
public static void main(String[] args) throws Exception {
String dir = "http://192.168.1.89:9082/-/json/20180921/pinganRecepit";
String json = HttpClientUtils.listFolder(dir, "sdfsf", "123456");
JSONObject jsonObj = JSON.parseObject(json);
JSONArray files = jsonObj.getJSONArray("files");
String filePath = files.getJSONObject(0).getString("path");
System.out.println(FilenameUtils.normalize(dir + filePath));
dir = "http://192.168.1.89:9082/-/info/20180921/pinganRecepit/20171009_0901_010_15000082040406.pdf";
json = HttpClientUtils.getFileInfo(dir, "sdfsf", "123456");
// 如果文件不存在,则返回“Not a file”
System.out.println(json);
HttpClientUtils.createFolder("http://192.168.1.89:9082/-/folder/20190110/pinganReceipt/",
"sdfsf", "123456");
}
private static CloseableHttpClient enableSSL() throws Exception {
CloseableHttpClient httpClient = null;
try {
SSLContext sslcontext = SSLContext.getInstance("TLS");
sslcontext.init(null, new TrustManager[]{truseAllManager}, null);
SSLConnectionSocketFactory sslcsf = new SSLConnectionSocketFactory(sslcontext, null, null,
hostnameVerifier);
RegistryBuilder<ConnectionSocketFactory> registryBuilder = RegistryBuilder
.<ConnectionSocketFactory>create();
Registry<ConnectionSocketFactory> registry = registryBuilder.register("https", sslcsf).build();
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(registry);
httpClient = HttpClientBuilder.create().setConnectionManager(connManager).build();
return httpClient;
} catch (Exception e) {
throw new Exception(e);
}
}
public static HttpResponse sendGet(String targetUrl, String enCoding, String contentType) throws Exception {
HttpResponse response = null;
try {
HttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(targetUrl);
httpGet.setConfig(RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build());
httpGet.addHeader("Content-Type", contentType);
response = httpClient.execute(httpGet);
} catch (Exception e) {
throw new Exception(e);
}
return response;
}
public static Map sendPostNameValuePair(String targetUrl, NameValuePair[] nameValuePairs, String enCoding, String contentType) throws Exception {
HttpResponse response = null;
CloseableHttpClient httpClient = null;
HttpPost httpPost = null;
Map ret = new HashMap();
try {
httpClient = HttpClientBuilder.create().build();
if (targetUrl.startsWith("https")) {
httpClient = enableSSL();
}
httpPost = new HttpPost(targetUrl);
httpPost.setConfig(RequestConfig.custom().setConnectionRequestTimeout(connectionRequestTimeout)
.setConnectTimeout(connectTimeout).setSocketTimeout(socketTimeout).build());
HttpEntity httpEntity = new UrlEncodedFormEntity(Arrays.asList(nameValuePairs));
httpPost.addHeader("Content-Type", contentType);
httpPost.setEntity(httpEntity);
response = httpClient.execute(httpPost);
// 调用request的abort方法
String code = response.getStatusLine().getStatusCode() + "";
String body = "";
ret.put("code", code);
ret.put("allHeaders",response.getAllHeaders());
if ("200".equals(code)) {
Optional<Header> header = Arrays.stream(response.getAllHeaders()).filter(h -> "content-disposition".equals(h.getName())).findAny();
if(header.isPresent()) {
byte[] contentBytes = EntityUtils.toByteArray(response.getEntity());
ret.put("bodyBytes", contentBytes);
}else {
body=EntityUtils.toString(response.getEntity());
ret.put("body", body);
}
} else if ("302".equals(code)) {
ret.put("location", response.getFirstHeader("Location").getValue());
}
} catch (ClientProtocolException e) {
throw new Exception(e);
} catch (IOException e) {
throw new Exception(e);
} finally {
if (null != httpPost) {
httpPost.abort();
}
if (null != httpClient) {
httpClient.getConnectionManager().shutdown();
}
}
return ret;
}
public static String uploadFileBytesWithBasicAuth(String remoteUrl,String user,String pwd,String fileName,byte [] bytes) throws Exception {
CloseableHttpClient httpClient = null;//HttpClients.createDefault();
String result = "";
try {
HttpPost httpPost = new HttpPost(remoteUrl);
httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
httpPost.addHeader(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-cn"));
httpPost.addHeader(new BasicHeader(HttpHeaders.EXPECT, "100-continue"));
httpPost.setConfig(RequestConfig.custom().setConnectionRequestTimeout(60000)
.setConnectTimeout(60000).setSocketTimeout(60000).build());
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,pwd);
provider.setCredentials(AuthScope.ANY, credentials);
Charset utf8 = Charset.forName("UTF-8");
ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(utf8).build();
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).setDefaultConnectionConfig(connectionConfig).build();
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE).setCharset(utf8);
builder.addBinaryBody("file", bytes, ContentType.MULTIPART_FORM_DATA, fileName);// 文件流
builder.addTextBody("filename", fileName);// 类似浏览器表单提交,对应input的name和value
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);// 执行提交
int status=response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
if(status!=200) {
throw new Exception("http upload error:"+status+","+result);
}
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 删除server文件
* @param remoteUrl 文件地址
* @param user 用户名(如无身份验证可空,但不能为null)
* @param pwd 密码 (如无身份验证可空,但不能为null)
* @return
*/
public static String deleteFileBytesWithBasicAuth(String remoteUrl,String user,String pwd) throws Exception {
CloseableHttpClient httpClient = null;//HttpClients.createDefault();
String result = "";
try {
HttpDelete httpDelete = new HttpDelete(remoteUrl);
httpDelete.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
httpDelete.addHeader(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-cn"));
httpDelete.addHeader(new BasicHeader(HttpHeaders.EXPECT, "100-continue"));
httpDelete.setConfig(RequestConfig.custom().setConnectionRequestTimeout(60000)
.setConnectTimeout(60000).setSocketTimeout(60000).build());
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,pwd);
provider.setCredentials(AuthScope.ANY, credentials);
Charset utf8 = Charset.forName("UTF-8");
ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(utf8).build();
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).setDefaultConnectionConfig(connectionConfig).build();
HttpResponse response = httpClient.execute(httpDelete);// 执行提交
int status=response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
if(status!=200) {
throw new Exception("http upload error:"+status+","+result);
}
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/**
* 创建文件目录
* @param remoteUrl serverurl + "-/folder/"+ folderPath(要以/结尾)
* @param user
* @param pwd
*/
public static void createFolder(String remoteUrl,String user,String pwd) throws Exception {
CloseableHttpClient httpClient = null;//HttpClients.createDefault();
String result = "";
try {
HttpGet httpget = new HttpGet(remoteUrl);
httpget.addHeader(new BasicHeader(HttpHeaders.ACCEPT_CHARSET, "utf-8"));
httpget.addHeader(new BasicHeader(HttpHeaders.ACCEPT_LANGUAGE, "zh-cn"));
httpget.setConfig(RequestConfig.custom().setConnectionRequestTimeout(60000)
.setConnectTimeout(60000).setSocketTimeout(60000).build());
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(user,pwd);
provider.setCredentials(AuthScope.ANY, credentials);
Charset utf8 = Charset.forName("UTF-8");
ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(utf8).build();
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).setDefaultConnectionConfig(connectionConfig).build();
HttpResponse response = httpClient.execute(httpget);// 执行提交
int status=response.getStatusLine().getStatusCode();
HttpEntity responseEntity = response.getEntity();
if (responseEntity != null) {
// 将响应内容转换为字符串
result = EntityUtils.toString(responseEntity, Charset.forName("UTF-8"));
}
if(status!=200) {
throw new Exception("http createFolder error:"+status+","+result);
}
} catch (Exception e) {
throw new Exception(e.getMessage());
} finally {
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static String listFolder(String serverDir, String username, String password) throws Exception {
CloseableHttpClient httpClient = null;
try {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
//goHttpServer要求这么做,拼接uri
HttpGet httpGet = new HttpGet(serverDir + "?_=" + System.currentTimeMillis());
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
String content = EntityUtils.toString(entity, "UTF-8");
return content;
} catch (Exception e) {
throw new Exception(e);
} finally {
if (null != httpClient) {
try {
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static String getFileInfo(String serverDir, String username, String password) throws Exception {
CloseableHttpClient httpClient = null;
try {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
//goHttpServer要求这么做,拼接uri
HttpGet httpGet = new HttpGet(serverDir);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
String content = EntityUtils.toString(entity, "UTF-8");
return content;
} catch (Exception e) {
throw new Exception(e);
} finally {
if (null != httpClient) {
try {
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
public static byte [] downloadZipFileByBytes(String fileName, String serverUrl, String username, String password) throws Exception {
CloseableHttpClient httpClient = null;
InputStream inputStream = null;
ZipArchiveInputStream zipArchiveInputStream = null;
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
try {
CredentialsProvider provider = new BasicCredentialsProvider();
UsernamePasswordCredentials credentials = new UsernamePasswordCredentials(username, password);
provider.setCredentials(AuthScope.ANY, credentials);
httpClient = HttpClients.custom().setDefaultCredentialsProvider(provider).build();
HttpGet httpGet = new HttpGet(serverUrl + fileName);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
inputStream = entity != null ? entity.getContent() : null;
if (inputStream == null) {
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
String encoding = "UTF-8";
zipArchiveInputStream = new ZipArchiveInputStream(inputStream, encoding);
ArchiveEntry archiveEntry = null;
byte[] buff = new byte[1024 * 5];
int rc = 0;
while(null!=(archiveEntry = zipArchiveInputStream.getNextEntry())){
while ((rc = zipArchiveInputStream.read(buff)) != -1) {
swapStream.write(buff, 0, rc);
}
}
byte[] in2b = swapStream.toByteArray();
return in2b;
} catch (Exception e) {
throw new Exception(e);
} finally {
try {
if (null != httpClient) {
httpClient.close();
}
} catch (Exception e) {
e.printStackTrace();
}
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (zipArchiveInputStream != null) {
try {
zipArchiveInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (swapStream != null) {
try {
swapStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public static void uploadStringWithBasicAuthAppand(String remoteDir, String username, String password, String remoteFileName, String content, String appandSeperator)
throws Exception {
// 这个方法是被外围的方法for循环调用的,所以写done文件时,需要使用追加的方式
String localFileName = null; // mgt所在服务器-临时文件
try {
localFileName = FilenameUtils.normalize(
FileUtils.getTempDirectoryPath() + File.separator + RandomStringUtils.random(8, "abcd1234jhkv"));
HttpClientUtils.downloadFileWithBasicAuth(remoteDir, username, password, remoteFileName, localFileName);
String oldContent = FileUtils.readFileToString(FileUtils.getFile(localFileName), "UTF-8");
if (StringUtils.contains(oldContent, "404 page not found")) {
logger.info("404 page not found。oldContent:{},content:{},localFileName:{}", oldContent, content, localFileName);
HttpClientUtils.uploadStringWithBasicAuth(remoteDir, username, password, remoteFileName, content);
} else {
String newContent = oldContent + appandSeperator + content;
logger.info("找到文件。 oldContent:{},content:{},localFileName:{}", oldContent, newContent, localFileName);
HttpClientUtils.uploadStringWithBasicAuth(remoteDir, username, password, remoteFileName, newContent);
}
// if (FileUtils.sizeOf(FileUtils.getFile(localFileName)) == 0) {
// } else {
// // FileUtils.writeStringToFile(FileUtils.getFile(localFileName),
// // appandSeperator + content, "UTF-8", true);
// }
}catch (Exception e){
throw new Exception(e.getMessage());
}finally {
if (localFileName != null) {
FileUtils.deleteQuietly(new File(localFileName));
}
}
}
}
FileUtil.java
package com.rongsoft.common.util;
import com.ayg.paymentmgt.exception.BusinessException;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;
public class FileUtil {
public static final String NO_JSON_WRAPPER = "NO-JSON-WRAPPER";
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(FileUtil.class);
public static void exportFile(HttpServletResponse response, String exportFileName, InputStream is) {
OutputStream os = null;
try {
response.addHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(exportFileName, "UTF-8")
+ ";filename*=utf-8''" + URLEncoder.encode(exportFileName, "UTF-8"));
response.addHeader(NO_JSON_WRAPPER, "1");
os = response.getOutputStream();
if (is != null) {// 判断文件是否存在
byte[] buff = new byte[200 * 1024];
int filelength = 0;
while ((filelength = is.read(buff)) > 0) {
os.write(buff, 0, filelength);
}
os.flush();
} else {
throw new BusinessException("文件不存在");
}
} catch (Exception e) {
throw new BusinessException(e);
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
} catch (IOException e) {
throw new BusinessException(e);
}
}
}
public static void exportFile(HttpServletResponse response, String exportFileName, byte[] bytes) {
OutputStream os = null;
try {
response.addHeader("Content-Disposition",
"attachment;filename=" + URLEncoder.encode(exportFileName, "UTF-8")
+ ";filename*=utf-8''" + URLEncoder.encode(exportFileName, "UTF-8"));
response.addHeader(NO_JSON_WRAPPER, "1");
os = response.getOutputStream();
os.write(bytes);
os.flush();
} catch (Exception e) {
throw new BusinessException(e);
} finally {
}
}
/**
* 使用GBK编码可以避免压缩中文文件名乱码
*/
private static final String CHINESE_CHARSET = "GBK";
/**
* 文件读取缓冲区大小
*/
private static final int CACHE_SIZE = 1024;
/**
* 第一步: 把 支付宝生成的账单 下载到本地目录
*
* @param path 支付宝资源url
* @param filePath 生成的zip 包目录
* @throws MalformedURLException
*/
public static void downloadNet(String path, String filePath)
throws MalformedURLException {
// 下载网络文件
if(StringUtils.isAnyEmpty(path,filePath)) {
logger.error("下载url文件,下载地址或保存路径为空,path:"+path+",filePath:"+filePath);
throw new BusinessException("下载url文件,下载地址或保存路径为空");
}
String logmsg = String.format("下载url文件,url:%s,localSavePath:%s",path,filePath);
URLConnection conn = null;
InputStream inStream = null;
FileOutputStream fs = null;
try {
URL url = new URL(path);
conn = url.openConnection();
inStream = conn.getInputStream();
fs = new FileOutputStream(filePath);
byte[] buffer = new byte[1024];
int byteread = 0;
while ((byteread = inStream.read(buffer)) != -1) {
fs.write(buffer, 0, byteread);
}
} catch (FileNotFoundException e) {
logger.error(logmsg+",出现异常",e);
throw new BusinessException(logmsg+",出现异常", e);
} catch (IOException e) {
logger.error(logmsg+",出现异常",e);
throw new BusinessException(logmsg+",出现异常", e);
} finally {
IOUtils.closeQuietly(fs);
IOUtils.closeQuietly(inStream);
}
}
public static void writeBytesToFile(byte[] bytes,String filePath){
FileOutputStream fos =null;
try {
fos=new FileOutputStream(filePath);
org.apache.commons.io.IOUtils.write(bytes,fos);
} catch (IOException e) {
logger.error("出现异常",e);
}finally {
IOUtils.closeQuietly(fos);
}
}
public static byte[] downloadFileByBytes(String serverFileUrl) {
CloseableHttpClient httpClient = null;
InputStream inputStream = null;
try {
httpClient = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(60000)
.setConnectTimeout(60000).setSocketTimeout(60000).setCircularRedirectsAllowed(true).build()).build();
HttpGet httpGet = new HttpGet(serverFileUrl);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity entity = httpResponse.getEntity();
inputStream = entity != null ? entity.getContent() : null;
if (inputStream == null) {
return ArrayUtils.EMPTY_BYTE_ARRAY;
}
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[100];
int rc = 0;
while ((rc = inputStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] in2b = swapStream.toByteArray();
return in2b;
} catch (IOException e) {
return ArrayUtils.EMPTY_BYTE_ARRAY;
} catch (Exception e) {
return ArrayUtils.EMPTY_BYTE_ARRAY;
} finally {
IOUtils.closeQuietly(httpClient);
IOUtils.closeQuietly(inputStream);
}
}
public static Map<String,byte[]> unZip(byte[] zipBytes,Charset charset) {
if (ArrayUtils.getLength(zipBytes) <= 0) {
throw new BusinessException("zip file is blank");
}
Map<String,byte[]> map=new HashMap<>();
// FileUtil.writeBytesToFile(zipBytes,"G:\\tmp.zip");
// ZipInputStream zis= null;
// FileInputStream fis=null;
// try {
// fis=new FileInputStream("G:\\tmp.zip");
// zis = new ZipInputStream(fis,Charset.forName("GBK"));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }finally {
// }
ZipInputStream zis=new ZipInputStream(new ByteArrayInputStream(zipBytes),charset);
ZipEntry entry = null ;
try {
while ((entry = zis.getNextEntry()) != null) {
byte[] bytes=org.apache.commons.io.IOUtils.toByteArray(zis);
map.put(entry.getName(), bytes);
}
}catch (Throwable t){
throw new BusinessException("解压缩文件失败",t);
}
return map;
}
public static void unZip(String zipFilePath, String destDir)
throws Exception {
ZipFile zipFile = new ZipFile(zipFilePath, Charset.forName(CHINESE_CHARSET));
Enumeration<?> emu = zipFile.entries();
BufferedInputStream bis;
FileOutputStream fos;
BufferedOutputStream bos;
File file, parentFile;
ZipEntry entry;
byte[] cache = new byte[CACHE_SIZE];
while (emu.hasMoreElements()) {
entry = (ZipEntry) emu.nextElement();
if (entry.isDirectory()) {
new File(destDir + entry.getName()).mkdirs();
continue;
}
bis = new BufferedInputStream(zipFile.getInputStream(entry));
file = new File(destDir + entry.getName());
parentFile = file.getParentFile();
if (parentFile != null && (!parentFile.exists())) {
parentFile.mkdirs();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos, CACHE_SIZE);
int nRead = 0;
while ((nRead = bis.read(cache, 0, CACHE_SIZE)) != -1) {
fos.write(cache, 0, nRead);
}
bos.flush();
bos.close();
fos.close();
bis.close();
}
zipFile.close();
}
}