亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java 發(fā)送http請求上傳文件功能實例

 更新時間:2017年06月07日 10:25:10   作者:阿文丶  
本文通過實例代碼給大家介紹了Java 發(fā)送http請求上傳文件功能,需要的朋友參考下吧

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示:

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 { 
  /** 
   * 發(fā)送get請求 
   * 
   * @param requestUrl 
   *      請求url 
   * @param requestHeader 
   *      請求頭 
   * @param responseEncoding 
   *      響應(yīng)編碼 
   * @return 頁面響應(yīng)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("發(fā)送GET請求出現(xiàn)異常!"); 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (reader != null) { 
          reader.close(); 
        } 
      } catch (Exception e) { 
        e.printStackTrace(); 
      } 
    } 
    return result; 
  } 
  /** 
   * 發(fā)送post請求 
   *  
   * @param requestUrl 
   *      請求url 
   * @param requestHeader 
   *      請求頭 
   * @param formTexts 
   *      表單數(shù)據(jù) 
   * @param files 
   *      上傳文件 
   * @param requestEncoding 
   *      請求編碼 
   * @param responseEncoding 
   *      響應(yīng)編碼 
   * @return 頁面響應(yīng)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("發(fā)送POST請求出現(xiàn)異常!"); 
      e.printStackTrace(); 
    } finally { 
      try { 
        if (out != null) { 
          out.close(); 
        } 
        if (reader != null) { 
          reader.close(); 
        } 
      } catch (IOException ex) { 
        ex.printStackTrace(); 
      } 
    } 
    return result; 
  } 
} 

以上所述是小編給大家介紹的Java 發(fā)送http請求上傳文件功能實例,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

  • ActiveMQ中consumer的消息確認(rèn)機制詳解

    ActiveMQ中consumer的消息確認(rèn)機制詳解

    這篇文章主要介紹了ActiveMQ中consumer的消息確認(rèn)機制詳解,對于broker而言,只有接收到確認(rèn)指令,才會認(rèn)為消息被正確的接收或者處理成功了,InforSuiteMQ提供以下幾種Consumer與Broker之間的消息確認(rèn)方式,需要的朋友可以參考下
    2023-10-10
  • Struts2 OGNL表達(dá)式實例詳解

    Struts2 OGNL表達(dá)式實例詳解

    這篇文章主要介紹了Struts2 OGNL表達(dá)式實例詳解,相關(guān)實例代碼,需要的朋友可以參考。
    2017-09-09
  • IDEA導(dǎo)入jar包的完整實現(xiàn)步驟

    IDEA導(dǎo)入jar包的完整實現(xiàn)步驟

    由于導(dǎo)入jar包項目存在很多不確定的問題,導(dǎo)致每次都需要調(diào)試、配置好多遍,對此特意記錄下來,這篇文章主要給大家介紹了關(guān)于IDEA導(dǎo)入jar包的相關(guān)資料,需要的朋友可以參考下
    2024-01-01
  • java用LocalDateTime類獲取當(dāng)天時間、前一天時間及本周/本月的開始和結(jié)束時間

    java用LocalDateTime類獲取當(dāng)天時間、前一天時間及本周/本月的開始和結(jié)束時間

    這篇文章主要給大家介紹了關(guān)于java使用LocalDateTime類獲取當(dāng)天時間、前一天時間及本周/本月的開始和結(jié)束時間的相關(guān)資料,文中通過代碼示例介紹的非常詳細(xì),需要的朋友可以參考下
    2023-08-08
  • Java中的system.getProperty()的作用及使用方法

    Java中的system.getProperty()的作用及使用方法

    System.getProperty()?方法用于獲取系統(tǒng)屬性的值,該方法接受一個字符串參數(shù),表示要獲取的系統(tǒng)屬性的名稱,返回值為字符串類型,表示該屬性的值,接下來通過本文給大家介紹Java中的system.getProperty()的作用及使用方法,感興趣的朋友跟隨小編一起看看吧
    2023-05-05
  • Springboot如何使用Map將錯誤提示輸出到頁面

    Springboot如何使用Map將錯誤提示輸出到頁面

    這篇文章主要介紹了Springboot如何使用Map將錯誤提示輸出到頁面,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-08-08
  • 詳解在Spring Boot框架下使用WebSocket實現(xiàn)消息推送

    詳解在Spring Boot框架下使用WebSocket實現(xiàn)消息推送

    這篇文章主要介紹了詳解在Spring Boot框架下使用WebSocket實現(xiàn)消息推送,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2016-12-12
  • 總結(jié)Java常用加解密方法AES?SHA1?md5

    總結(jié)Java常用加解密方法AES?SHA1?md5

    這篇文章主要為大家介紹了Java常用加密方法AES?SHA1?md5總結(jié)及示例demo,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-06-06
  • SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法

    SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法

    這篇文章主要介紹了SpringCloud 服務(wù)負(fù)載均衡和調(diào)用 Ribbon、OpenFeign的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-09-09
  • SpringMVC 方法四種類型返回值總結(jié)(你用過幾種)

    SpringMVC 方法四種類型返回值總結(jié)(你用過幾種)

    這篇文章主要介紹了SpringMVC 方法四種類型返回值總結(jié)(你用過幾種),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05

最新評論