base64文件传输,Java 发送json数据接口

base64文件传输,Java 发送json数据接口

需求:接收方需要接收json格式的数据,如下图。

传输json数据格式:
base64文件传输,Java 发送json数据接口_第1张图片
话不多说,上代码:完成需求全代码如下:

package xxxxxxxxxx;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import java.lang.StringBuilder;
import java.io.BufferedReader;
import com.alibaba.fastjson.JSONObject;

public class Test2 {
	public static void main(String[] args) {
		String url = "http://xxxxxxxxxxxxxxx";  //备注:您请求对方接口的url地址
		
		Map map = new HashMap<>();
		map.put("packageName", "XXXXXXXXXX");
		map.put("packageCode", "PMJJ-XXX");
		map.put("aName", XXXXX");
		map.put("aCode", "123456789012345678");
		map.put("aMandator", "XXXX");
		map.put("packageType", "XXX");
		map.put("address", "XXXXXXXXXX");
		map.put("plotCode", "190101010100002,190101010100003");
		map.put("area", 5.96);
		map.put("areaUnit", "XX");
		map.put("lease", 1);
		map.put("leaseUnit", "XX");
		map.put("customType", "XXX");
		map.put("useDescribe", "XX");
		map.put("bName", "XX");
		map.put("bCode", "987654321098765432");
		map.put("finishPrice", 1000);
		map.put("noticeTime", "2020-05-21");
		String param = "";
		List extraFile = new ArrayList<>();
		try {
			Map fileMap = new HashMap<>();
			String base64 = null;
			File file = new File("C:/Desktop/水调歌头base64.txt");   //本地已转的base64格式的文件
			FileInputStream fis;
			fis = new FileInputStream(file);
			byte[] byte64 = new byte[(int) file.length()];
			fis.read(byte64);
			base64 = new String(byte64);
			fileMap.put("fileName", "xxxx.pdf");  //附件名称
			fileMap.put("fileBase64", base64);
			extraFile.add(fileMap);
			map.put("extraFile", extraFile);
			param = JSONObject.toJSONString(map);
			System.out.println(param);
			PrintWriter out = null;
	        BufferedReader in = null;
	        String result = "";
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection conn = realUrl.openConnection();
            // 设置通用的请求属性
            conn.setRequestProperty("accept", "*/*");
            conn.setRequestProperty("connection", "Keep-Alive");
            conn.setRequestProperty("Content-Type", "application/json");
            //conn.setRequestProperty("Content-Type", "multipart/form-data");
            conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 发送POST请求必须设置如下两行
            conn.setDoOutput(true);
            conn.setDoInput(true);
            // 获取URLConnection对象对应的输出流
            out = new PrintWriter(conn.getOutputStream());
            // 发送请求参数
            out.print(param);
            // flush输出流的缓冲
            out.flush();
            // 定义BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
            System.out.println("----------------------------");
            System.out.println(result);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}

}

运行输出结果如下:


{"area":5.96,"areaUnit":"XX","address":"XXXXXXX","bName":"XX","aMandator":"XXXX","bCode":"987654321098765432","extraFile":[{"fileName":"xxxxxx.pdf","fileBase64":"此处输出是base64格式编码,由于过长,省略。"}],"packageType":"XXXX","customType":"XXX","finishPrice":1000,"packageCode":"PMJJ-XXXXX1","aName":"XXXXXX","leaseUnit":"XX","aCode":"123456789012345678","packageName":"XXXXXXXXXX","lease":1,"useDescribe":"XXXX","noticeTime":"2020-05-21","plotCode":"190101010100002,190101010100003"}

备注:
base64转码可参考链接:https://www.zhangxinxu.com/sp/base64.html。直接拖文件进去即可。
base64,后面的即是图片转码后值。

你可能感兴趣的:(接口,java)