加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java上传带图片的Http请求

发布时间:2020-12-15 03:19:56 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 /** * 上传带图片的http请求 * * @param murl网址 * @param map * 参数对 主要不要包括图片 * @param path * 图片路径 也可以是其他格式 自行做 * @re

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

/**
     * 上传带图片的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 + "rn");
                sb.append("Content-Disposition: form-data; name=""
                        + entry.getKey() + """ + "rn");
                sb.append("rn");
                sb.append(entry.getValue() + "rn");
 
            }
        }
 
        sb.append("--" + BOUNDARY + "rn");
        sb.append("Content-Disposition: form-data; name="image"; filename=""
                + filename + """ + "rn");
 
        sb.append("Content-Type: image/pjpeg" + "rn");
        sb.append("rn");
 
        byte[] before = sb.toString().getBytes("UTF-8");
        byte[] after = ("rn--" + BOUNDARY + "--rn").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,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,n));
        }
        return out.toString();
    }

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读