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程序员面试题集50道
本文是本站小编搜索整理的关于Java程序员面试题集50道,涵盖内容全面,包括数据结构和算法相关的题目、经典面试编程题等,对hashCode方法的设计、垃圾收集的堆和代进行剖析,供参考学习,感兴趣的小伙伴们可以参考一下!想了解更多相关信息请持续关注我们应届毕业生考试...
-
Java运算符
计算机的最基本用途之一就是执行数学运算,作为一门计算机语言,Java也提供了一套丰富的运算符来操纵变量。欢迎大家阅读!更多相关信息请关注相关栏目!Java中的运算符和C/C++相差无几。数学运算符数学运算,结果为一个数值。见下表:关系运算符关系运算符,结果为一个布尔...
-
Java语言中substr和substring的区别
由于在项目中有需要对字符串进行截取,然后手残使用了IDE自动提示的substr,没想那么多以为substr和substring没多大区别。然而并不是,且听我一一道来。1.substr(index,length)从起始索引号提取字符串中指定数目的`字符。substr方法可以传入两个参数index和length。...