/*** POST Multipart Request
* @Description:
*@paramrequestUrl 请求url
*@paramrequestText 请求参数
*@paramrequestFile 请求上传的文件
*@return*@throwsException*/
publicString sendPost3(String requestUrl,
Map requestText, Map requestFile) throwsException{
HttpURLConnection conn= null;
InputStream input= null;
OutputStream os= null;
BufferedReader br= null;
StringBuffer buffer= null;try{
URL url= newURL(requestUrl);
conn=(HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(1000 * 10);
conn.setReadTimeout(1000 * 10);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "multipart/form-data");
conn.setRequestProperty("Cookie", "sid=d696901c23409a695bb89caa67f38bd6;USER_FLAG_CHECK=d1281e431ec02361926042addd6e8244;pt_key=AAJdrsZRADAv-rYqAIae8TCpk15m92Ed0tNrhOrnxio7Zxd0PyWmR_eRNIrvKw9XUUrbNWqrzq4;appkey=wxshop_jstj;appid=wx805ef0a16c179341;");
conn.setRequestProperty("Accept", "*/*");
conn.setRequestProperty("Connection", "keep-alive");
conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
conn.setRequestProperty("Charset", "UTF-8");
conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" +BOUNDARY);
conn.connect();//往服务器端写内容 也就是发起http请求需要带的参数
os = newDataOutputStream(conn.getOutputStream());//请求参数部分
writeParams(requestText, os);//请求上传文件部分
writeFile(requestFile, os);//请求结束标志
String endTarget = PREFIX + BOUNDARY + PREFIX +LINE_END;
os.write(endTarget.getBytes());
os.flush();//读取服务器端返回的内容
System.out.println("======================响应体=========================");
System.out.println("ResponseCode:" +conn.getResponseCode()+ ",ResponseMessage:" +conn.getResponseMessage());if(conn.getResponseCode()==200){
input=conn.getInputStream();
}else{
input=conn.getErrorStream();
}
br= new BufferedReader(new InputStreamReader( input, "UTF-8"));
buffer= newStringBuffer();
String line= null;while ((line = br.readLine()) != null) {
buffer.append(line);
}//......
System.out.println("返回报文:" +buffer.toString());
}catch(Exception e) {//log.error(e.getMessage(), e);
throw newException(e);
}finally{try{if (conn != null) {
conn.disconnect();
conn= null;
}if (os != null) {
os.close();
os= null;
}if (br != null) {
br.close();
br= null;
}
}catch(IOException ex) {//log.error(ex.getMessage(), ex);
throw newException(ex);
}
}returnbuffer.toString();
}/*** 对post参数进行编码处理并写入数据流中
*@throwsException
*
*@throwsIOException
*
**/
private static void writeParams(MaprequestText,
OutputStream os)throwsException {try{
String msg= "请求参数部分:\n";if (requestText == null ||requestText.isEmpty()) {
msg+= "空";
}else{
StringBuilder requestParams= newStringBuilder();
Set> set =requestText.entrySet();
Iterator> it =set.iterator();while(it.hasNext()) {
Map.Entry entry =it.next();
requestParams.append(PREFIX).append(BOUNDARY).append(LINE_END);
requestParams.append("Content-Disposition: form-data; name=\"")
.append(entry.getKey()).append("\"").append(LINE_END);
requestParams.append("Content-Type: text/plain; charset=utf-8")
.append(LINE_END);
requestParams.append("Content-Transfer-Encoding: 8bit").append(
LINE_END);
requestParams.append(LINE_END);//参数头设置完以后需要两个换行,然后才是参数内容
requestParams.append(entry.getValue());
requestParams.append(LINE_END);
}
os.write(requestParams.toString().getBytes());
os.flush();
msg+=requestParams.toString();
}//System.out.println(msg);
}catch(Exception e){//log.error("writeParams failed", e);
throw newException(e);
}
}/*** 对post上传的文件进行编码处理并写入数据流中
*
*@throwsIOException
*
**/
private static void writeFile(MaprequestFile,
OutputStream os)throwsException {
InputStream is= null;try{
String msg= "请求上传文件部分:\n";if (requestFile == null ||requestFile.isEmpty()) {
msg+= "空";
}else{
StringBuilder requestParams= newStringBuilder();
Set> set =requestFile.entrySet();
Iterator> it =set.iterator();while(it.hasNext()) {
Map.Entry entry =it.next();if(entry.getValue() == null){//剔除value为空的键值对
continue;
}
requestParams.append(PREFIX).append(BOUNDARY).append(LINE_END);
requestParams.append("Content-Disposition: form-data; name=\"")
.append(entry.getKey()).append("\"; filename=\"")
.append(entry.getValue().getName()).append("\"")
.append(LINE_END);
requestParams.append("Content-Type:")
.append(entry.getValue().getContentType())
.append(LINE_END);
requestParams.append("Content-Transfer-Encoding: 8bit").append(
LINE_END);
requestParams.append(LINE_END);//参数头设置完以后需要两个换行,然后才是参数内容
os.write(requestParams.toString().getBytes());
os.write(entry.getValue().getBytes());
os.write(LINE_END.getBytes());
os.flush();
msg+=requestParams.toString();
}
}//System.out.println(msg);
}catch(Exception e){//log.error("writeFile failed", e);
throw newException(e);
}finally{try{if(is!=null){
is.close();
}
}catch(Exception e){//log.error("writeFile FileInputStream close failed", e);
throw newException(e);
}
}
}