java http post图片

java http post图片文件,参考文章https://blog.csdn.net/yjclsx/article/details/70675057

 /**
     * 上传文件到指定地址URL
     *
     * @param fileName 文件名
     * @param urlStr   上传地址
     * @return
     */
    public static String uploadFile(String urlStr, String deviceId, File file) {
        try {
            // 换行符
            final String NEWLINE = "\r\n";
            final String BOUNDARY = "----brujarBoundaryari0emH33oMihIU4";

            // 服务器的上传地址
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();

            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            // 设置请求头参数
            conn.setRequestMethod("POST");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36");
            conn.connect();

            OutputStream out = new DataOutputStream(conn.getOutputStream());

            // 添加参数
            StringBuilder sb1 = new StringBuilder();
            sb1.append("--");
            sb1.append(BOUNDARY);
            sb1.append(NEWLINE);
            sb1.append("Content-Disposition: form-data; name=\"deviceId\"");
            sb1.append(NEWLINE);
            sb1.append(NEWLINE);
            sb1.append(deviceId);
            sb1.append(NEWLINE);
            out.write(sb1.toString().getBytes());

            // 文件参数
            StringBuilder sb = new StringBuilder();
            sb.append("--");
            sb.append(BOUNDARY);
            sb.append(NEWLINE);
            sb.append("Content-Disposition: form-data; name=\"file\"; filename=\"" + file.getName() + "\"");
            sb.append(NEWLINE);
            sb.append("Content-Type:application/octet-stream");
            sb.append(NEWLINE);
            sb.append(NEWLINE);
            // 将参数头的数据写入到输出流中
            out.write(sb.toString().getBytes());

            // 数据输入流,用于读取文件数据
            DataInputStream in = new DataInputStream(new FileInputStream(file));
            byte[] bufferOut = new byte[2048];
            int bytes = 0;
            // 每次读2KB数据,并且将文件数据写入到输出流中
            while ((bytes = in.read(bufferOut)) != -1) {
                out.write(bufferOut, 0, bytes);
            }
            // 最后添加换行
            out.write(NEWLINE.getBytes());
            in.close();
            // 定义最后数据分隔线,即--加上BOUNDARY再加上--。
            byte[] end_data = (NEWLINE + "--" + BOUNDARY + "--" + NEWLINE).getBytes();
            // 写上结尾标识
            out.write(end_data);
            out.flush();
            out.close();

            // 定义BufferedReader输入流来读取URL的响应
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    conn.getInputStream(), "UTF-8"));
            StringBuffer buffer = new StringBuffer();
            String line = null;
            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }
            return buffer.toString();
        } catch (Exception e) {
            System.out.println("发送POST请求出现异常!" + e);
            e.printStackTrace();
        }
        return "";
    }


    public static void main(String[] args) {
        String url = "http://127.0.0.1:8880/api";
        String filePath = "D:\\11.jpg";
        String result = uploadFile(url, "hgfuuyitygcfyri", new File(filePath));
        System.out.println(result);
    }

打印结果,成功会返回图片预览地址,以后将屏蔽预览地址,不要做其他用途

你可能感兴趣的:(JAVA)