没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论














Java 发送发送http请求上传文件功能实例请求上传文件功能实例
本文通过实例代码给大家介绍了Java 发送http请求上传文件功能,需要的朋友参考下吧
废话不多说了,直接给大家贴代码了,具体代码如下所示:
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 {
/**
* 发送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 || 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("发送GET请求出现异常!");
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
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 || 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");
资源评论


weixin_38728555
- 粉丝: 3
- 资源: 923
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
