中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

在Java項目中如果發送http請求實現文件上傳功能

發布時間:2020-11-23 16:22:35 來源:億速云 閱讀:278 作者:Leah 欄目:編程語言

在Java項目中如果發送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 { 
  /** 
   * 發送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請求實現文件上傳功能的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

苏尼特右旗| 洪江市| 尼木县| 德庆县| 房山区| 富蕴县| 盐源县| 安阳县| 沁源县| 威信县| 张家港市| 怀远县| 潜山县| 广昌县| 乌审旗| 石阡县| 枣庄市| 香格里拉县| 衡阳市| 三都| 黎城县| 郯城县| 怀集县| 京山县| 临桂县| 昌宁县| 赫章县| 林州市| 兰西县| 泰顺县| 鄂托克前旗| 东辽县| 南陵县| 时尚| 临泽县| 麻江县| 泸溪县| 全南县| 板桥市| 博兴县| 塔城市|