Java Word文档发送给外部文件上传API

java 复制代码
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.*;

/**
     * 请求外部文件上传接口方法
     * @author hjj
     */
    public static String uploadFile(String targetUrl,MultipartFile file,String knowledgeBaseName) throws IOException {
        String boundary = Long.toHexString(System.currentTimeMillis()); // 边界字符串
        String CRLF = "\r\n"; // 换行符
        URL url = new URL(targetUrl);
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
        httpConn.setUseCaches(false);
        httpConn.setDoOutput(true); // 使用 URL 连接进行输出
        httpConn.setDoInput(true); // 使用 URL 连接进行输入
        httpConn.setRequestMethod("POST");
        httpConn.setRequestProperty("Connection", "Keep-Alive");
        httpConn.setRequestProperty("User-Agent", "Java Multipart Upload Client");
        httpConn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);

        try (
                OutputStream output = httpConn.getOutputStream();
                PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, "UTF-8"), true);
        ) {
            // 发送文件
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"files\"; filename=\"" + file.getOriginalFilename() + "\"").append(CRLF);
            writer.append("Content-Type: " + file.getContentType()).append(CRLF);
            writer.append(CRLF).flush();

            InputStream fileInputStream = file.getInputStream();
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = fileInputStream.read(buffer)) != -1) {
                output.write(buffer, 0, bytesRead);
            }
            output.flush();
            fileInputStream.close();
            writer.append(CRLF).flush();

            // 发送knowledge_base_name字段
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"knowledge_base_name\"").append(CRLF);
            writer.append(CRLF).append(knowledgeBaseName).append(CRLF).flush();

            // 发送override字段
            writer.append("--" + boundary).append(CRLF);
            writer.append("Content-Disposition: form-data; name=\"override\"").append(CRLF);
            writer.append(CRLF).append(String.valueOf(true)).append(CRLF).flush();

            // 请求结束
            writer.append("--" + boundary + "--").append(CRLF).flush();
        }
        // 获取响应代码
        int responseCode = httpConn.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            BufferedReader in = new BufferedReader(new InputStreamReader(httpConn.getInputStream()));
            String inputLine;
            StringBuffer response = new StringBuffer();
            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
            }
            in.close();
            // print result
            System.out.println(response.toString());
            return response.toString();
            // OK
        } else {
            // 处理错误
            return httpConn.getResponseMessage();
        }
    }
相关推荐
S***26751 分钟前
Spring Boot环境配置
java·spring boot·后端
Dev7z1 分钟前
基于Matlab的多制式条形码识别与图形界面(GUI)系统设计与实现
开发语言·matlab
6***83052 分钟前
什么是Spring Boot 应用开发?
java·spring boot·后端
合作小小程序员小小店2 分钟前
桌面开发,在线%信息管理%系统,基于vs2022,c#,winform,sql server数据。
开发语言·数据库·sql·microsoft·c#
FL16238631293 分钟前
ONNX RuntimeC++ 静态库下载安装和使用教程
开发语言·c++
星释4 分钟前
Rust 练习册 95:React与响应式编程
开发语言·react.js·rust
Evand J6 分钟前
【MATLAB例程】3D雷达-IMU融合定位系统(基于扩展卡尔曼滤波)|雷达观测距离、俯仰角、方向角,IMU包括6维(加速度与角速度)。附下载链接
开发语言·matlab·跟踪·雷达观测·三维定位·ekf滤波
毕设源码柳学姐7 分钟前
计算机毕设 java 智慧社区服务系统 SSM 框架社区生活平台 Java 开发的便民服务与互动系统
java·开发语言·生活
U***l83210 分钟前
【postgresql】分区表管理
java·数据库·postgresql
倚肆10 分钟前
MyBatis-Plus Mapper 接口方法详解
java·mybatis