Java使用HttpClient實現文件下載
使用HttpClient實現文件下載,供大家參考,具體內容如下
服務端:
1、服務端是一個SpringBoot服務,寫了一個接口用于客戶端請求文件,客戶端攜帶參數(文件名fileName)使用Get方式請求;
2、服務端獲取到文件名后將與本地倉庫地址E:/downloadRepository/組成絕對路徑,獲取文件轉換成流,最后利用OutputStream對象將緩沖區(qū)的數據送到客戶端;
3、緩沖區(qū)大小也可設置為一個定值(如:1024*1024),然后再循環(huán)寫入輸出流對象;
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、原文件內容;
客戶端:
1、請求參數為一個文件的文件名,這里我們通過URIBuilder對象的addParameter()方法來進行設置請求參數;
2、在狀態(tài)碼等于200時,將請求來的文件利用FileUtils工具類將字符串轉換成文件,并且該方法可以設置保存路徑;
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對象用于設置請求參數 ? ? ? ? ? ? ? ? URIBuilder urlBuilder = new URIBuilder(url); ? ? ? ? ? ? ? ? urlBuilder.addParameter("fileName", fileName); ? ? ? ? ? ? ? ? HttpGet httpGet = new HttpGet(urlBuilder.build()); ? ? ? ? ? ? ? ? // 執(zhí)行請求 ? ? ? ? ? ? ? ? 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"); ? ? } }
下載結果:
1、按照代碼所設定的路徑,應該存在E:/demo/目錄下;
2、檢查文件完整性,沒啥問題:)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
java ArrayBlockingQueue阻塞隊列的實現示例
ArrayBlockingQueue是一個基于數組實現的阻塞隊列,本文就來介紹一下java ArrayBlockingQueue阻塞隊列的實現示例,具有一定的參考價值,感興趣的可以了解一下2024-02-02