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();
        }
    }
相关推荐
BD_Marathon2 小时前
【Flink】部署模式
java·数据库·flink
鼠鼠我捏,要死了捏5 小时前
深入解析Java NIO多路复用原理与性能优化实践指南
java·性能优化·nio
CodeCraft Studio5 小时前
3D文档控件Aspose.3D实用教程:使用 C# 构建 OBJ 到 U3D 转换器
开发语言·3d·c#·3d渲染·aspose·3d文件格式转换·3d sdk
ningqw5 小时前
SpringBoot 常用跨域处理方案
java·后端·springboot
superlls5 小时前
(Redis)主从哨兵模式与集群模式
java·开发语言·redis
小付同学呀6 小时前
word——如何给封面、目录、摘要、正文设置不同的页码
word
chenglin0166 小时前
C#_gRPC
开发语言·c#
Quz6 小时前
使用VBA宏批量修改Word中表格题注格式
word
骑驴看星星a6 小时前
数学建模--Topsis(Python)
开发语言·python·学习·数学建模
叫我阿柒啊7 小时前
Java全栈工程师面试实战:从基础到微服务的深度解析
java·redis·微服务·node.js·vue3·全栈开发·电商平台