httpconnent传输附件

论坛 期权论坛     
选择匿名的用户   2021-6-2 15:54   252   0
<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> * &#64;see 通过HttpURLConnection请求商家系统进行商家注册<br> * &#64;author zhengcf<br> * &#64;since 2015/02/28<br> */<br>public class HttpPostUtil { <br> URL url; <br> HttpURLConnection conn; <br> String boundary &#61; &#34;--------httppost&#34;; <br> Map&lt;String, String&gt; textParams &#61; new HashMap&lt;String, String&gt;(); <br> Map&lt;String, File&gt; fileparams &#61; new HashMap&lt;String, File&gt;(); <br> DataOutputStream ds; <br> String para;<br><br> public HttpPostUtil(String url) throws Exception { <br> this.url &#61; new URL(url); <br> } <br> /**<br> * &#64;author zcf<br> * &#64;param url<br> * &#64;throws Exception<br> */<br> //重新设置要请求的服务器地址,即上传文件的地址。 <br> public void setUrl(String url) throws Exception { <br> this.url &#61; 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&lt;String, String&gt; 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 &#61; new DataOutputStream(conn.getOutputStream()); <br> writeFileParams(); <br> writeStringParams(); <br> paramsEnd(); <br> InputStream in &#61; conn.getInputStream(); <br> ByteArrayOutputStream out &#61; new ByteArrayOutputStream(); <br> int b; <br> while ((b &#61; in.read()) !&#61; -1) { <br> out.write(b); <br> } <br> conn.disconnect(); <br> return out.toByteArray(); <br> } <br> //文件上传的connection的一些必须设置 <br> private void initConnection() throws Exception { <br> conn &#61; (HttpURLConnection) this.url.openConnection(); <br> conn.setDoOutput(true); <br> conn.setUseCaches(false); <br> conn.setConnectTimeout(10000); //连接超时为10秒 <br> conn.setRequestMethod(&#34;POST&#34;); <br> conn.setRequestProperty(&#34;Content-Type&#34;, <br> &#34;multipart/form-data; boundary&#61;&#34; &#43; boundary); <br> } <br> //普通字符串数据 <br> private void writeStringParams() throws Exception { <br> Set&lt;String&gt; keySet &#61; textParams.keySet(); <br> for (Iterator&lt;String&gt; it &#61; keySet.iterator(); it.hasNext();) { <br> String name &#61; it.next(); <br> String value &#61; textParams.get(name); <br> ds.writeBytes(&#34;--&#34; &#43; boundary &#43; &#34;\r\n&#34;); <br> ds.writeBytes(&#34;Content-Disposition: form-data; name&#61;\&#34;&#34; &#43; name <br> &#43; &#34;\&#34;\r\n&#34;); <br> ds.writeBytes(&#34;\r\n&#34;); <br> ds.writeBytes(encode(value) &#43; &#34;\r\n&#34;); <br> } <br> } <br> //文件数据 <br> private void writeFileParams() throws Exception { <br> Set&lt;String&gt; keySet &#61; fileparams.keySet(); <br> for (Iterator&lt;String&gt; it &#61; keySet.iterator(); it.hasNext();) { <br> String name &#61; it.next(); <br> File value &#61; fileparams.get(name); <br> ds.writeBytes(&#34;--&#34; &#43; boundary &#43; &#34;\r\n&#34;); <br> ds.writeBytes(&#34;Content-Disposition: form-data; name&#61;\&#34;&#34; &#43; name <br> &#43; &#34;\&#34;; filename&#61;\&#34;&#34; &#43; encode(value.getName()) &#43; &#34;\&#34;\r\n&#34;); <br> ds.writeBytes(&#34;Content-Type: &#34; &#43; getContentType(value) &#43; &#34;\r\n&#34;); <br> ds.writeBytes(&#34;\r\n&#34;); <br> ds.write(getBytes(value)); <br> ds.writeBytes(&#34;\r\n&#34;); <br> } <br> } <br> //获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream <br> private String getContentType(File f) throws Exception { <br><br>// return &#34;application/octet-stream&#34;; // 此行不再细分是否为图片,全部作为application/octet-stream 类型 <br> ImageInputStream imagein &#61; ImageIO.createImageInputStream(f); <br> if (imagein &#61;&#61; null) { <br> return &#34;application/octet-stream&#34;; <br> } <br> Iterator&lt;ImageReader&gt; it &#61; ImageIO.getImageReaders(imagein); <br> if (!it.hasNext()) { <br> imagein.close(); <br> return &#34;application/octet-stream&#34;; <br> } <br> imagein.close(); <br> return &#34;image/&#34; &#43; it.next().getFormatName().toLowerCase();//将Fo
分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP