1 packagecom.gede.wechat.util;2
3 importjava.io.BufferedReader;4 importjava.io.DataInputStream;5 importjava.io.DataOutputStream;6 importjava.io.File;7 importjava.io.FileInputStream;8 importjava.io.InputStreamReader;9 importjava.io.OutputStream;10 importjava.net.HttpURLConnection;11 importjava.net.URL;12 importjava.util.Iterator;13 importjava.util.Map;14
15 importjavax.activation.MimetypesFileTypeMap;16
17 importcom.gede.web.util.GlobalConstants;18
19 /**
20 *@authorgede21 *@versiondate:2019年5月26日 下午8:47:2822 * @description :23 */
24 public classHttpPostUploadUtil {25
26 publicString urlStr;27
28 publicHttpPostUploadUtil() {29 urlStr = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token="
30 + GlobalConstants.getInterfaceUrl("access_token") + "&type=image";31 }32
33 /**
34 * 上传图片35 *36 *@paramurlStr37 *@paramtextMap38 *@paramfileMap39 *@return
40 */
41 @SuppressWarnings("rawtypes")42 public String formUpload(Map textMap, MapfileMap) {43 String res = "";44 HttpURLConnection conn = null;45 String BOUNDARY = "---------------------------123821742118716"; //boundary就是request头和上传文件内容的分隔符
46 try{47 URL url = newURL(urlStr);48 conn =(HttpURLConnection) url.openConnection();49 conn.setConnectTimeout(5000);50 conn.setReadTimeout(30000);51 conn.setDoOutput(true);52 conn.setDoInput(true);53 conn.setUseCaches(false);54 conn.setRequestMethod("POST");55 conn.setRequestProperty("Connection", "Keep-Alive");56 conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.6)");57 conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" +BOUNDARY);58
59 OutputStream out = newDataOutputStream(conn.getOutputStream());60 //text
61 if (textMap != null) {62 StringBuffer strBuf = newStringBuffer();63 Iterator> iter =textMap.entrySet().iterator();64 while(iter.hasNext()) {65 Map.Entry entry =(Map.Entry) iter.next();66 String inputName =(String) entry.getKey();67 String inputValue =(String) entry.getValue();68 if (inputValue == null) {69 continue;70 }71 strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");72 strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"\r\n\r\n");73 strBuf.append(inputValue);74 }75 out.write(strBuf.toString().getBytes());76 }77
78 //file
79 if (fileMap != null) {80 Iterator> iter =fileMap.entrySet().iterator();81 while(iter.hasNext()) {82 Map.Entry entry =(Map.Entry) iter.next();83 String inputName =(String) entry.getKey();84 String inputValue =(String) entry.getValue();85 if (inputValue == null) {86 continue;87 }88 File file = newFile(inputValue);89 String filename =file.getName();90 String contentType = newMimetypesFileTypeMap().getContentType(file);91 if (filename.endsWith(".jpg")) {92 contentType = "image/jpg";93 }94 if (contentType == null || contentType.equals("")) {95 contentType = "application/octet-stream";96 }97
98 StringBuffer strBuf = newStringBuffer();99 strBuf.append("\r\n").append("--").append(BOUNDARY).append("\r\n");100 strBuf.append("Content-Disposition: form-data; name=\"" + inputName + "\"; filename=\"" +filename101 + "\"\r\n");102 strBuf.append("Content-Type:" + contentType + "\r\n\r\n");103
104 out.write(strBuf.toString().getBytes());105
106 DataInputStream in = new DataInputStream(newFileInputStream(file));107 int bytes = 0;108 byte[] bufferOut = new byte[1024];109 while ((bytes = in.read(bufferOut)) != -1) {110 out.write(bufferOut, 0, bytes);111 }112 in.close();113 }114 }115
116 byte[] endData = ("\r\n--" + BOUNDARY + "--\r\n").getBytes();117 out.write(endData);118 out.flush();119 out.close();120
121 //读取返回数据
122 StringBuffer strBuf = newStringBuffer();123 BufferedReader reader = new BufferedReader(newInputStreamReader(conn.getInputStream()));124 String line = null;125 while ((line = reader.readLine()) != null) {126 strBuf.append(line).append("\n");127 }128 res =strBuf.toString();129 reader.close();130 reader = null;131 } catch(Exception e) {132 System.out.println("发送POST请求出错。" +urlStr);133 e.printStackTrace();134 } finally{135 if (conn != null) {136 conn.disconnect();137 conn = null;138 }139 }140 returnres;141 }142
143 }