java 图片请求_Java上传带图片的Http请求

/**

* 上传带图片的http请求

*

* @param murl网址

* @param map

* 参数对 主要不要包括图片

* @param path

* 图片路径 也可以是其他格式 自行做

* @return

* @throws Exception

*/

static public String post(String murl, HashMap map,

String path) throws Exception {

File file = new File(path);

String filename = path.substring(path.lastIndexOf("/"));

// String filename = Str.md5(path);

StringBuilder sb = new StringBuilder();

if (null != map) {

for (Map.Entry entry : map.entrySet()) {

sb.append("--" + BOUNDARY + "\r\n");

sb.append("Content-Disposition: form-data; name=\""

+ entry.getKey() + "\"" + "\r\n");

sb.append("\r\n");

sb.append(entry.getValue() + "\r\n");

}

}

sb.append("--" + BOUNDARY + "\r\n");

sb.append("Content-Disposition: form-data; name=\"image\"; filename=\""

+ filename + "\"" + "\r\n");

sb.append("Content-Type: image/pjpeg" + "\r\n");

sb.append("\r\n");

byte[] before = sb.toString().getBytes("UTF-8");

byte[] after = ("\r\n--" + BOUNDARY + "--\r\n").getBytes("UTF-8");

URL url = new URL(murl);

HttpURLConnection conn = (HttpURLConnection) url.openConnection();

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type",

"multipart/form-data; boundary=" + BOUNDARY);

conn.setRequestProperty("Authorization",

"Bearer " + Douban.getAccessToken());

conn.setRequestProperty("Content-Length",

String.valueOf(before.length + file.length() + after.length));

conn.setRequestProperty("HOST", url.getHost());

conn.setDoOutput(true);

OutputStream out = conn.getOutputStream();

InputStream in = new FileInputStream(file);

out.write(before);

byte[] buf = new byte[1024];

int len;

while ((len = in.read(buf)) != -1)

out.write(buf, 0, len);

out.write(after);

in.close();

out.close();

MLog.e(inputStream2String(conn.getInputStream()) + "");

return conn.getContent().toString();

}

/**

* is转String

*

* @param in

* @return

* @throws IOException

*/

public static String inputStream2String(InputStream in) throws IOException {

StringBuffer out = new StringBuffer();

byte[] b = new byte[4096];

for (int n; (n = in.read(b)) != -1;) {

out.append(new String(b, 0, n));

}

return out.toString();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java可以使用HttpURLConnection或者HttpClient来发送get和post请求到微信接口。以下是示例代码: 使用HttpURLConnection发送get请求: ```java URL url = new URL("https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.connect(); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } else { System.out.println("GET request failed, response code: " + responseCode); } ``` 使用HttpURLConnection发送post请求: ```java String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN"; URL obj = new URL(url); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("POST"); // 设置post请求参数 String urlParameters = "json data..."; con.setDoOutput(true); DataOutputStream wr = new DataOutputStream(con.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); int responseCode = con.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); System.out.println(response.toString()); } else { System.out.println("POST request failed, response code: " + responseCode); } ``` 使用HttpClient发送get请求: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); HttpGet httpGet = new HttpGet("https://api.weixin.qq.com/cgi-bin/user/get?access_token=ACCESS_TOKEN&next_openid=NEXT_OPENID"); CloseableHttpResponse response = httpClient.execute(httpGet); try { HttpEntity entity = response.getEntity(); String responseBody = EntityUtils.toString(entity, "UTF-8"); EntityUtils.consume(entity); System.out.println(responseBody); } finally { response.close(); } ``` 使用HttpClient发送post请求: ```java CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN"); // 设置post请求参数 StringEntity entity = new StringEntity("json data...", ContentType.APPLICATION_JSON); httpPost.setEntity(entity); CloseableHttpResponse response = httpClient.execute(httpPost); try { HttpEntity responseEntity = response.getEntity(); String responseBody = EntityUtils.toString(responseEntity, "UTF-8"); EntityUtils.consume(responseEntity); System.out.println(responseBody); } finally { response.close(); } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值