Java 发送http请求上传文件功能案例
来源:才华咖 本文已影响8.68K人
来源:才华咖 本文已影响8.68K人
如何使用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;
}
}
php和javascript之间变量的传递实现代码
关于JAVA实现httpClient的实例
Java语言第一个javaapplet
java环境变量path和classpath的配置过程
使用JavaScript实现Java的List功能
Java Socket应答与HTTP服务器的关系
JavaScript中push(),join() 函数实例详解
java环境变量path设置
javascript中的match函数用法简介
JavaScript监听textarea中按键事件
PPT职称考试“Shockwave Flash Object”控件法
Java中hashmap和hashtable的区别
java环境变量设置path
分析JavaScript类型系统之Math
Python和JavaScript语言转换的常用工具
Java文件解压缩示例
Java软件开发实习报告
JavaWeb如何实现文件上传下载功能
Java软件开发工程师求职简历范文
解析ajax请求post和get的区别
Java中基于Aspectwerkz的AOP开发
JavaScript插件化开发方式
java软件工程师求职信
Happy Dragon Boat Festival英语作文
java与javascript之间json格式数据互转详解
Javascript中函数名.length属性用法
JAVA认证基础知识:JavaNativeInterface学习小结
PHP+jQuery+Ajax仿淘宝多上传按钮单文件上传
关于javascript对象之内置和对象Math的使用方法
Java与JavaScript的区别
在JavaScript中操作数组之map()方法的使用示例
javascript全选与全取消功能的代码
PHP和Java的功能
PHP+Ajax+lottery.js抽奖
Javascript到PHP加密通讯的简单实现
php动态生成JavaScript代码
JavaScript插件化开发讲解
JavaScript与Java的区别
Java Ajax jsonp跨域请求详解
JavaScript中的with关键字