pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.go</groupId>
<artifactId>tool-com</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.13</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency>
</dependencies>
</project>
download
package cn.go;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class HttpClientTest {
public static void main(String[] args) {
try {
download("6010102080720200036802");
} catch (IOException e) {
e.printStackTrace();
}
}
private static void download(String str) throws IOException {
String urlPrefix = "http://xxx";
String url = urlPrefix + "/xxx?policyNo=" + str;
String result= null;
String defaultCharset = "utf-8";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = httpclient.execute(httpGet);
if (response != null) {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
result = EntityUtils.toString(resEntity, defaultCharset);
}
}
System.out.println(result);
String fileUrl = urlPrefix + "/xxxx"+result;
System.out.println(fileUrl);
HttpGet httpGetFile = new HttpGet(fileUrl);
HttpResponse fileResponse = httpclient.execute(httpGetFile);
if (fileResponse != null) {
HttpEntity resEntity = fileResponse.getEntity();
Header contentType = fileResponse.getEntity().getContentType();
System.out.println(contentType);
if (resEntity != null) {
result = EntityUtils.toString(resEntity, defaultCharset);
System.out.println(result);
if(!"xxxx".equals(result)){
System.exit(1);
}
return;
}
}
InputStream inputStream = fileResponse.getEntity().getContent();
ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
byte[] buff = new byte[1024*1024];
int len = 0;
while((len = inputStream.read(buff)) > 0) {
byteArray.write(buff, 0, len);
byteArray.flush();
}
inputStream.close();
((CloseableHttpResponse) fileResponse).close();
}
}