Java 發送http請求上傳文件功能案例
如何使用Java做一個http請求上傳文件的功能呢?下面是由本站小編爲大家整理的Java 發送http請求上傳文件功能案例,喜歡的可以收藏一下!瞭解更多詳情資訊,請關注應屆畢業生考試網!
具體代碼如下所示:
package lper;
import eredReader;
import InputStream;
import OutputStream;
import ;
import InputStream;
import ception;
import tStreamReader;
import utStream;
import URLConnection;
import ;
import onnection;
import ;
import ;
import y;
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 || pty()) {
return result;
}
URL realUrl = new URL(requestUrl);
URLConnection connection = Connection();
equestProperty("accept", "text/html, application/xhtml+xml, image/jxr, */*");
equestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");
if (requestHeader != null && () > 0) {
for (Entry<String, String> entry : ySet()) {
equestProperty(ey(), alue());
}
}
ect();
if (responseEncoding == null || pty()) {
responseEncoding = "UTF-8";
}
reader = new BufferedReader(new InputStreamReader(nputStream(), responseEncoding));
String line;
while ((line = Line()) != null) {
result += line;
}
} catch (Exception e) {
tln("發送GET請求出現異常!");
tStackTrace();
} finally {
try {
if (reader != null) {
e();
}
} catch (Exception e) {
tStackTrace();
}
}
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 || pty()) {
return result;
}
URL realUrl = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) Connection();
equestProperty("accept", "text/html, application/xhtml+xml, image/jxr, */*");
equestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0");
if (requestHeader != null && () > 0) {
for (Entry<String, String> entry : ySet()) {
equestProperty(ey(), alue());
}
}
oOutput(true);
oInput(true);
equestMethod("POST");
if (requestEncoding == null || pty()) {
requestEncoding = "UTF-8";
}
if (responseEncoding == null || pty()) {
responseEncoding = "UTF-8";
}
if (requestHeader != null && () > 0) {
for (Entry<String, String> entry : ySet()) {
equestProperty(ey(), alue());
}
}
if (files == null || () == 0) {
equestProperty("content-type", "application/x-www-form-urlencoded");
out = new DataOutputStream(utputStream());
if (formTexts != null && () > 0) {
String formData = "";
for (Entry<String, String> entry : ySet()) {
formData += ey() + "=" + alue() + "&";
}
formData = tring(0, th() - 1);
e(ring()ytes(requestEncoding));
}
} else {
String boundary = "-----------------------------" + eOf(new Date()ime());
equestProperty("content-type", "multipart/form-data; boundary=" + boundary);
out = new DataOutputStream(utputStream());
if (formTexts != null && () > 0) {
StringBuilder sbFormData = new StringBuilder();
for (Entry<String, String> entry : ySet()) {
nd("--" + boundary + "rn");
nd("Content-Disposition: form-data; name="" + ey() + ""rnrn");
nd(alue() + "rn");
}
e(ring()ytes(requestEncoding));
}
for (Entry<String, String> entry : ySet()) {
String fileName = ey();
String filePath = alue();
if (fileName == null || pty() || filePath == null || pty()) {
continue;
}
File file = new File(filePath);
if (!ts()) {
continue;
}
e(("--" + boundary + "rn")ytes(requestEncoding));
e(("Content-Disposition: form-data; name="" + fileName + ""; filename="" + ame() + ""rn")ytes(requestEncoding));
e(("Content-Type: application/x-msdownloadrnrn")ytes(requestEncoding));
DataInputStream in = new DataInputStream(new FileInputStream(file));
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = (bufferOut)) != -1) {
e(bufferOut, 0, bytes);
}
e();
e(("rn")ytes(requestEncoding));
}
e(("--" + boundary + "--")ytes(requestEncoding));
}
h();
e();
out = null;
reader = new BufferedReader(new InputStreamReader(nputStream(), responseEncoding));
String line;
while ((line = Line()) != null) {
result += line;
}
} catch (Exception e) {
tln("發送POST請求出現異常!");
tStackTrace();
} finally {
try {
if (out != null) {
e();
}
if (reader != null) {
e();
}
} catch (IOException ex) {
tStackTrace();
}
}
return result;
}
}
相關文章
-
java如何利用java.net.URLConnection發送HTTP請求
如何通過Java發送HTTP請求,通俗點講,如何通過Java(模擬瀏覽器)發送HTTP請求。下面是由本站小編爲大家整理的java如何利用onnection發送HTTP請求,喜歡的可以收藏一下!瞭解更多詳情資訊,請關注應屆畢業生考試網! 運用原生Jav -
關於java實現http請求工具類示例
通過http rest請求返回數據複製代碼 代碼如下:import ;import actory;import ValuePair;import ntProtocolException;import Client;import onseHandler;import ncodedFormEntity;import Get;import Post;import cRes -
JAVA如何獲取HTTP請求頭
在利用Java網絡編程時,常常需要獲取HTTP請求頭,那麼JAVA如何獲取HTTP請求頭呢?下面小編爲大家介紹了JAVA獲取HTTP請求頭的.方法,希望能幫到大家!在利用Java網絡編程時,利用Java獲取HTTP Request 和 Response頭字段,可以利用J -
Javascript實例教程如何使用HoTMetal
在日常學習、工作和生活中,說到作文,大家肯定都不陌生吧,作文是從內部言語向外部言語的過渡,即從經過壓縮的簡要的、自己能明白的語言,向開展的、具有規範語法結構的、能爲他人所理解的外部語言形式的轉化。怎麼寫作文才能 -
使用JavaScript實現Java的List功能
複製代碼 代碼如下:/*** js模擬java中的List*/var list = new Array();/*** 添加* @param {Object} object*/function add(object) {list[th] = object;}/*** 移除此列表中指定位置上的元素。* @param index 指定位置 -
java環境變量path和classpath的配置過程
小時候的偶一直在梁詠琪的歌聲中長大,是梁詠琪的頭髮粉絲。我已剪短我的發 剪斷了懲罰 剪一地傷透我的尷尬 反反覆覆清清楚楚 一刀兩斷你的情話 你的謊話,一曲《短髮》訴說着梁詠琪的情感,很好的詮釋者情侶分手時的心境, -
關於JAVA實現httpClient的實例
在平日的學習、工作和生活裏,大家都寫過作文,肯定對各類作文都很熟悉吧,寫作文可以鍛鍊我們的獨處習慣,讓自己的心靜下來,思考自己未來的方向。相信很多朋友都對寫作文感到非常苦惱吧,下面是小編幫大家整理的最開心的作文4 -
Java中hashmap和hashtable的區別
引導語:HashMap 和HashSet 是Java Collection Framework 的兩個重要成員,其中 HashMap 是Map 接口的常用實現類,HashSet 是Set 接口的常用實現類。以下是本站小編分享給大家的Java中hashmap和hashtable的區別,歡迎閱讀!1 -
分析JavaScript類型系統之Math
開門必讀math和其他對象不同,Math對象是一個靜態對象,而不是構造函數。實際上,Math只是一個由Javascript設置的對象命名空間,用於存儲數學函數 屬性Math.E 自然對數的底數,即常量e的值(約等於2.718) 派的值(約等於3.14159 -
Java Socket應答與HTTP服務器的關係
在平平淡淡的日常中,大家總免不了要接觸或使用作文吧,作文是由文字組成,經過人的思想考慮,通過語言組織來表達一個主題意義的文體。那麼一般作文是怎麼寫的呢?以下是小編收集整理的懂我的那個人作文,供大家參考借鑑,希望可以