Java 后台访问别的项目API,上传文件实现

Java 后台访问别的项目API,上传文件实现

简述

跨系统API访问操作:A系统上传附件,现需要将A系统上传的附件同时上传到B系统,B系统有对应的页面操作实现。要求:不需要经过B系统页面,直接A系统后台上传附件到B系统对应目录下,故想到一个解决方案就是,A系统后台直接访问B系统的现有的上传文件的后台API。
**注意:**1、此处是http POST
2、每个添加的参数要和API匹配哦!

代码

public static String sendPostWithFile() {
		String url = "http://XXXXX/ImportFile"
        DataOutputStream out = null;
        BufferedReader in = null;
        String result = "";
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            HttpURLConnection conn = (HttpURLConnection) realUrl.openConnection();
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);

            String BOUNDARY = "------------1qazxsw23dedc"; // 定义数据分隔线
            conn.setUseCaches(false);
            conn.setRequestMethod("POST"); // 注意这里的“POST”要大写哦~
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("user-agent", "Mozilla/4.0(compatitble;MSIE 6.0; Windows NT 5.1; SV1");
            conn.setRequestProperty("Charsert", "UTF-8");
            conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
            conn.connect();

            out = new DataOutputStream(conn.getOutputStream());
            byte[] end_data = ("\r\n--" + BOUNDARY + "--\r\n").getBytes(); // 定义最后数据分隔
			// 添加参数areaFlag
            StringBuilder sb2 = new StringBuilder();
            sb2.append("--");
            sb2.append(BOUNDARY);
            sb2.append("\r\n");
            sb2.append("Content-Disposition: form-data;name=\"areaFlag\"");
            sb2.append("\r\n");
            sb2.append("\r\n");
            sb2.append("All");
            sb2.append("\r\n");
            out.write(sb1.toString().getBytes());

            // 添加参数file
            File file = new File("D:/4513420347.pdf");
            StringBuilder sb = new StringBuilder();
            sb.append("--");
            sb.append(BOUNDARY);
            sb.append("\r\n");
            sb.append("Content-Disposition: form-data;name=\"productFile\";filename=\"" + file.getName()+"\"");// name=后面接的B系统后台接参的名称,注意根据具体情况修改哦
            sb.append("\r\n");
            sb.append("Content-Type:application/octet-stream");
            sb.append("\r\n");
            sb.append("\r\n");
            out.write(sb.toString().getBytes());

            DataInputStream in1 = new DataInputStream(new FileInputStream(file));
            int bytes = 0;
            byte[] bufferOut = new byte[1024];
            while ((bytes = in1.read(bufferOut)) != -1) {
                out.write(bufferOut, 0, bytes);
            }
            out.write("\r\n".getBytes()); // 多个文件时,两个文件之间加这个
            in1.close();
            out.write(end_data);

            out.flush();
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while((line = in.readLine()) != null) {
                result += line;
            }

        } catch (Exception e) {
            System.out.print("发送POST请求出现异常!" + e);
            e.printStackTrace();
        }
        finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
        return result;
    }

猜你喜欢

转载自blog.csdn.net/zcxbd/article/details/105245339
今日推荐