httpPost提交数据
2016-03-24 14:25
886 查看
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class HttpPostUtil {
URL url;
HttpURLConnection conn;
String boundary = "--------httppost123";
Map<String, String> textParams = new HashMap<String, String>();
Map<String, File> fileparams = new HashMap<String, File>();
DataOutputStream ds;
public HttpPostUtil(String url) throws Exception {
this.url = new URL(url);
}
//重新设置要请求的服务器地址,即上传文件的地址。
public void setUrl(String url) throws Exception {
this.url = new URL(url);
}
//增加一个普通字符串数据到form表单数据中
public void addTextParameter(String name, String value) {
textParams.put(name, value);
}
//增加一个文件到form表单数据中
public void addFileParameter(String name, File value) {
fileparams.put(name, value);
}
// 清空所有已添加的form表单数据
public void clearAllParameters() {
textParams.clear();
fileparams.clear();
}
// 发送数据到服务器,返回一个字节包含服务器的返回结果的数组
public byte[] send() throws Exception {
initConnection();
try {
conn.connect();
} catch (SocketTimeoutException e) {
// something
throw new RuntimeException();
}
ds = new DataOutputStream(conn.getOutputStream());
writeFileParams();
writeStringParams();
paramsEnd();
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
out.flush();
byte[] re = null;
try {
re = out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
out=null;
conn= null;
}finally{
out.close();
out=null;
conn.disconnect();
conn= null;
}
return re;
}
//文件上传的connection的一些必须设置
private void initConnection() throws Exception {
conn = (HttpURLConnection) this.url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(10000); //连接超时为10秒
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
conn.setChunkedStreamingMode(1024 * 50);
}
//普通字符串数据
private void writeStringParams() throws Exception {
Set<String> keySet = textParams.keySet();
for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
String name = it.next();
String value = textParams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"\r\n");
ds.writeBytes("\r\n");
ds.writeBytes(encode(value) + "\r\n");
}
}
//文件数据
private void writeFileParams() throws Exception {
Set<String> keySet = fileparams.keySet();
for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
String name = it.next();
File value = fileparams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"; filename=\"" + encode(value.getName()) + "\"\r\n");
ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");
ds.writeBytes("\r\n");
ds.write(getBytes(value));
ds.writeBytes("\r\n");
}
}
//获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream
private String getContentType(File f) throws Exception {
// return "application/octet-stream"; // 此行不再细分是否为图片,全部作为application/octet-stream 类型
ImageInputStream imagein = ImageIO.createImageInputStream(f);
if (imagein == null) {
return "application/octet-stream";
}
Iterator<ImageReader> it = ImageIO.getImageReaders(imagein);
if (!it.hasNext()) {
imagein.close();
return "application/octet-stream";
}
imagein.close();
return "image/" + it.next().getFormatName().toLowerCase();//将FormatName返回的值转换成小写,默认为大写
}
//把文件转换成字节数组
private byte[] getBytes(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
return out.toByteArray();
}
//添加结尾数据
private void paramsEnd() throws Exception {
ds.writeBytes("--" + boundary + "--" + "\r\n");
ds.writeBytes("\r\n");
}
// 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码
private String encode(String value) throws Exception{
return URLEncoder.encode(value, "UTF-8");
}
public static void main(String[] args) throws Exception {
HttpPostUtil u = new HttpPostUtil("http://localhost:8080/Test/synDataAction.do?method=test");
u.addFileParameter("file", new File("F:/327.jpg"));
u.addTextParameter("ext_type", "picture");
u.addTextParameter("remark", "sfw单位的微风dd哈哈哈哈");
u.addTextParameter("user_id", "aa");
u.addTextParameter("chn_id", "17818");
byte[] b = u.send();
String ss = new String(b);
System.out.println(ss);
/* HttpPostUtil u = new HttpPostUtil("http://localhost:8080/Test/synDataAction.do?method=test");
u.addTextParameter("path", "/weblogic/upload/facadeImages/20141202112845_2.jpg");
u.addTextParameter("user_id", "aa");
byte[] b = u.send();
//System.out.println(Arrays.toString(b));
File f = new File("F:/a");
if(!f.mkdir())
f.mkdir();
FileOutputStream out = new FileOutputStream(new File("F:/a/a.jpg"));
try{
out.write(b, 0, b.length);
out.close();
}catch (Exception e) {
e.printStackTrace();
}*/
String str = "sfw%E5%8D%95%E4%BD%8D%E7%9A%84%E5%BE%AE%E9%A3%8Edd%E5%93%88%E5%93%88%E5%93%88%E5%93%88";
System.out.println(URLDecoder.decode(str,
bfe8
"utf-8"));
}
}
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.SocketTimeoutException;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
public class HttpPostUtil {
URL url;
HttpURLConnection conn;
String boundary = "--------httppost123";
Map<String, String> textParams = new HashMap<String, String>();
Map<String, File> fileparams = new HashMap<String, File>();
DataOutputStream ds;
public HttpPostUtil(String url) throws Exception {
this.url = new URL(url);
}
//重新设置要请求的服务器地址,即上传文件的地址。
public void setUrl(String url) throws Exception {
this.url = new URL(url);
}
//增加一个普通字符串数据到form表单数据中
public void addTextParameter(String name, String value) {
textParams.put(name, value);
}
//增加一个文件到form表单数据中
public void addFileParameter(String name, File value) {
fileparams.put(name, value);
}
// 清空所有已添加的form表单数据
public void clearAllParameters() {
textParams.clear();
fileparams.clear();
}
// 发送数据到服务器,返回一个字节包含服务器的返回结果的数组
public byte[] send() throws Exception {
initConnection();
try {
conn.connect();
} catch (SocketTimeoutException e) {
// something
throw new RuntimeException();
}
ds = new DataOutputStream(conn.getOutputStream());
writeFileParams();
writeStringParams();
paramsEnd();
InputStream in = conn.getInputStream();
ByteArrayOutputStream out = new ByteArrayOutputStream();
int b;
while ((b = in.read()) != -1) {
out.write(b);
}
out.flush();
byte[] re = null;
try {
re = out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
out=null;
conn= null;
}finally{
out.close();
out=null;
conn.disconnect();
conn= null;
}
return re;
}
//文件上传的connection的一些必须设置
private void initConnection() throws Exception {
conn = (HttpURLConnection) this.url.openConnection();
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(10000); //连接超时为10秒
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
conn.setChunkedStreamingMode(1024 * 50);
}
//普通字符串数据
private void writeStringParams() throws Exception {
Set<String> keySet = textParams.keySet();
for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
String name = it.next();
String value = textParams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"\r\n");
ds.writeBytes("\r\n");
ds.writeBytes(encode(value) + "\r\n");
}
}
//文件数据
private void writeFileParams() throws Exception {
Set<String> keySet = fileparams.keySet();
for (Iterator<String> it = keySet.iterator(); it.hasNext();) {
String name = it.next();
File value = fileparams.get(name);
ds.writeBytes("--" + boundary + "\r\n");
ds.writeBytes("Content-Disposition: form-data; name=\"" + name
+ "\"; filename=\"" + encode(value.getName()) + "\"\r\n");
ds.writeBytes("Content-Type: " + getContentType(value) + "\r\n");
ds.writeBytes("\r\n");
ds.write(getBytes(value));
ds.writeBytes("\r\n");
}
}
//获取文件的上传类型,图片格式为image/png,image/jpg等。非图片为application/octet-stream
private String getContentType(File f) throws Exception {
// return "application/octet-stream"; // 此行不再细分是否为图片,全部作为application/octet-stream 类型
ImageInputStream imagein = ImageIO.createImageInputStream(f);
if (imagein == null) {
return "application/octet-stream";
}
Iterator<ImageReader> it = ImageIO.getImageReaders(imagein);
if (!it.hasNext()) {
imagein.close();
return "application/octet-stream";
}
imagein.close();
return "image/" + it.next().getFormatName().toLowerCase();//将FormatName返回的值转换成小写,默认为大写
}
//把文件转换成字节数组
private byte[] getBytes(File f) throws Exception {
FileInputStream in = new FileInputStream(f);
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b, 0, n);
}
in.close();
return out.toByteArray();
}
//添加结尾数据
private void paramsEnd() throws Exception {
ds.writeBytes("--" + boundary + "--" + "\r\n");
ds.writeBytes("\r\n");
}
// 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码
private String encode(String value) throws Exception{
return URLEncoder.encode(value, "UTF-8");
}
public static void main(String[] args) throws Exception {
HttpPostUtil u = new HttpPostUtil("http://localhost:8080/Test/synDataAction.do?method=test");
u.addFileParameter("file", new File("F:/327.jpg"));
u.addTextParameter("ext_type", "picture");
u.addTextParameter("remark", "sfw单位的微风dd哈哈哈哈");
u.addTextParameter("user_id", "aa");
u.addTextParameter("chn_id", "17818");
byte[] b = u.send();
String ss = new String(b);
System.out.println(ss);
/* HttpPostUtil u = new HttpPostUtil("http://localhost:8080/Test/synDataAction.do?method=test");
u.addTextParameter("path", "/weblogic/upload/facadeImages/20141202112845_2.jpg");
u.addTextParameter("user_id", "aa");
byte[] b = u.send();
//System.out.println(Arrays.toString(b));
File f = new File("F:/a");
if(!f.mkdir())
f.mkdir();
FileOutputStream out = new FileOutputStream(new File("F:/a/a.jpg"));
try{
out.write(b, 0, b.length);
out.close();
}catch (Exception e) {
e.printStackTrace();
}*/
String str = "sfw%E5%8D%95%E4%BD%8D%E7%9A%84%E5%BE%AE%E9%A3%8Edd%E5%93%88%E5%93%88%E5%93%88%E5%93%88";
System.out.println(URLDecoder.decode(str,
bfe8
"utf-8"));
}
}
相关文章推荐
- Android使用httpPost向服务器发送请求的方法
- HttpClient获取服务器JSON数据问题处理
- Android中使用HttpPost请求
- Android HttpPost
- android http post file
- android jni 调用 libcurl
- 用httpPost对JSON发送和接收的例子
- 利用HTTPClient模块中HttpGet和HttpPost 发送接口请求
- JSP&&Android中使用Http
- 温习HttpClient使用范例
- Android-封装post和get 网络请求
- 用httpPost对JSON发送和接收的例子
- Android文件上传至服务器
- [android]-如何在向服务器发送request时附加已保存的cookie数据
- Java Tomcat SSL 服务端/客户端双向认证
- 使用httppost请求发送form表单的方法
- 导入HttpPost包
- 图片上传
- 对于携带请求参数的HttpGet与的HttpPost两种请求方式,代码段分析
- HttpClient4 学习分享总结