運用HttpClient完成文件的上傳下載辦法。本站提示廣大學習愛好者:(運用HttpClient完成文件的上傳下載辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是運用HttpClient完成文件的上傳下載辦法正文
1 HTTP
HTTP 協議能夠是如今 Internet 上運用得最多、最重要的協議了,越來越多的 Java 使用順序需求直接經過 HTTP 協議來訪問網絡資源。
雖然在 JDK 的 java.net 包中曾經提供了訪問 HTTP 協議的根本功用,但是關於大局部使用順序來說,JDK 庫自身提供的功用還不夠豐厚和靈敏。HttpClient 用來提供高效的、最新的、功用豐厚的支持 HTTP 協議的客戶端編程工具包,並且它支持 HTTP 協議最新的版本和建議。
普通的狀況下我們都是運用Chrome或許其他閱讀器來訪問一個WEB服務器,用來閱讀頁面檢查信息或許提交一些數據、文件上傳下載等等。所訪問的這些頁面有的僅僅是一些普通的頁面,有的需求用戶登錄前方可運用,或許需求認證以及是一些經過加密方式傳輸,例如HTTPS。目前我們運用的閱讀器處置這些狀況都不會構成問題。但是一旦我們有需求不經過閱讀器來訪問服務器的資源呢?那該怎樣辦呢?
上面以本地客戶端發起文件的上傳、下載為例做個小Demo。HttpClient有兩種方式,一種是org.apache.http下的,一種是org.apache.commons.httpclient.HttpClient。
2 文件上傳
文件上傳可以運用兩種方式完成,一種是PostMethod方式,一種是HttpPost方式。兩者的處置迥然不同。PostMethod是運用FileBody將文件包裝流包裝起來,HttpPost是運用FilePart將文件流包裝起來。在傳遞文件流給服務端的時分,都可以同時傳遞其他的參數。
2.1 客戶端處置
2.1.1 PostMethod方式
將文件封裝到FilePart中,放入Part數組,同時,其他參數可以放入StringPart中,這裡沒有寫,只是單純的將參數以setParameter的方式停止設置。此處的HttpClient是org.apache.commons.httpclient.HttpClient。
public void upload(String localFile){ File file = new File(localFile); PostMethod filePost = new PostMethod(URL_STR); HttpClient client = new HttpClient(); try { // 經過以下辦法可以模仿頁面參數提交 filePost.setParameter("userName", userName); filePost.setParameter("passwd", passwd); Part[] parts = { new FilePart(file.getName(), file) }; filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams())); client.getHttpConnectionManager().getParams().setConnectionTimeout(5000); int status = client.executeMethod(filePost); if (status == HttpStatus.SC_OK) { System.out.println("上傳成功"); } else { System.out.println("上傳失敗"); } } catch (Exception ex) { ex.printStackTrace(); } finally { filePost.releaseConnection(); } }
記得搞完之後,要經過releaseConnection釋放銜接。
2.1.2 HttpPost方式
這種方式,與下面相似,只不過變成了FileBody。下面的Part數組在這裡對應HttpEntity。此處的HttpClient是org.apache.http.client.methods下的。
public void upload(String localFile){ CloseableHttpClient httpClient = null; CloseableHttpResponse response = null; try { httpClient = HttpClients.createDefault(); // 把一個普通參數和文件上傳給上面這個地址 是一個servlet HttpPost httpPost = new HttpPost(URL_STR); // 把文件轉換成流對象FileBody FileBody bin = new FileBody(new File(localFile)); StringBody userName = new StringBody("Scott", ContentType.create( "text/plain", Consts.UTF_8)); StringBody password = new StringBody("123456", ContentType.create( "text/plain", Consts.UTF_8)); HttpEntity reqEntity = MultipartEntityBuilder.create() // 相當於<input type="file" name="file"/> .addPart("file", bin) // 相當於<input type="text" name="userName" value=userName> .addPart("userName", userName) .addPart("pass", password) .build(); httpPost.setEntity(reqEntity); // 發起懇求 並前往懇求的呼應 response = httpClient.execute(httpPost); System.out.println("The response value of token:" + response.getFirstHeader("token")); // 獲取呼應對象 HttpEntity resEntity = response.getEntity(); if (resEntity != null) { // 打印呼應長度 System.out.println("Response content length: " + resEntity.getContentLength()); // 打印呼應內容 System.out.println(EntityUtils.toString(resEntity, Charset.forName("UTF-8"))); } // 銷毀 EntityUtils.consume(resEntity); }catch (Exception e){ e.printStackTrace(); }finally { try { if(response != null){ response.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(httpClient != null){ httpClient.close(); } } catch (IOException e) { e.printStackTrace(); } } }
2.2 服務端處置
無論客戶端是哪種上傳方式,服務端的處置都是一樣的。在經過HttpServletRequest取得參數之後,把失掉的Item停止分類,分為普通的表單和File表單。
經過ServletFileUpload 可以設置上傳文件的大小及編碼格式等。
總之,服務端的處置是把失掉的參數當做HTML表單停止處置的。
public void processUpload(HttpServletRequest request, HttpServletResponse response){ File uploadFile = new File(uploadPath); if (!uploadFile.exists()) { uploadFile.mkdirs(); } System.out.println("Come on, baby ......."); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //檢測是不是存在上傳文件 boolean isMultipart = ServletFileUpload.isMultipartContent(request); if(isMultipart){ DiskFileItemFactory factory = new DiskFileItemFactory(); //指定在內存中緩存數據大小,單位為byte,這裡設為1Mb factory.setSizeThreshold(1024*1024); //設置一旦文件大小超越getSizeThreshold()的值時數據寄存在硬盤的目錄 factory.setRepository(new File("D:\\temp")); // Create a new file upload handler ServletFileUpload upload = new ServletFileUpload(factory); // 指定單個上傳文件的最大尺寸,單位:字節,這裡設為50Mb upload.setFileSizeMax(50 * 1024 * 1024); //指定一次上傳多個文件的總尺寸,單位:字節,這裡設為50Mb upload.setSizeMax(50 * 1024 * 1024); upload.setHeaderEncoding("UTF-8"); List<FileItem> items = null; try { // 解析request懇求 items = upload.parseRequest(request); } catch (FileUploadException e) { e.printStackTrace(); } if(items!=null){ //解析表單項目 Iterator<FileItem> iter = items.iterator(); while (iter.hasNext()) { FileItem item = iter.next(); //假如是普通表單屬性 if (item.isFormField()) { //相當於input的name屬性 <input type="text" name="content"> String name = item.getFieldName(); //input的value屬性 String value = item.getString(); System.out.println("屬性:" + name + " 屬性值:" + value); } //假如是上傳文件 else { //屬性名 String fieldName = item.getFieldName(); //上傳文件途徑 String fileName = item.getName(); fileName = fileName.substring(fileName.lastIndexOf("/") + 1);// 取得上傳文件的文件名 try { item.write(new File(uploadPath, fileName)); } catch (Exception e) { e.printStackTrace(); } } } } } response.addHeader("token", "hello"); }
服務端在處置之後,可以在Header中設置前往給客戶端的復雜信息。假如前往客戶端是一個流的話,流的大小必需提早設置!
response.setContentLength((int) file.length());
3 文件下載
文件的下載可以運用HttpClient的GetMethod完成,還可以運用HttpGet方式、原始的HttpURLConnection方式。
3.1 客戶端處置
3.1.1 GetMethod方式
此處的HttpClient是org.apache.commons.httpclient.HttpClient。
public void downLoad(String remoteFileName, String localFileName) { HttpClient client = new HttpClient(); GetMethod get = null; FileOutputStream output = null; try { get = new GetMethod(URL_STR); get.setRequestHeader("userName", userName); get.setRequestHeader("passwd", passwd); get.setRequestHeader("fileName", remoteFileName); int i = client.executeMethod(get); if (SUCCESS == i) { System.out.println("The response value of token:" + get.getResponseHeader("token")); File storeFile = new File(localFileName); output = new FileOutputStream(storeFile); // 失掉網絡資源的字節數組,並寫入文件 output.write(get.getResponseBody()); } else { System.out.println("DownLoad file occurs exception, the error code is :" + i); } } catch (Exception e) { e.printStackTrace(); } finally { try { if(output != null){ output.close(); } } catch (IOException e) { e.printStackTrace(); } get.releaseConnection(); client.getHttpConnectionManager().closeIdleConnections(0); } }
3.1.2 HttpGet方式
此處的HttpClient是org.apache.http.client.methods下的。
public void downLoad(String remoteFileName, String localFileName) { DefaultHttpClient httpClient = new DefaultHttpClient(); OutputStream out = null; InputStream in = null; try { HttpGet httpGet = new HttpGet(URL_STR); httpGet.addHeader("userName", userName); httpGet.addHeader("passwd", passwd); httpGet.addHeader("fileName", remoteFileName); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity entity = httpResponse.getEntity(); in = entity.getContent(); long length = entity.getContentLength(); if (length <= 0) { System.out.println("下載文件不存在!"); return; } System.out.println("The response value of token:" + httpResponse.getFirstHeader("token")); File file = new File(localFileName); if(!file.exists()){ file.createNewFile(); } out = new FileOutputStream(file); byte[] buffer = new byte[4096]; int readLength = 0; while ((readLength=in.read(buffer)) > 0) { byte[] bytes = new byte[readLength]; System.arraycopy(buffer, 0, bytes, 0, readLength); out.write(bytes); } out.flush(); } catch (IOException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(in != null){ in.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(out != null){ out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
3.1.3 HttpURLConnection方式
public void download3(String remoteFileName, String localFileName) { FileOutputStream out = null; InputStream in = null; try{ URL url = new URL(URL_STR); URLConnection urlConnection = url.openConnection(); HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection; // true -- will setting parameters httpURLConnection.setDoOutput(true); // true--will allow read in from httpURLConnection.setDoInput(true); // will not use caches httpURLConnection.setUseCaches(false); // setting serialized httpURLConnection.setRequestProperty("Content-type", "application/x-java-serialized-object"); // default is GET httpURLConnection.setRequestMethod("POST"); httpURLConnection.setRequestProperty("connection", "Keep-Alive"); httpURLConnection.setRequestProperty("Charsert", "UTF-8"); // 1 min httpURLConnection.setConnectTimeout(60000); // 1 min httpURLConnection.setReadTimeout(60000); httpURLConnection.addRequestProperty("userName", userName); httpURLConnection.addRequestProperty("passwd", passwd); httpURLConnection.addRequestProperty("fileName", remoteFileName); // connect to server (tcp) httpURLConnection.connect(); in = httpURLConnection.getInputStream();// send request to // server File file = new File(localFileName); if(!file.exists()){ file.createNewFile(); } out = new FileOutputStream(file); byte[] buffer = new byte[4096]; int readLength = 0; while ((readLength=in.read(buffer)) > 0) { byte[] bytes = new byte[readLength]; System.arraycopy(buffer, 0, bytes, 0, readLength); out.write(bytes); } out.flush(); }catch(Exception e){ e.printStackTrace(); }finally{ try { if(in != null){ in.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(out != null){ out.close(); } } catch (IOException e) { e.printStackTrace(); } } }
3.2 服務端處置
雖然客戶端的處置方式不同,但是服務端是一樣的。
public void processDownload(HttpServletRequest request, HttpServletResponse response){ int BUFFER_SIZE = 4096; InputStream in = null; OutputStream out = null; System.out.println("Come on, baby ......."); try{ request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); response.setContentType("application/octet-stream"); String userName = request.getHeader("userName"); String passwd = request.getHeader("passwd"); String fileName = request.getHeader("fileName"); System.out.println("userName:" + userName); System.out.println("passwd:" + passwd); System.out.println("fileName:" + fileName); //可以依據傳遞來的userName和passwd做進一步處置,比方驗證懇求能否合法等 File file = new File(downloadPath + "\\" + fileName); response.setContentLength((int) file.length()); response.setHeader("Accept-Ranges", "bytes"); int readLength = 0; in = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE); out = new BufferedOutputStream(response.getOutputStream()); byte[] buffer = new byte[BUFFER_SIZE]; while ((readLength=in.read(buffer)) > 0) { byte[] bytes = new byte[readLength]; System.arraycopy(buffer, 0, bytes, 0, readLength); out.write(bytes); } out.flush(); response.addHeader("token", "hello 1"); }catch(Exception e){ e.printStackTrace(); response.addHeader("token", "hello 2"); }finally { if (in != null) { try { in.close(); } catch (IOException e) { } } if (out != null) { try { out.close(); } catch (IOException e) { } } } }
4 小結
HttpClient最根本的功用就是執行Http辦法。一個Http辦法的執行觸及到一個或許多個Http懇求/Http呼應的交互,通常這個進程都會自動被HttpClient處置,對用戶通明。用戶只需求提供Http懇求對象,HttpClient就會將http懇求發送給目的服務器,並且接納服務器的呼應,假如http懇求執行不成功,httpclient就會拋出異常。所以在寫代碼的時分留意finally的處置。
一切的Http懇求都有一個懇求列(request line),包括辦法名、懇求的URI和Http版本號。HttpClient支持HTTP/1.1這個版本定義的一切Http辦法:GET,HEAD,POST,PUT,DELETE,TRACE和OPTIONS。下面的上傳用到了Post,下載是Get。
目前來說,運用org.apache.commons.httpclient.HttpClient多一些。看自己了~
以上就是為大家帶來的運用HttpClient完成文件的上傳下載辦法全部內容了,希望大家多多支持~