使用HttpClient携带文件发送请求简单实例
2014-08-29 10:19
375 查看
所需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.jarhttpcore-4.3.2.jarhttpmime-4.3.3.jar点击下载jar包: httpcomponents-client-4.3.3-bin.tar.gz 官网地址:http://hc.apache.org/httpclient-3.x/ 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.jarcommons-collections-3.2.jarcommons-lang-2.4.jarcommons-logging-1.0.4.jarezmorph-1.0.4.jarjson-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发送简单request请求
- 使用HttpClient 发送 GET、POST、PUT、Delete请求及文件上传
- 使用xutils发送POST请求,携带json和图片二进制文件数据获取服务器端返回json数据
- java发送HttpClient请求及接收请求结果过程的简单实例
- httpClient使用postMethod方法发送请求,携带参数并解决中文乱码问题
- java中使用scoket模拟http post请求发送图片或文件
- 使用HttpClient来异步发送POST请求并解析GZIP回应
- 使用HttpClient发送WebService Security(WSS)请求
- 如何使用HttpClient来发送带客户端证书的请求,以及如何忽略掉对服务器端证书的校验
- Android HTTP实例 使用GET方法和POST方法发送请求
- 使用单个httpclient实例请求数据。
- SpringMVC配置,简单实例,文件上传与下载,ajax请求
- java中使用scoket模拟http post请求发送图片或文件
- Java使用HttpClient发送请求
- 使用HttpClient向HTTPS地址发送POST请求4.1.2
- 使用HttpClient发送请求参数
- java中使用scoket模拟http post请求发送图片或文件
- java中使用scoket模拟http post请求发送图片或文件
- 【Media Foundation】简单实例 - 使用Media Session来播放文件
- gSoap使用实例 - 发送WSDL文件