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中如何獲取Spring中配置的bean
Spring是一個分層的JavaSE/EEfull-stack(一站式)輕量級開源框架。在Java中如何獲取Spring中配置的'bean?下面本站小編帶大家一起來看看詳細操作,希望對大家有所幫助!想了解更多相關資訊請持續關注我們應屆畢業生考試網!一、什麼是Spring?Spring是一個輕量級的控...
-
全國計算機等級《二級JAVA》命題預測試卷
以下是本站小編帶來的全國計算機等級《二級JAVA》命題預測試卷,歡迎學習!更多內容請關注應屆畢業生考試網!1、軟體生命週期是指()。A.軟體產品從提出、實現、使用維護到停止使用退役的過程B.軟體從需求分析、設計、實現到測試完成的過程,C.軟體的開發過程D.軟體...
-
Java語法基礎for語句練習
本文是本站小編搜尋整理的關於Java語法基礎for語句練習,供參考學習,希望對大家有所幫助!想了解更多相關資訊請持續關注我們應屆畢業生考試網!控制語句——for練習語句的巢狀應用累加求和,計數器迴圈巢狀一、語句的巢狀應用語句巢狀形式。其實就是語句中...
-
計算機二級JAVA考試構建JAVA程式2017
很多人一見到說要程式設計就以為很難,其實只有一點一滴學知識點也不難,下面是小編整理的計算機二級JAVA考試構建JAVA程式介紹,歡迎閱讀!第一個javaapplicationjava程式分為javaapplication(java應用程式)和javaapplet(java小應用程式)兩種。下面讓我們編寫一個java應...