生活随笔
收集整理的這篇文章主要介紹了
[Java] 使用HttpClient实现文件下载
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
使用HttpClient實(shí)現(xiàn)文件下載
服務(wù)端:
服務(wù)端是一個(gè)SpringBoot服務(wù),寫(xiě)了一個(gè)接口用于客戶端請(qǐng)求文件,客戶端攜帶參數(shù)(文件名fileName)使用Get方式請(qǐng)求;
服務(wù)端獲取到文件名后將與本地倉(cāng)庫(kù)地址E:/downloadRepository/組成絕對(duì)路徑,獲取文件轉(zhuǎn)換成流,最后利用OutputStream對(duì)象將緩沖區(qū)的數(shù)據(jù)送到客戶端;
緩沖區(qū)大小也可設(shè)置為一個(gè)定值(如:1024*1024),然后再循環(huán)寫(xiě)入輸出流對(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
));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();}}
}
原文件:
原文件所在地址;
原文件內(nèi)容;
客戶端:
請(qǐng)求參數(shù)為一個(gè)文件的文件名,這里我們通過(guò)URIBuilder對(duì)象的addParameter()方法來(lái)進(jìn)行設(shè)置請(qǐng)求參數(shù);
在狀態(tài)碼等于200時(shí),將請(qǐng)求來(lái)的文件利用FileUtils工具類(lèi)將字符串轉(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
) {String url
= "http://localhost:8080/download";String savePath
= "E:/demo/" + fileName
;CloseableHttpClient httpclient
= HttpClients
.createDefault();CloseableHttpResponse response
= null
;try {URIBuilder urlBuilder
= new URIBuilder(url
);urlBuilder
.addParameter("fileName", fileName
);HttpGet httpGet
= new HttpGet(urlBuilder
.build());response
= httpclient
.execute(httpGet
);if (response
.getStatusLine().getStatusCode() == 200) {String content
= EntityUtils
.toString(response
.getEntity(), "UTF-8");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();requestFileService
.httpRequest("205老面館.txt");}
}
下載結(jié)果:
按照代碼所設(shè)定的路徑,應(yīng)該存在E:/demo/目錄下;
檢查文件完整性,沒(méi)啥問(wèn)題:)
總結(jié)
以上是生活随笔為你收集整理的[Java] 使用HttpClient实现文件下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。