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();
        }
    }
相关推荐
比特 GOK2 分钟前
Qt项目ui文件中新添加的控件在代码中不识别的问题解决
开发语言·qt·ui
云天AI实战派6 分钟前
Agent 全流程实战:用 Python 搭建技能路由智能体,落地小龙虾门店运营助手
开发语言·人工智能·python
rit843249917 分钟前
基于遗传算法的电动汽车充电站选址优化:模型与MATLAB实现
开发语言·matlab
Rust研习社20 分钟前
你为什么总是入门 Rust 失败
开发语言·后端·rust
逸Y 仙X23 分钟前
文章二十四:Elasticsearch查询排序应用实战e
java·大数据·数据库·elasticsearch·搜索引擎·全文检索
我滴老baby41 分钟前
工具调用全景解析从Function Calling到MCP协议的完整实践
开发语言·人工智能·python·架构·fastapi
小李子呢021141 分钟前
前端八股JS---Map / Set / WeakMap / WeakSet
开发语言·前端·javascript
feifeigo12343 分钟前
自适应大邻域搜索(ALNS)算法的MATLAB 实现
开发语言·算法·matlab
沐知全栈开发1 小时前
API 类别 - 实用工具
开发语言
Cx330❀1 小时前
Qt 入门指南:从零搭建开发环境到第一个图形界面程序
xml·大数据·开发语言·网络·c++·人工智能·qt