HttpClient 3.1下载文件

 项目有点老,用的HttpClient 3.1

 

需要有的Jar;
    * commons-httpclient-3.1.jar
    * commons-logging-1.04.jar
    * commons-codec-1.3.jar

 

public class HttpClientTest {   
       
    private final static String REMOTE_FILE_URL = "http://www.gjt.org/download/time/java/tar/javatar-2.5.tar.gz";   
       
    private final static int BUFFER = 1024;   
  
    public static void main(String[] args) {   
  
       HttpClient client = new HttpClient();   
       GetMethod httpGet = new GetMethod(REMOTE_FILE_URL);   
        try {   
            client.executeMethod(httpGet);   
               
            InputStream in = httpGet.getResponseBodyAsStream();   
              
            FileOutputStream out = new FileOutputStream(new File("E:\\test_jar\\javatar-2.5.tar.gz"));   
              
            byte[] b = new byte[BUFFER];   
            int len = 0;   
            while((len=in.read(b))!= -1){   
                out.write(b,0,len);   
            }   
            in.close();   
            out.close();   
               
        }catch (HttpException e){   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        }finally{   
            httpGet.releaseConnection();   
        }   
        System.out.println("download, success!!");   
       }   
} 

 

参考了:

http://kukuqiu.iteye.com/blog/343019

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Spring Boot 3.1 中使用 `RestTemplate` 和 `HttpClient 5`,你需要添加以下依赖项到你的 `pom.xml` 文件中: ```xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>5.0.4</version> </dependency> </dependencies> ``` 然后,你需要创建一个 `RestTemplate` 的 Bean,使用 `HttpClient 5` 作为底层 HTTP 客户端。以下是示例代码: ```java import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; import org.apache.hc.client5.http.impl.classic.HttpClients; import org.apache.hc.core5.http.config.Registry; import org.apache.hc.core5.http.config.RegistryBuilder; import org.apache.hc.core5.http.impl.io.PoolingHttpClientConnectionManager; import org.apache.hc.core5.http.io.SocketConfig; import org.apache.hc.core5.http.protocol.HttpRequestInterceptor; import org.apache.hc.core5.http.protocol.RequestAddCookies; import org.apache.hc.core5.http.protocol.RequestDefaultHeaders; import org.apache.hc.core5.http.protocol.ResponseProcessCookies; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate() { // 创建 HttpClient CloseableHttpClient httpClient = HttpClients.custom() .setDefaultRequestConfig(RequestConfig.custom() .setSocketTimeout(5000) .setConnectTimeout(5000) .build()) .setConnectionManager(new PoolingHttpClientConnectionManager( RegistryBuilder.<ConnectionSocketFactory>create() .register("http", PlainConnectionSocketFactory.getSocketFactory()) .register("https", SSLConnectionSocketFactory.getSocketFactory()) .build())) .setDefaultSocketConfig(SocketConfig.custom() .setTcpNoDelay(true) .build()) .addInterceptorFirst((HttpRequestInterceptor) new RequestDefaultHeaders( Arrays.asList(new BasicHeader("User-Agent", "MyRestClient")))) // 设置 User-Agent .addInterceptorFirst(new RequestAddCookies()) .addInterceptorLast(new ResponseProcessCookies()) .build(); // 创建 RestTemplate HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(httpClient); RestTemplate restTemplate = new RestTemplate(factory); return restTemplate; } } ``` 这里我们创建了一个 `RestTemplate` 的 Bean,使用 `HttpClient 5` 作为底层 HTTP 客户端。我们还可以设置一些默认的请求配置和拦截器,例如设置 `User-Agent`、添加 Cookie 等。 现在你可以在你的代码中使用 `RestTemplate` 进行 HTTP 请求了: ```java @Autowired private RestTemplate restTemplate; public void doRequest() { String url = "http://example.com/api"; ResponseEntity<String> response = restTemplate.getForEntity(url, String.class); String body = response.getBody(); // 处理响应数据 } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值