java 调用第三方上传接口

该代码段展示了一个Java方法,通过HttpURLConnection发送POST请求上传文件。它设置了请求头信息,包括Content-Type为multipart/form-data,并分块上传文件内容。同时,方法处理了文件和参数的编码,以及连接的关闭。
摘要由CSDN通过智能技术生成
package block.mall.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;


@Slf4j
public class UploadFile {
    /**

     * 调用流程上传文件接口上传文件

     * @param url

     * @param path

     * @return

     */

    public static String sendPostUplodFile(String url,String path) {

        DataOutputStream out = null;

        BufferedReader in = null;

        String result = "";

        try {

            URL realUrl = new URL(url);

            //打开和URL之间的连接

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

            //发送POST请求必须设置如下两行

            conn.setDoOutput(true);

            conn.setDoInput(true);

            String BOUNDARY = "----WebKitFormBoundary07I8UIuBx6LN2KyY";

            conn.setUseCaches(false);

            conn.setRequestMethod("POST");

            conn.setRequestProperty("connection", "Keep-Alive");

            //            conn.setRequestProperty("user-agent", "Mozilla/4.0 (conpatible; MSIE 6.0; Windows NT 5.1; SV1)");

            conn.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36");

            conn.setRequestProperty("Charsert", "UTF-8");

            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);

            conn.connect();

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

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

            //添加参数

            StringBuffer sb1 = new StringBuffer();

            sb1.append("--");

            sb1.append(BOUNDARY);

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

            sb1.append("Content-Disposition: form-data;name=\"luid\"");

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

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

            sb1.append("123");

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

            out.write(sb1.toString().getBytes());

            //添加参数file

            File file = new File(path);

            StringBuffer sb = new StringBuffer();

            sb.append("--");

            sb.append(BOUNDARY);

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

            sb.append("Content-Disposition: form-data;name=\"file\";filename=\"" + file.getName() + "\"");

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

            sb.append("Content-Type: application/octet-stream");

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

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

            out.write(sb.toString().getBytes());

            DataInputStream in1 = new DataInputStream(new FileInputStream(file));

            int bytes = 0;

            byte[] bufferOut = new byte[1024];

            while ((bytes = in1.read(bufferOut)) != -1) {

                out.write(bufferOut,0,bytes);

            }

            out.write("\r\n".getBytes());

            in1.close();

            out.write(end_data);

            //flush输出流的缓冲

            out.flush();

            //定义BufferedReader输入流来读取URL的响应

            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));

            String line;

            while ((line = in.readLine()) != null) {

                result += line;

            }

        } catch (Exception e) {

            // TODO Auto-generated catch block

            log.error("发送POST请求出现异常" + e);

            e.printStackTrace();

        }finally {

            try {

                if (out != null) {

                    out.close();

                }

                if (in != null) {

                    in.close();

                }

            } catch (Exception ex) {

                // TODO: handle exception

                ex.printStackTrace();

            }

        }

        return result;

    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值