欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Java 发送http请求上传文件功能实例

程序员文章站 2023-12-18 12:27:22
废话不多说了,直接给大家贴代码了,具体代码如下所示: package wxapi.wxhelper; import java.io.bufferedreade...

废话不多说了,直接给大家贴代码了,具体代码如下所示:

package wxapi.wxhelper; 
import java.io.bufferedreader; 
import java.io.datainputstream; 
import java.io.dataoutputstream; 
import java.io.file; 
import java.io.fileinputstream; 
import java.io.ioexception; 
import java.io.inputstreamreader; 
import java.io.outputstream; 
import java.net.httpurlconnection; 
import java.net.url; 
import java.net.urlconnection; 
import java.util.date; 
import java.util.map; 
import java.util.map.entry; 
public class httprequestutil { 
  /** 
   * 发送get请求 
   * 
   * @param requesturl 
   *      请求url 
   * @param requestheader 
   *      请求头 
   * @param responseencoding 
   *      响应编码 
   * @return 页面响应html 
   */ 
  public static string sendget(string requesturl, map<string, string> requestheader, string responseencoding) { 
    string result = ""; 
    bufferedreader reader = null; 
    try { 
      if (requesturl == null || requesturl.isempty()) { 
        return result; 
      } 
      url realurl = new url(requesturl); 
      urlconnection connection = realurl.openconnection(); 
      connection.setrequestproperty("accept", "text/html, application/xhtml+xml, image/jxr, */*"); 
      connection.setrequestproperty("user-agent", "mozilla/5.0 (windows nt 10.0; wow64; rv:53.0) gecko/20100101 firefox/53.0"); 
      if (requestheader != null && requestheader.size() > 0) { 
        for (entry<string, string> entry : requestheader.entryset()) { 
          connection.setrequestproperty(entry.getkey(), entry.getvalue()); 
        } 
      } 
      connection.connect(); 
      if (responseencoding == null || responseencoding.isempty()) { 
        responseencoding = "utf-8"; 
      } 
      reader = new bufferedreader(new inputstreamreader(connection.getinputstream(), responseencoding)); 
      string line; 
      while ((line = reader.readline()) != null) { 
        result += line; 
      } 
    } catch (exception e) { 
      system.out.println("发送get请求出现异常!"); 
      e.printstacktrace(); 
    } finally { 
      try { 
        if (reader != null) { 
          reader.close(); 
        } 
      } catch (exception e) { 
        e.printstacktrace(); 
      } 
    } 
    return result; 
  } 
  /** 
   * 发送post请求 
   *  
   * @param requesturl 
   *      请求url 
   * @param requestheader 
   *      请求头 
   * @param formtexts 
   *      表单数据 
   * @param files 
   *      上传文件 
   * @param requestencoding 
   *      请求编码 
   * @param responseencoding 
   *      响应编码 
   * @return 页面响应html 
   */ 
  public static string sendpost(string requesturl, map<string, string> requestheader, map<string, string> formtexts, map<string, string> files, string requestencoding, string responseencoding) { 
    outputstream out = null; 
    bufferedreader reader = null; 
    string result = ""; 
    try { 
      if (requesturl == null || requesturl.isempty()) { 
        return result; 
      } 
      url realurl = new url(requesturl); 
      httpurlconnection connection = (httpurlconnection) realurl.openconnection(); 
      connection.setrequestproperty("accept", "text/html, application/xhtml+xml, image/jxr, */*"); 
      connection.setrequestproperty("user-agent", "mozilla/5.0 (windows nt 10.0; wow64; rv:53.0) gecko/20100101 firefox/53.0"); 
      if (requestheader != null && requestheader.size() > 0) { 
        for (entry<string, string> entry : requestheader.entryset()) { 
          connection.setrequestproperty(entry.getkey(), entry.getvalue()); 
        } 
      } 
      connection.setdooutput(true); 
      connection.setdoinput(true); 
      connection.setrequestmethod("post"); 
      if (requestencoding == null || requestencoding.isempty()) { 
        requestencoding = "utf-8"; 
      } 
      if (responseencoding == null || responseencoding.isempty()) { 
        responseencoding = "utf-8"; 
      } 
      if (requestheader != null && requestheader.size() > 0) { 
        for (entry<string, string> entry : requestheader.entryset()) { 
          connection.setrequestproperty(entry.getkey(), entry.getvalue()); 
        } 
      } 
      if (files == null || files.size() == 0) { 
        connection.setrequestproperty("content-type", "application/x-www-form-urlencoded"); 
        out = new dataoutputstream(connection.getoutputstream()); 
        if (formtexts != null && formtexts.size() > 0) { 
          string formdata = ""; 
          for (entry<string, string> entry : formtexts.entryset()) { 
            formdata += entry.getkey() + "=" + entry.getvalue() + "&"; 
          } 
          formdata = formdata.substring(0, formdata.length() - 1); 
          out.write(formdata.tostring().getbytes(requestencoding)); 
        } 
      } else { 
        string boundary = "-----------------------------" + string.valueof(new date().gettime()); 
        connection.setrequestproperty("content-type", "multipart/form-data; boundary=" + boundary); 
        out = new dataoutputstream(connection.getoutputstream()); 
        if (formtexts != null && formtexts.size() > 0) { 
          stringbuilder sbformdata = new stringbuilder(); 
          for (entry<string, string> entry : formtexts.entryset()) { 
            sbformdata.append("--" + boundary + "\r\n"); 
            sbformdata.append("content-disposition: form-data; name=\"" + entry.getkey() + "\"\r\n\r\n"); 
            sbformdata.append(entry.getvalue() + "\r\n"); 
          } 
          out.write(sbformdata.tostring().getbytes(requestencoding)); 
        } 
        for (entry<string, string> entry : files.entryset()) { 
          string filename = entry.getkey(); 
          string filepath = entry.getvalue(); 
          if (filename == null || filename.isempty() || filepath == null || filepath.isempty()) { 
            continue; 
          } 
          file file = new file(filepath); 
          if (!file.exists()) { 
            continue; 
          } 
          out.write(("--" + boundary + "\r\n").getbytes(requestencoding)); 
          out.write(("content-disposition: form-data; name=\"" + filename + "\"; filename=\"" + file.getname() + "\"\r\n").getbytes(requestencoding)); 
          out.write(("content-type: application/x-msdownload\r\n\r\n").getbytes(requestencoding)); 
          datainputstream in = new datainputstream(new fileinputstream(file)); 
          int bytes = 0; 
          byte[] bufferout = new byte[1024]; 
          while ((bytes = in.read(bufferout)) != -1) { 
            out.write(bufferout, 0, bytes); 
          } 
          in.close(); 
          out.write(("\r\n").getbytes(requestencoding)); 
        } 
        out.write(("--" + boundary + "--").getbytes(requestencoding)); 
      } 
      out.flush(); 
      out.close(); 
      out = null; 
      reader = new bufferedreader(new inputstreamreader(connection.getinputstream(), responseencoding)); 
      string line; 
      while ((line = reader.readline()) != null) { 
        result += line; 
      } 
    } catch (exception e) { 
      system.out.println("发送post请求出现异常!"); 
      e.printstacktrace(); 
    } finally { 
      try { 
        if (out != null) { 
          out.close(); 
        } 
        if (reader != null) { 
          reader.close(); 
        } 
      } catch (ioexception ex) { 
        ex.printstacktrace(); 
      } 
    } 
    return result; 
  } 
} 

以上所述是小编给大家介绍的java 发送http请求上传文件功能实例,希望对大家有所帮助

上一篇:

下一篇: