Java中的附件上传功能怎么利用HttpURLConnection实现

  介绍

本篇文章给大家分享的是有关Java中的附件上传功能怎么利用HttpURLConnection实现,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

<强> Java中HttpURLConnection附件上传的实例详解

示例代码:

/* *   *以Http协议传输文件   *   * @author mingxue.zhang@163.com   *   */公开课HttpPostUtil {      私人最终静态char [] MULTIPART_CHARS=?_1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"   .toCharArray ();      私人URL网址;   私人HttpURLConnection康涅狄格州;   私人字符串边界=零;   私人Map<字符串,String>textParams=new HashMap<字符串,String> ();   私人Map<字符串,File>fileparams=new HashMap<字符串,File> ();      公共HttpPostUtil(字符串url){抛出异常   这一点。url=new url(网址);   }//重新设置要请求的服务器地址,即上传文件的地址。   公共空间setUrl(字符串url){抛出异常   这一点。url=new url(网址);   }//增加一个普通字符串数据到形式表单数据中   公共空间addTextParameter(字符串名称字符串值){   textParams。把(名称,值);   }//增加一个文件到形式表单数据中   公共空间addFileParameter(字符串名称,文件价值){   fileparams。把(名称,值);   }//清空所有已添加表的形式单数据   公共空间clearAllParameters () {   textParams.clear ();   fileparams.clear ();   }/* *   *发送数据到服务器   *   * @return一个字节包含服务器的返回结果的数组   * @throws例外   */公共byte[]发送()抛出异常{   initConnection ();   尝试{   conn.connect ();   }捕捉(SocketTimeoutException e) {   抛出新的异常(e);   }      OutputStream connOutStream=new DataOutputStream (   conn.getOutputStream ());      writeFileParams (connOutStream);   writeStringParams (connOutStream);   writesEnd (connOutStream);      InputStream responseInStream=conn.getInputStream ();   ByteArrayOutputStream responseOutStream=new ByteArrayOutputStream ();   int len;   byte [] bufferByte=新字节[1024];   在((len=responseInStream.read (bufferByte) !=1) {   responseOutStream。写(bufferByte 0 len);   }   responseInStream.close ();   connOutStream.close ();      conn.disconnect ();   byte [] resultByte=responseOutStream.toByteArray ();   responseOutStream.close ();   返回resultByte;   }//文件上传的连接的一些必须设置   私人空间initConnection()抛出异常{   StringBuffer buf=new StringBuffer (“- - - - -“);   随机兰德=new随机();   for (int i=0;我& lt;15;我+ +){   buf.append (MULTIPART_CHARS [rand.nextInt (MULTIPART_CHARS.length)]);   }   这一点。边界=buf.toString ();      康涅狄格州=(HttpURLConnection) this.url.openConnection ();   conn.setDoOutput(真正的);   conn.setUseCaches(假);   conn.setConnectTimeout (3 * 60 * 1000);//连接超时为10秒   conn.setRequestMethod (“POST");   conn.setRequestProperty (“Content-Type"   “多部分/格式;边界=?+边界);   }//普通字符串数据   私人空间writeStringParams (OutputStream)抛出异常{   Set键盘=textParams.keySet ();   (Iterator它=keySet.iterator ();it.hasNext ();) {   字符串名称=it.next ();   字符串值=https://www.yisu.com/zixun/textParams.get(名称);      出去了。写(边界(”——“+ +/r/n) .getBytes ());   出去了。写(“附加项:格式;name=" +名称+“/?r/n”)   .getBytes ());   out.write ((/r/n) .getBytes ());   out.write(编码(值)+/r/n) .getBytes ());   }   }//文件数据   私人空间writeFileParams (OutputStream)抛出异常{   设置<字符串>键盘=fileparams.keySet ();   (迭代器<字符串>它=keySet.iterator ();it.hasNext ();) {   字符串名称=it.next ();   文件价值=fileparams.get(名称);      出去了。写(边界(”——“+ +/r/n) .getBytes ());   出去了。写(“附加项:格式;name=/" +名字   +“/?文件名=/" +编码(value.getName ()) + "/r/n”)   .getBytes ());   出去了。写(“内容类型:“+ getContentType(值)+/r/n)   .getBytes ());   出去了。写((“Content-Transfer-Encoding:”+“二进制”+“/r/n”)   .getBytes ());      out.write ((/r/n) .getBytes ());      FileInputStream生态基=new FileInputStream(价值);   int字节=0;   byte [] bufferByte=新字节[1024];   在((字节=inStream.read (bufferByte)) !=1) {   出去了。写(bufferByte 0字节);   }   inStream.close ();      out.write ((/r/n) .getBytes ());   }   }//添加结尾数据   私人空间writesEnd (OutputStream)抛出异常{   出去了。写(边界(”——“+ +”——“+”/r/n) .getBytes ());   out.write ((/r/n) .getBytes ());   }//获取文件的上传类型,图片格式为图像/png图像/jpg等。非图片为应用程序/八进制   私人字符串getContentType (File f)抛出异常{   字符串文件名=f.getName ();   如果(fileName.endsWith (" . jpg ")) {   返回“图像/jpeg”;   }else if (fileName.endsWith (png)) {   返回“图像/png”;   }   返回“应用程序/八进制”;   }//对包含中文的字符串进行转码,此为utf - 8。服务器那边要进行一次解码   私人字符串编码(字符串值){抛出异常   URLEncoder返回。编码(价值,“utf - 8”);   }      }      

Java中的附件上传功能怎么利用HttpURLConnection实现