/*** 上传带图片的http请求* * @param murl网址* @param map* 参数对 主要不要包括图片* @param path* 图片路径 也可以是其他格式 自行做* @return* @throws Exception*/static public String post(String murl, HashMap<String, String> 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<String, String> 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();}
详细解决方案
上传带图片的Http请求
热度:78 发布时间:2024-01-21 16:17:00.0
相关解决方案
- ,http 200状态码
- tomcat新手有关问题 HTTP Status 404 - /manager/
- http 传送数据 每个包有大小限制码?解决办法
- 急关于Windows Server 2008 的 iis 7 下的aspx页面“HTTP 异常 404.3 - Not Found”异常
- HTTP 异常 405.0 - Method Not Allowed
- HTTP 异常 400 - Bad Request怎么解决
- Uploadify http error 500解决思路
- []http 500有关问题
- []小弟我上传的网站出现 HTTP 异常 403 - 禁止访问 ,咋回事啊
- HTTP 404。您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用。请检查以下 URL 并确保其拼写正确。该如何解决
- http 怎么获取重定向后的地址
- 关键字:MTK,DNS,HTTP,WAP,SOCKET,求高人点拨!该怎么处理
- 关于ibm http server6.1,该如何处理
- HTTP/1.1 503 服务不可用,没法收发邮件
- Tivoli Tam http-header 认证模式求教
- Tivoli Tam http-header 认证方式求教!该怎么解决
- HTTP 异常 500.100 - 内部服务器异常 - ASP 异常
- 自定义后缀名找不到文件 HTTP 异常 404
- http 快进快退 (IPTV存档节目回放的快进快退)解决办法
- [求助]HTTP Status 404 是什么问题?
- HTTP Status 500 - 请大家来看看啊
- [求助] HTTP STATUS 500-
- [求助]HTTP Status 500
- HTTP Status 404 问题
- HTTP Status 404 -/chinaHospitalSystem/
- HTTP method POST is not supported by this URL
- post http 405
- HTTP cache-control 治理
- HTTP 异常 401.1(401.2/401.3) - 未经授权:访问由于凭据无效被拒绝
- 网站打不开,揭示“HTTP/1.1 New Session Failed”