Java 發(fā)送http請(qǐng)求上傳文件功能實(shí)例
廢話不多說(shuō)了,直接給大家貼代碼了,具體代碼如下所示:
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請(qǐng)求
*
* @param requestUrl
* 請(qǐng)求url
* @param requestHeader
* 請(qǐng)求頭
* @param responseEncoding
* 響應(yīng)編碼
* @return 頁(yè)面響應(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請(qǐng)求出現(xiàn)異常!");
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return result;
}
/**
* 發(fā)送post請(qǐng)求
*
* @param requestUrl
* 請(qǐng)求url
* @param requestHeader
* 請(qǐng)求頭
* @param formTexts
* 表單數(shù)據(jù)
* @param files
* 上傳文件
* @param requestEncoding
* 請(qǐng)求編碼
* @param responseEncoding
* 響應(yīng)編碼
* @return 頁(yè)面響應(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請(qǐng)求出現(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請(qǐng)求上傳文件功能實(shí)例,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- JAVA發(fā)送HTTP請(qǐng)求,返回HTTP響應(yīng)內(nèi)容,應(yīng)用及實(shí)例代碼
- JAVA通過(guò)HttpClient發(fā)送HTTP請(qǐng)求的方法示例
- java發(fā)送http請(qǐng)求并獲取狀態(tài)碼的簡(jiǎn)單實(shí)例
- 詳解Java發(fā)送HTTP請(qǐng)求
- java利用java.net.URLConnection發(fā)送HTTP請(qǐng)求的方法詳解
- URLConnection發(fā)送HTTP請(qǐng)求的方法_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
- Java 發(fā)送http請(qǐng)求(get、post)的示例
- Java 如何使用Feign發(fā)送HTTP請(qǐng)求
- Java發(fā)送http請(qǐng)求的示例(get與post方法請(qǐng)求)
- JAVA發(fā)送HTTP請(qǐng)求的四種方式總結(jié)
相關(guān)文章
java執(zhí)行SQL語(yǔ)句實(shí)現(xiàn)查詢的通用方法詳解
這篇文章主要介紹了java執(zhí)行SQL語(yǔ)句實(shí)現(xiàn)查詢的通用方法詳解,具有一定借鑒價(jià)值,需要的朋友可以參考下。2017-12-12
java?ThreadPoolExecutor線程池內(nèi)部處理流程解析
這篇文章主要為大家介紹了java?ThreadPoolExecutor線程池內(nèi)部處理流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12
SpringBoot整合第三方技術(shù)的實(shí)現(xiàn)
本文主要介紹了SpringBoot整合第三方技術(shù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
dom4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇dom4j創(chuàng)建和解析xml文檔的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
java生成pdf表格,調(diào)用itext創(chuàng)建的實(shí)例
這篇文章主要介紹了java生成pdf表格,調(diào)用itext創(chuàng)建的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01

