需求:接收方需要接收json格式的数据,如下图。
传输json数据格式:
话不多说,上代码:完成需求全代码如下:
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
运行输出结果如下:
{"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,后面的即是图片转码后值。