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


    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();
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
  • 相关阅读:
    拷贝构造函数vs移动构造函数
    1136 A Delayed Palindrome
    linux练习题
    《算法系列》之排序
    【面试题 17.16.】按摩师 c++
    中国企业出海金字塔:产品出海、渠道出海和品牌出海
    初识Java 9-1 内部类
    心跳包
    KingbaseES V8R6兼容Oracle的exp-imp导出导入工具使用
    详解Redis三大集群模式,轻松实现高可用!
  • 原文地址:https://blog.csdn.net/weixin_45067120/article/details/134447424