当前位置: 首页 » 产品 » 农牧养殖 » 正文

Java 发送http请求上传文件功能案例

放大字体  缩小字体 发布日期: 2024-11-29 03:56   来源:http://www.baidu.com/  作者:无忧资讯  浏览次数:13
核心提示:package wxapi.WxHelper; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream;

package wxapi.WxHelper; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import java.util.Date; import java.util.Map; import java.util.Map.Entry; public class HttpRequestUtil { public static String sendGet(String requestUrl, Map<String, String> requestHeader, String responseEncoding) { String result=""; BufferedReader reader=null; try { if (requestUrl==null || requestUrl.isEmpty()) { return result; } URL realUrl=new URL(requestUrl); URLConnection connection=realUrl.openConnection(); connection.setRequestProperty("accept", "text/html, application/xhtml+xml, image/jxr, * public static String sendPost(String requestUrl, Map<String, String> requestHeader, Map<String, String> formTexts, Map<String, String> files, String requestEncoding, String responseEncoding) { OutputStream out=null; BufferedReader reader=null; String result=""; try { if (requestUrl==null || requestUrl.isEmpty()) { return result; } URL realUrl=new URL(requestUrl); HttpURLConnection connection=(HttpURLConnection) realUrl.openConnection(); connection.setRequestProperty("accept", "text/html, application/xhtml+xml, image/jxr, */*"); connection.setRequestProperty("user-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:53.0) Gecko/20100101 Firefox/53.0"); if (requestHeader !=null && requestHeader.size() > 0) { for (Entry<String, String> entry : requestHeader.entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue()); } } connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); if (requestEncoding==null || requestEncoding.isEmpty()) { requestEncoding="UTF-8"; } if (responseEncoding==null || responseEncoding.isEmpty()) { responseEncoding="UTF-8"; } if (requestHeader !=null && requestHeader.size() > 0) { for (Entry<String, String> entry : requestHeader.entrySet()) { connection.setRequestProperty(entry.getKey(), entry.getValue()); } } if (files==null || files.size()==0) { connection.setRequestProperty("content-type", "application/x-www-form-urlencoded"); out=new DataOutputStream(connection.getOutputStream()); if (formTexts !=null && formTexts.size() > 0) { String formData=""; for (Entry<String, String> entry : formTexts.entrySet()) { formData +=entry.getKey() + "=" + entry.getValue() + "&"; } formData=formData.substring(0, formData.length() - 1); out.write(formData.toString().getBytes(requestEncoding)); } } else { String boundary="-----------------------------" + String.valueOf(new Date().getTime()); connection.setRequestProperty("content-type", "multipart/form-data; boundary=" + boundary); out=new DataOutputStream(connection.getOutputStream()); if (formTexts !=null && formTexts.size() > 0) { StringBuilder sbFormData=new StringBuilder(); for (Entry<String, String> entry : formTexts.entrySet()) { sbFormData.append("--" + boundary + "\r "); sbFormData.append("Content-Disposition: form-data; name=\"" + entry.getKey() + "\"\r \r "); sbFormData.append(entry.getValue() + "\r "); } out.write(sbFormData.toString().getBytes(requestEncoding)); } for (Entry<String, String> entry : files.entrySet()) { String fileName=entry.getKey(); String filePath=entry.getValue(); if (fileName==null || fileName.isEmpty() || filePath==null || filePath.isEmpty()) { continue; } File file=new File(filePath); if (!file.exists()) { continue; } out.write(("--" + boundary + "\r ").getBytes(requestEncoding)); out.write(("Content-Disposition: form-data; name=\"" + fileName + "\"; filename=\"" + file.getName() + "\"\r ").getBytes(requestEncoding)); out.write(("Content-Type: application/x-msdownload\r \r ").getBytes(requestEncoding)); DataInputStream in=new DataInputStream(new FileInputStream(file)); int bytes=0; byte[] bufferOut=new byte[1024]; while ((bytes=in.read(bufferOut)) !=-1) { out.write(bufferOut, 0, bytes); } in.close(); out.write(("\r ").getBytes(requestEncoding)); } out.write(("--" + boundary + "--").getBytes(requestEncoding)); } out.flush(); out.close(); out=null; reader=new BufferedReader(new InputStreamReader(connection.getInputStream(), responseEncoding)); String line; while ((line=reader.readLine()) !=null) { result +=line; } } catch (Exception e) { System.out.println("发送POST请求出现异常!"); e.printStackTrace(); } finally { try { if (out !=null) { out.close(); } if (reader !=null) { reader.close(); } } catch (IOException ex) { ex.printStackTrace(); } } return result; } }

 
 
[ 产品搜索 ]  [ 加入收藏 ]  [ 告诉好友 ]  [ 打印本文 ]  [ 违规举报 ]  [ 关闭窗口 ]

 

 
推荐图文
推荐产品
点击排行
    行业协会  备案信息  可信网站