使用HttpClient攜帶檔案傳送請求簡單例項
所需jar包:
commons-codec-1.6.jar
commons-io-1.2.jar
commons-logging-1.1.3.jar
fluent-hc-4.3.3.jar
httpclient-4.3.3.jar
httpclient-cache-4.3.3.jar
httpcore-4.3.2.jar
httpmime-4.3.3.jar
HttpClient請求
package com.servser; import java.io.File; import java.io.InputStream; import java.io.InputStreamReader; import net.sf.json.JSONObject; import org.apache.commons.io.IOUtils; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.FileEntity; import org.apache.http.impl.client.DefaultHttpClient; public class ClientTest { public static void main(String[] args) throws Exception { String filePath = ""; String servletPath = ""; //本地影像地址 filePath = "d:/img/000000000163887-2.jpg"; //連線服務端地址引數直接在地址後加上 servletPath = "http://192.168.1.226:8080/ServletDemo/ServletTest?test1=test"; File file = new File(filePath); FileEntity fileEntity = new FileEntity(file, "text/plain;charset=gbk"); HttpPost httpPost = new HttpPost(servletPath); httpPost.setHeader("Content-Type","text/xml;charset=gbk"); httpPost.setEntity(fileEntity); //set影像 System.out.println(httpPost); HttpClient httpclient = new DefaultHttpClient(); //傳送請求 HttpResponse response = httpclient.execute(httpPost); HttpEntity entity = response.getEntity(); if (entity != null) { //返參 InputStream instream = entity.getContent(); //轉換為String String str = IOUtils.toString(new InputStreamReader(instream)); //因為本例項傳送的json格式需要進行解析 JSONObject obj = JSONObject.fromObject(str) ; System.out.println(obj.get("imageKey")); System.out.println(obj.get("flag")); System.out.println(obj.get("detail")); IOUtils.closeQuietly(instream); //System.out.println(new String(str.getBytes("utf-8"))); } } }
解析Json格式所需JSON-lib jar包
commons-beanutils-1.8.0.jar
commons-collections-3.2.jar
commons-lang-2.4.jar
commons-logging-1.0.4.jar
ezmorph-1.0.4.jar
json-lib-2.2.3-jdk15.jar
服務端(使用的是servlet)
package com.servlet; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.IOUtils; /** * Servlet implementation class ServletTest */ @WebServlet("/ServletTest") public class ServletTest extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public ServletTest() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("utf-8"); //解決中文亂碼問題 System.out.println(request.getParameter("test1")); //接參 String imageKey = "3313"; //設定反參 String flag = "N";//設定反參 String detail = "不清晰";//設定反參 //這個是返回簡單字串方式,示例使用的是返回json格式 //response.getWriter().print(detail); //把接收過來的影像轉換為byte陣列 byte [] bfile = IOUtils.toByteArray(request.getInputStream()); BufferedOutputStream bos = null; FileOutputStream fos = null; File file = null; try { // File dir = new File("d:/"); /*if(!dir.exists()&&dir.isDirectory()){//判斷檔案目錄是否存在 dir.mkdirs(); }*/ file = new File("d:/img.jpg"); fos = new FileOutputStream(file); bos = new BufferedOutputStream(fos); bos.write(bfile); //把接收到的影像儲存到磁碟 } catch (Exception e) { e.printStackTrace(); } finally { if (bos != null) { try { bos.close(); } catch (IOException e1) { e1.printStackTrace(); } } if (fos != null) { try { fos.close(); } catch (IOException e1) { e1.printStackTrace(); } } } //返回引數,使用拼接json格式 response.getWriter().print("{\"imageKey\":\""+imageKey +"\",\"flag\":\""+flag+"\",\"detail\":\""+detail+"\"}"); } }
相關推薦
使用HttpClient攜帶檔案傳送請求簡單例項
所需jar包: commons-codec-1.6.jarcommons-io-1.2.jarcommons-logging-1.1.3.jarfluent-hc-4.3.3.jarhttpclient-4.3.3.jarhttpclient-cache-4.3.3.
java利用http請求實現簡訊傳送的簡單例項
public class sms_send { public static void main(String[] avgs) throws UnsupportedEncodingExceptio
fileupload檔案上傳簡單例項
1. 首先需要下載需要的元件。 下載commons-fileupload-1.2.1-bin.zip,下載網址: 下載commons-io-1.4-bin.zip,下載網址: 2. 用開源的FileUpload元件,可以很方便的給自己的系統新增功能
【Java】通過httpClient同步非同步傳送請求
非同步和同步 概念: 同步:傳送一個請求,需要等待返回結果,然後才能傳送下一個請求 非同步:傳送一個請求,不需要等待返回結果,隨時可以傳送下一個請求 Java 同步 GET 方法 priva
HttpClient與HtmlParse完美融合簡單例項
當然第一步當然是去網上下載Jar包啦,相信這個應該都會吧!收索HttpClient下載收索HtmlParse下載簡單例項程式碼:package com.fldyown.advertisement;import org.apache.http.HttpEntity;import
Java 檔案讀寫簡單例項
import java.io.*; public class ReadWrite { public void writeFile() { String str = "this is a p
java 通過HTTPClient工具類傳送請求
客戶端code 如下: package com.eas.bojoy; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.ParseExceptio
使用 HtmlEmail 傳送郵件 簡單例項
import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; public static void main(S
curl傳送請求簡單實踐
環境 作業系統:win7 伺服器:centos6.5 場景 因為有個指令碼需要模擬瀏覽器傳送api請求,該api是用來初始化資料的。 具體指令碼如下: appKey="TKtXLPUuGkfaRHA" appSecret="aArwoGW9d
Python爬蟲的post請求簡單例項
登陸網頁註冊時用Python進行post請求,程式碼 首先檢視頁面原始碼,注意form表單部分,哪裡是需要遞交的表單資訊 執行程式碼 import urllib.request import urllib.parse url='http://www.iqianyue.co
HttpClient傳送put與post請求程式碼例項與解析
/** * 例項化HttpClient * maxTotal 最大連線數 * maxPerRoute 最大併發量 * socketTimeout 從伺服器讀取資料超時時間 * connectTimeout 和伺服器建立連
SpringMVC配置,簡單例項,檔案上傳與下載,ajax請求
本文基於SpringMVC採用註解方式,從配置到簡單常用的功能舉例詳解,功能已經在本機測試過,能跑起來。 參考資料:iteye部落格; Spring文件; 1、匯入相關jar包:
httpClient使用postMethod方法傳送請求,攜帶引數並解決中文亂碼問題
前言:工作中遇到兩個系統之間通訊的問題,需求是這樣的:要求將資訊上報給上級部門(這裡的上級部門是兩一個系統),這就是跨系統通訊了 解決方案:使用httpclient實現網路通訊,傳遞資料。 關鍵問題:httpClient postMethod 傳遞引數 防止中文亂碼
curl傳送請求上傳檔案(multipart file upload)
折騰一下午的問題 第三方介面需要我們傳multipart 上傳檔案 curl一直各種試不成功,用Restlet Client工具總是能成功! 對比傳送的頭,發現工具在Content-Type: multipart/form-data;後面多了個這個boundary 然後去查了下
httpclient 傳送請求,帶中文資訊
有些功能 通過瀏覽器或者postman來代用沒問題。可是有時候通過java程式碼來呼叫,就容易出現中文亂碼 通過httpclient的get方法傳送中文 String location = "嘻哈"; CloseableHttpClient httpClient = HttpClient
httpclient 簡單例項
使用httpclient執行get請求 @Test public void doGet() throws Exception { //建立一個httpclient物件 CloseableHt
輕鬆把玩HttpClient之封裝HttpClient工具類(五),攜帶Cookie的請求
最近更新了一下HttpClientUtil工具類程式碼,主要是添加了一個引數HttpContext,這個是用來幹嘛的呢?其實是用來儲存和傳遞Cookie所需要的。因為我們有很多時候都需要登
單例模式使用httpclient傳送請求
使用httpclient傳送post和get請求時,需要例項化HttpClient例項,再呼叫httpClient.execute()方法,每次例項化HttpClient耗時較大,而且HttpClient例項不能共用,不利於大量的請求處理,考慮到HttpClient例項公用,
Scala 解析檔案內容簡單例項
import scala.io.Source //操作檔案的類 if(args.length > 0){ for(line <- Source.fromFile(args(0)).getLines()) println(line.
Java使用HttpClient傳送請求的幾種常用方式
使用的jar包有3個,Maven中新增以下依賴: <dependency> <groupId>commons-httpclient</groupId> <artifactId>commons-httpclient<