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 Class文件是什麼
Class類是在Java語言中定義一個特定類的實現。一個類的定義包含成員變量,成員方法,還有這個類實現的接口,以及這個類的父類。Class類的對象用於表示當前運行的Java應用程序中的類和接口。比如:每個數組均屬於一個Class類對象,所有具有相同元素類型和維數的數組共享...
-
編寫更好的Java單元測試的7個技巧
測試是開發的一個非常重要的方面,可以在很大程度上決定一個應用程序的命運。良好的測試可以在早期捕獲導致應用程序崩潰的問題,但較差的測試往往總是導致故障和停機。雖然有三種主要類型的軟件測試:單元測試,功能測試和集成測試,但是在這篇博文中,我們將討論開發人員...
-
Java多線程知識點
引導語;多線程是指從軟件或者硬件上實現多個線程併發執行的技術。以下是本站小編分享給大家的Java多線程知識點,歡迎閲讀!1.1多線程的概念多線程編程的含義是你可將程序任務分成幾個並行的子任務。特別是在網絡編程中,你會發現很多功能是可以併發執行的。比如網...
-
Java中嵌入式MySQL的使用方法介紹
這篇文件主要介紹在Java中嵌入式MySQL的使用,對於一些的應用項目,提供安裝版的Mysql,Oracle是必須的工作。但是有時候如果是一個小的工具,可安裝或者移植性比較強的小軟件。再去安裝數據庫可能就比較麻煩了。其實MySQL也有嵌入式的,不需要安裝,在使用的過程中,會自動...