Java客户端通过Http发送POST请求上传文件到web服务器
2015-01-29 13:46
941 查看
通过HttpURLConnection类实现http协议上传信息
import java.io.BufferedInputStream; import java.io.BufferedWriter; import java.io.ByteArrayOutputStream; import java.io.InputStream; import java.io.OutputStreamWriter; import java.net.HttpURLConnection; import java.net.URL; import com.sys.common.log.Log; /** * */ /** * 使用HttpURLConnection对象模拟测试后台接口 * @author * */ public class MyHttpURLConnection { private static String HTTP_URL="http://www.baidu.com"; private static String HTTP_METHOD_POST="POST"; public static void main(String[] args) { try { System.out.println( MyHttpURLConnection.sendHttp("")); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 模拟HTTP 请求 提交普通数据 * * @param postData * 发送数据 * @return */ private static String sendHttp(String postData) throws Exception { if (HTTP_URL == null || "".equals(HTTP_URL)) { throw new Exception("请传入远程请求地址"); } String result = ""; BufferedWriter out = null; BufferedInputStream reader = null; HttpURLConnection connection = null; try { URL u = new URL(HTTP_URL); connection = (HttpURLConnection) u.openConnection(); int code; connection.setDoOutput(true);//是否向connection输出 connection.setUseCaches(false);//因为是post请求所以不能使用缓存 connection.setConnectTimeout(120000); // 连接超时为120秒 connection.setRequestMethod(HTTP_METHOD_POST); //告诉服务端 要发送数据的长度 connection.setRequestProperty("Content-Length", postData.getBytes("UTF-8").length + ""); //告诉服务器客户端浏览器内核 connection.setRequestProperty("User-Agent", "Mozilla/4.0"); //告诉服务器 要发送数据的格式和字符编码 connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8"); //通过connection连接对象获取输出流对象 out = new BufferedWriter(new OutputStreamWriter( connection.getOutputStream(), "UTF-8")); //将需要想客户端发送的信息写入到输出流 刷新输出流并且关闭流 out.write(postData); out.flush(); out.close(); out = null; code = connection.getResponseCode(); if (code == HttpURLConnection.HTTP_OK) { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); reader = new BufferedInputStream(connection.getInputStream()); int b; byte[] bit = new byte[1024]; while (-1 != (b = reader.read(bit))) { byteArrayOutputStream.write(bit, 0, b); } result = new String(byteArrayOutputStream.toByteArray()); } else { Log.error("post ResponseCode=" + connection.getResponseCode()); Log.error("post ResponseMessage=" + connection.getResponseMessage()); reader = new BufferedInputStream(connection.getErrorStream()); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); int b; byte[] bit = new byte[1024]; while (-1 != (b = reader.read(bit))) { byteArrayOutputStream.write(bit, 0, b); } result = new String(byteArrayOutputStream.toByteArray()); throw new Exception("服务端发生异常"); } } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (out != null) { out.flush(); out.close(); } if (reader != null) { reader.close(); } if (connection != null) { connection.disconnect(); } } return result; } }
相关文章推荐
- Java客户端通过Http发送POST请求上传文件到web服务器
- Java客户端通过Http发送POST请求上传文件到web服务器
- 通过POST向WEB服务器发送数据/上传文件(HttpSendRequestEx、InternetWriteFile)
- Multipart/form-data POST文件上传详解 理论 简单的HTTP POST 大家通过HTTP向服务器发送POST请求提交数据,都是通过form表单提交的,代码如下: <form me
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- 一个简单地应用--从客户端向服务器发送数据.post,get.httpclientget,httpclinetpost,文件上传.
- 通过HttpWebRequest分别向服务器发送GET或POST请求
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- Java利用HttpURLConnection发送post请求上传文件
- 【转】asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件(二)
- Java利用HttpURLConnection发送post请求上传文件
- asp.net(c#)使用HttpWebRequest附加携带请求参数以post方式模拟上传大文件(以图片为例)到Web服务器端
- Android 发送HTTP GET POST 请求以及通过 MultipartEntityBuilder 上传文件
- 用ASIHttpRequest通过post请求给java服务器上传的代码(不会出现乱码)
- 如何用Java客户端/applet通过HTTP POST上传文件
- 用HttpListener做web服务器,简单解析post方式过来的参数、上传的文件
- C#实现通过HttpWebRequest发送POST请求实现网站自动登陆
- 利用HttpURLConnection发送post请求上传多个文件