* @return HTTP响应的字符串
*/
public static String doJsonPost(String reqUrl, Map<String, String> parameters, String jsonData) {
HttpURLConnection url_con = null;
String responseContent = null;
try {
String params = getMapParamsToStr(parameters, WeiXinConstant.REQUEST_ENCODING);
URL url = new URL(reqUrl + "&" + params);
url_con = (HttpURLConnection) url.openConnection();
url_con.setRequestMethod(WeiXinConstant.REQUEST_POST);
url_con.setConnectTimeout(
HttpRequestProxy.connectTimeOut);// (单位:毫秒)
url_con.setReadTimeout(
HttpRequestProxy.readTimeOut);// (单位:毫秒)
url_con.setDoOutput(true);
url_con.setRequestProperty("Accept", "application/json"); // 设置接收数据的格式
url_con.setRequestProperty("Content-Type", "application/json"); // 设置发送数据的格式
OutputStreamWriter out = new OutputStreamWriter(url_con.getOutputStream(), WeiXinConstant.REQUEST_ENCODING);
out.write(jsonData);
out.flush();
out.close();
InputStream in = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(in, WeiXinConstant.REQUEST_ENCODING));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
String crlf = System.getProperty("line.separator");
while (tempLine != null) {
tempStr.append(tempLine);
tempStr.append(crlf);
tempLine = rd.readLine();
}
responseContent = tempStr.toString();
rd.close();
in.close();
} catch (IOException e) {
} finally {
if (url_con != null) {
url_con.disconnect();
}
}
return responseContent;
}
/**
* post 文件的封装
* @param reqUrl HTTP请求URL
* @param parameters 参数映射表
* @param encoding 编码
* @param fileIn 数据流
* @param file 文件
* @param contentType 传输类型
* @param name 传输名称
* @return 响应结果
*/
public static String uploadMedia(String reqUrl, Map<String, String> parameters, String encoding,
InputStream fileIn, File file, String contentType,String name) {
HttpURLConnection url_con = null;
String responseContent = null;
try {
// 设置边界
String BOUNDARY = "----------" + System.currentTimeMillis();
String params = getMapParamsToStr(parameters, WeiXinConstant.REQUEST_ENCODING);
URL urlObj = new URL(reqUrl + "&" + params.toString());
// 连接
url_con = (HttpURLConnection) urlObj.openConnection();
/**
* 设置关键值
*/
url_con.setRequestMethod(
WeiXinConstant.REQUEST_POST); // 以Post方式提交表单,默认get方式
url_con.setDoInput(true);
url_con.setDoOutput(true);
url_con.setUseCaches(false); // post方式不能使用缓存
// 设置请求头信息
url_con.setRequestProperty("Connection", "Keep-Alive");
url_con.setRequestProperty("Charset", encoding);
// 设置边界
url_con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
// 请求正文信息
// 第一部分:
StringBuilder sb = new StringBuilder();
sb.append("--"); // 必须多两道线
sb.append(BOUNDARY);
sb.append("rn");
sb.append("Content-Disposition: form-data;name=""+name+"";filelength="" + file.length() + "";filename=""
+ file.getName() + ""rn");
sb.append("Content-Type:application/octet-streamrnrn");
byte[] head = sb.toString().getBytes(encoding);
// 获得输出流
OutputStream out = new DataOutputStream(url_con.getOutputStream());
// 输出表头
out.write(head);
// 文件正文部分
// 把文件已流文件的方式 推入到url中
DataInputStream in = new DataInputStream(fileIn);
int bytes = 0;
byte[] bufferOut = new byte[1024];
while ((bytes = in.read(bufferOut)) != -1) {
out.write(bufferOut, 0, bytes);
}
in.close();
// 结尾部分
byte[] foot = ("rn--" + BOUNDARY + "--rn").getBytes(encoding);// 定义最后数据分隔线
out.write(foot);
out.flush();
out.close();
InputStream iddn = url_con.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(iddn, encoding));
String tempLine = rd.readLine();
StringBuffer tempStr = new StringBuffer();
推荐阅读
- SpringBoot的可视化接口开发工具
- BI数据可视化工具使用技巧
- 浅谈java中重写和重载的区别
- 网络扫描神器Nmap常用操作方法详解以及使用脚本爆破telnet密码
- 详解Java反序列化漏洞
- 你开发的系统到底可以支撑多少并发访问?
- JavaScript 如何检测文件的类型?
- 用 C# 开发自己的语音识别程序
- 可以在线看视频和文档的开源网盘系统kiftd部署和使用教程
- 使用 IDEA 解决 Java8 的数据流问题,用过的都说好