<div class="blogpost-body" id="cnblogs_post_body">
<p>发送方:</p>
<p>package com.zte.mdm.home.util;<br>import java.io.ByteArrayOutputStream; <br>import java.io.DataOutputStream; <br>import java.io.File; <br>import java.io.FileInputStream; <br>import java.io.InputStream; <br>import java.lang.reflect.Type;<br>import java.net.HttpURLConnection; <br>import java.net.SocketTimeoutException; <br>import java.net.URL; <br>import java.net.URLEncoder; <br>import java.util.HashMap; <br>import java.util.Iterator; <br>import java.util.Map; <br>import java.util.Set; <br><br>import javax.imageio.ImageIO; <br>import javax.imageio.ImageReader; <br>import javax.imageio.stream.ImageInputStream; </p>
<p>import com.google.gson.Gson;<br>import com.google.gson.reflect.TypeToken;</p>
<p>/**<br> * @see 通过HttpURLConnection请求商家系统进行商家注册<br> * @author zhengcf<br> * @since 2015/02/28<br> */<br>public class HttpPostUtil { <br> URL url; <br> HttpURLConnection conn; <br> String boundary = "--------httppost"; <br> Map<String, String> textParams = new HashMap<String, String>(); <br> Map<String, File> fileparams = new HashMap<String, File>(); <br> DataOutputStream ds; <br> String para;<br><br> public HttpPostUtil(String url) throws Exception { <br> this.url = new URL(url); <br> } <br> /**<br> * @author zcf<br> * @param url<br> * @throws Exception<br> */<br> //重新设置要请求的服务器地址,即上传文件的地址。 <br> public void setUrl(String url) throws Exception { <br> this.url = new URL(url); <br> } <br> //增加一个普通字符串数据到form表单数据中 <br> public void addTextParameter(String name, String value) { <br> textParams.put(name, value); <br> }<br> //批量增加字符类键值对<br> public void addAllTextParameter(Map<String, String> Params) { <br> textParams.putAll(Params);<br> } <br> //增加一个文件到form表单数据中 <br> public void addFileParameter(String name, File value) { <br> fileparams.put(name, value); <br> } <br> // 清空所有已添加的form表单数据 <br> public void clearAllParameters() { <br> textParams.clear(); <br> fileparams.clear(); <br> } <br> // 发送数据到服务器,返回一个字节包含服务器的返回结果的数组 <br> public byte[] send() throws Exception { <br> initConnection(); <br> try { <br> conn.connect(); <br> } catch (SocketTimeoutException e) { <br> // something <br> throw new RuntimeException(); <br> } <br> ds = new DataOutputStream(conn.getOutputStream()); <br> writeFileParams(); <br> writeStringParams(); <br> paramsEnd(); <br> InputStream in = conn.getInputStream(); <br> ByteArrayOutputStream out = new ByteArrayOutputStream(); <br> int b; <br> while ((b = in.read()) != -1) { <br> out.write(b); <br> } <br> conn.disconnect(); <br> return out.toByteArray(); <br> } <br> //文件上传的connection的一些必须设置 <br> private void initConnection() throws Exception { <br> conn = (HttpURLConnection) this.url.openConnection(); <br> conn.setDoOutput(true); <br> conn.setUseCaches(false); <br> conn.setConnectTimeout(10000); //连接超时为10秒 <br> conn.setRequestMethod("POST"); <br> conn.setRequestProperty("Content-Type", <br> "multipart/form-data; boundary=" + boundary); <br> } <br> //普通字符串数据 <br> private void writeStringParams() throws Exception { <br> Set<String> keySet = textParams.keySet(); <br> for (Iterator<String> it = keySet.iterator(); it.hasNext();) { <br> String name = it.next(); <br> String value = textParams.get(name); <br> ds.writeBytes("--" + boundary + "\r\n"); <br> ds.writeBytes("Content-Disposition: form-data; name=\"" + name <br> + "\"\r\n"); <br> ds.writeBytes("\r\n"); <br> ds.writeBytes(encode(value) + "\r\n"); <br> } <br> } <br> //文件数据 <br> private void writeFileParams() throws Exception { <br> Set<String> keySet = fileparams.keySet(); <br> for (Iterator<String> it = keySet.iterator(); it.hasNext();) { <br> String name = it.next(); <br> File value = fileparams.get(name); <br> ds.writeBytes("--" + boundary + "\r\n"); <br> ds.writeBytes("Content-Disposition: form-data; name=\"" + name <br> + "\"; filename=\"" + encode(value.getName()) + "\"\r\n"); <br> ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n"); <br> ds.writeBytes("\r\n"); <br> ds.write(getBytes(value)); <br> ds.writeBytes("\r\n"); <br> } <br> } <br> //获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream <br> private String getContentType(File f) throws Exception { <br><br>// return "application/octet-stream"; // 此行不再细分是否为图片,全部作为application/octet-stream 类型 <br> ImageInputStream imagein = ImageIO.createImageInputStream(f); <br> if (imagein == null) { <br> return "application/octet-stream"; <br> } <br> Iterator<ImageReader> it = ImageIO.getImageReaders(imagein); <br> if (!it.hasNext()) { <br> imagein.close(); <br> return "application/octet-stream"; <br> } <br> imagein.close(); <br> return "image/" + it.next().getFormatName().toLowerCase();//将Fo |
|