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();
        }
    }
相关推荐
nlog3n2 小时前
Java策略模式详解
java·bash·策略模式
Qlittleboy3 小时前
windows如何安装wkhtmltoimage 给PHP使用根据HTML生成图片
开发语言·windows·php
水w4 小时前
【Android Studio】解决报错问题Algorithm HmacPBESHA256 not available
android·开发语言·android studio
冷琅辞4 小时前
Elixir语言的云计算
开发语言·后端·golang
Mryan20055 小时前
解决GraalVM Native Maven Plugin错误:JAVA_HOME未指向GraalVM Distribution
java·开发语言·spring boot·maven
Naomi5215 小时前
自定义汇编语言(Custom Assembly Language) 和 Unix & Git
服务器·开发语言·git·unix
烂蜻蜓5 小时前
C 语言命令行参数:让程序交互更灵活
c语言·开发语言·交互
VX_CXsjNo15 小时前
免费送源码:Java+SSM+Android Studio 基于Android Studio游戏搜索app的设计与实现 计算机毕业设计原创定制
java·spring boot·spring·游戏·eclipse·android studio·android-studio
zm-v-159304339865 小时前
解锁 DeepSeek 与 Matlab:攻克科研难题的技术利刃
开发语言·matlab·信息可视化
ylfhpy5 小时前
Java面试黄金宝典33
java·开发语言·数据结构·面试·职场和发展·排序算法