1:发送请求
// 1:地址 String actionUrl = GwlzxtPath.getInstance().getZjkmeetingul().concat("meeting/fromyxBaoMingUser"); // 2:组装存放文件的map Map<String, String> files = new HashMap<String, String>(); //:3:存放值的Map. BeanUtils.descube 主要是把实体类转化成了键值对 Map<String, String> textMap=org.apache.commons.beanutils.BeanUtils.describe(meetRegistration); textMap.put("meetingid", meetRegistration.getMid().getId()); String result = null; // 4:发送请求 result = HttpUtil.upLoadFilePost(actionUrl, textMap,files,"application/json");
/** * 发送请求 包含文件 * @param urlStr 地址 * @param textMap 参数集合 *@param fileMap 文件集合 * @return * @throws IOException */ public static String upLoadFilePost(String urlStr, Map<String, String> textMap, Map<String, String> fileMap, String contentType) throws IOException { String res = ""; HttpURLConnection conn = null; // boundary就是request头和上传文件内容的分隔符 String BOUNDARY = "---------------------------123821742118716"; try { URL url = new URL(urlStr); conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(5000); conn.setReadTimeout(30000); conn.setDoOutput(true); conn.setDoInput(true); conn.setUseCaches(false); //method conn.setRequestMethod("POST"); //http长连接 conn.setRequestProperty("Connection", "Keep-Alive"); //客户端标识 conn.setRequestProperty("User-Agent","Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)"); // 提交请求方式 : form 表单形式 conn.setRequestProperty("Content-Type","multipart/form-data; boundar