Commit 352eb876 authored by 郑建's avatar 郑建

增加删除临时文件

parent 87f50b15
......@@ -105,9 +105,7 @@ public class HttpClientConfig {
}
@Bean("niwodaiHttpsClient")
public CloseableHttpClient niwodaiHttpsClient() throws Exception{
File temp1 = null,temp2 = null;
try{
public CloseableHttpClient niwodaiHttpsClient() throws Exception {
/**
* 创建TrustManager
*/
......@@ -127,11 +125,11 @@ public class HttpClientConfig {
};
String classesPath = "/niwodai";
KeyStore keyStore = KeyStore.getInstance("PKCS12");
temp1 = asFile(readJarFile(classesPath + "/qg-keystore.jks"));
keyStore.load(new FileInputStream(temp1), "password".toCharArray());
temp2 = asFile(readJarFile(classesPath + "/qg-truststore.jks"));
keyStore.load(HttpClientConfig.class.getResourceAsStream(classesPath + "/qg-keystore.jks"), "password".toCharArray());
KeyStore trustStore = KeyStore.getInstance("JKS");
trustStore.load(HttpClientConfig.class.getResourceAsStream(classesPath + "/qg-truststore.jks"), "password".toCharArray());
SSLContext sslcontext = SSLContexts.custom()
.loadTrustMaterial(temp2, "password".toCharArray(), new TrustSelfSignedStrategy())
.loadTrustMaterial(trustStore, new TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, "password".toCharArray())
.build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(
......@@ -170,31 +168,7 @@ public class HttpClientConfig {
.setRetryHandler(retryHandler)
.setKeepAliveStrategy(keepAliveStrategy)
.build();
}finally {
if (temp1 != null){
temp1.delete();
}
if (temp2 != null){
temp2.delete();
}
}
}
private static InputStream readJarFile(String fileName) throws IOException
{
return HttpClientConfig.class.getResourceAsStream(fileName);
}
public static File asFile(InputStream inputStream) throws IOException{
File tmp = File.createTempFile("trustKey", ".jks", new File(""));
OutputStream os = new FileOutputStream(tmp);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
inputStream.close();
return tmp;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment