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();
        }
    }
相关推荐
benjiangliu2 分钟前
LINUX系统-09-程序地址空间
android·java·linux
霍理迪3 分钟前
JS其他常用内置对象
开发语言·前端·javascript
历程里程碑9 分钟前
子串-----和为 K 的子数组
java·数据结构·c++·python·算法·leetcode·tornado
独自破碎E11 分钟前
字符串相乘
android·java·jvm
yuhulkjv33512 分钟前
ChatGPT和Gemini复制到word格式
chatgpt·word
王多鱼鱼鱼18 分钟前
QT如何将exe打包成可执行文件
开发语言·qt
DokiDoki之父21 分钟前
边写软件边学kotlin(一):Kotlin语法初认识:
android·开发语言·kotlin
liu****22 分钟前
Qt进阶实战:事件处理、文件操作、多线程与网络编程全解析
开发语言·网络·数据结构·c++·qt
草原上唱山歌24 分钟前
C++如何调用Python代码
开发语言·c++·python
木子啊26 分钟前
PHP中间件:ThinkCMF 6.x核心利器解析
开发语言·中间件·php