Java使用HttpClient實(shí)現(xiàn)文件下載
使用HttpClient實(shí)現(xiàn)文件下載,供大家參考,具體內(nèi)容如下
服務(wù)端:
1、服務(wù)端是一個(gè)SpringBoot服務(wù),寫了一個(gè)接口用于客戶端請(qǐng)求文件,客戶端攜帶參數(shù)(文件名fileName)使用Get方式請(qǐng)求;
2、服務(wù)端獲取到文件名后將與本地倉(cāng)庫(kù)地址E:/downloadRepository/組成絕對(duì)路徑,獲取文件轉(zhuǎn)換成流,最后利用OutputStream對(duì)象將緩沖區(qū)的數(shù)據(jù)送到客戶端;
3、緩沖區(qū)大小也可設(shè)置為一個(gè)定值(如:1024*1024),然后再循環(huán)寫入輸出流對(duì)象;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@RestController
public class RequestController {
? ? @RequestMapping(path = "/download", method = RequestMethod.GET)
? ? public void downLoad(HttpServletResponse response, String fileName){
? ? ? ? File file = new File("E:/downloadRepository/" + fileName);
? ? ? ? try{
? ? ? ? ? ? InputStream inputStream = new BufferedInputStream (new FileInputStream(file));
? ? ? ? ? ? //創(chuàng)建緩沖區(qū)
? ? ? ? ? ? byte[] buffer = new byte[inputStream.available()];
? ? ? ? ? ? inputStream.read(buffer);
? ? ? ? ? ? inputStream.close();
? ? ? ? ? ? OutputStream outputStream = new BufferedOutputStream(response
? ? ? ? ? ? ? ? ? ? .getOutputStream());
? ? ? ? ? ? outputStream.write(buffer);
? ? ? ? ? ? outputStream.flush();
? ? ? ? ? ? outputStream.close();
? ? ? ? }catch (Exception e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}原文件:
1、原文件所在地址;

2、原文件內(nèi)容;

客戶端:
1、請(qǐng)求參數(shù)為一個(gè)文件的文件名,這里我們通過URIBuilder對(duì)象的addParameter()方法來進(jìn)行設(shè)置請(qǐng)求參數(shù);
2、在狀態(tài)碼等于200時(shí),將請(qǐng)求來的文件利用FileUtils工具類將字符串轉(zhuǎn)換成文件,并且該方法可以設(shè)置保存路徑;
import org.apache.commons.io.FileUtils;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.*;
public class RequestFileService {
? ? public void httpRequest(String fileName) {
? ? ? ? ? ? // 請(qǐng)求地址
? ? ? ? ? ? String url = "http://localhost:8080/download";
? ? ? ? ? ? // 下載文件保存路徑
? ? ? ? ? ? String savePath = "E:/demo/" + fileName;
? ? ? ? ? ? CloseableHttpClient httpclient = HttpClients.createDefault();
? ? ? ? ? ? CloseableHttpResponse response = null;
? ? ? ? ? ? try {
? ? ? ? ? ? ? ? // 使用URIBuilder對(duì)象用于設(shè)置請(qǐng)求參數(shù)
? ? ? ? ? ? ? ? URIBuilder urlBuilder = new URIBuilder(url);
? ? ? ? ? ? ? ? urlBuilder.addParameter("fileName", fileName);
? ? ? ? ? ? ? ? HttpGet httpGet = new HttpGet(urlBuilder.build());
? ? ? ? ? ? ? ? // 執(zhí)行請(qǐng)求
? ? ? ? ? ? ? ? response = httpclient.execute(httpGet);
? ? ? ? ? ? ? ? if (response.getStatusLine().getStatusCode() == 200) {
? ? ? ? ? ? ? ? ? ? String content = EntityUtils.toString(response.getEntity(), "UTF-8");
? ? ? ? ? ? ? ? ? ? // 將請(qǐng)求內(nèi)容轉(zhuǎn)換成文件并保存
? ? ? ? ? ? ? ? ? ? FileUtils.writeStringToFile(new File(savePath), content, "UTF-8");
? ? ? ? ? ? ? ? }
? ? ? ? ? ? } catch (Exception e) {
? ? ? ? ? ? ? ? e.getStackTrace();
? ? ? ? ? ? }finally {
? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? response.close();
? ? ? ? ? ? ? ? ? ? httpclient.close();
? ? ? ? ? ? ? ? }catch (IOException e){
? ? ? ? ? ? ? ? ? ? e.getStackTrace();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? }
? ? public static void main(String[] args) {
? ? ? ? RequestFileService requestFileService = new RequestFileService();
? ? ? ? //參數(shù)為請(qǐng)求文件名
? ? ? ? requestFileService.httpRequest("205老面館.txt");
? ? }
}下載結(jié)果:
1、按照代碼所設(shè)定的路徑,應(yīng)該存在E:/demo/目錄下;

2、檢查文件完整性,沒啥問題:)

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Spring中@PathVariable注解的簡(jiǎn)單使用
這篇文章主要介紹了Spring中@PathVariable注解的簡(jiǎn)單使用,@PathVariable 是 Spring Framework 中的注解之一,用于處理 RESTful Web 服務(wù)中的 URL 路徑參數(shù),它的作用是將 URL 中的路徑變量綁定到方法的參數(shù)上,需要的朋友可以參考下2024-01-01
java之lombok的構(gòu)建者模式Builder中的泛型寫法說明
這篇文章主要介紹了java之lombok的構(gòu)建者模式Builder中的泛型寫法說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01
SpringBoot3.0自定stater模塊的操作流程(chatGPT提供的49種場(chǎng)景)
SpringBoot3.0發(fā)布等重磅陸消息續(xù)進(jìn)入大家的視線,而本文作者將以技術(shù)整合的角度,帶大家把最火的兩個(gè)技術(shù)整合在一起,通過本文學(xué)習(xí)你將熟悉SpringBoot3.0自定stater模塊的操作流程,并熟悉OpenAi為chatGPT提供的49種場(chǎng)景,感興趣的朋友一起看看吧2022-12-12
詳解簡(jiǎn)單基于spring的redis配置(單機(jī)和集群模式)
這篇文章主要介紹了詳解簡(jiǎn)單基于spring的redis配置(單機(jī)和集群模式),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-02-02
java 遞歸查詢所有子節(jié)點(diǎn)id的方法實(shí)現(xiàn)
在多層次的數(shù)據(jù)結(jié)構(gòu)中,經(jīng)常需要查詢一個(gè)節(jié)點(diǎn)下的所有子節(jié)點(diǎn),本文主要介紹了java 遞歸查詢所有子節(jié)點(diǎn)id的方法實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03
Java實(shí)現(xiàn)“年-月-日 上午/下午時(shí):分:秒”的簡(jiǎn)單代碼
當(dāng)前的日期輸出的方法有很多,本文為大家介紹下在java中是如何實(shí)現(xiàn)“年-月-日 上午/下午時(shí):分:秒”,感興趣的朋友不妨參考下2015-08-08
java中將漢字轉(zhuǎn)換成拼音的實(shí)現(xiàn)代碼
java中將漢字轉(zhuǎn)換成拼音的實(shí)現(xiàn)代碼。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-10-10

