/**
     * 上传带图片的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();
    }

java 中 HttpClient 和HttpURLConnection都可以用来发起Http请求。

美高梅电子游戏,HttpURLConnection

HttpURLConnection是一种多用途、轻量极的HTTP客户端,使用它来进行HTTP操作可以适用于大多数的应用程序。

  • 优点:API比较简单,更加容易地使用和扩展
  • 缺点:涉及Session、Cookie的处理时,使用HttpURLConnection
    来处理这些细节,处理难度较大

HttpClient

简单来说,HttpClient就是一个增强版的HttpURLConnection,HttpURLConnection可以做的事情HttpClient全部可以做;HttpURLConnection没有提供的有些功能,HttpClient也提供了,但它只是关注于如何发送请求、接收响应,以及管理HTTP连接

  • 缺点:
    由于HttpClient的API数量过多,使得我们很难在不破坏兼容性的情况下对它进行升级和扩展

Author

发表评论

电子邮件地址不会被公开。 必填项已用*标注

相关文章