import java.io.bufferedreader;
import java.io.dataoutputstream;
import java.io.file;
import java.io.fileinputstream;
import java.io.filenotfoundexception;
import java.io.fileoutputstream;
import java.io.ioexception;
import java.io.inputstream;
import java.io.inputstreamreader;
import java.io.outputstream;
import java.net.httpurlconnection;
import java.net.malformedurlexception;
import java.net.url;
import java.nio.charset.charset;
import android.content.context;
import android.util.log;
public class httppost {
public static boolean postsdfile(string urlstr, string msg, string filepath
) {
final string tag = "uploadfile";
final int time_out = 10 * 1000; // 超时时间
final string charset = "utf-8"; // 设置编码
//string result = null;
string boundary = base64.encode(msg);// uuid.randomuuid().tostring(); //边界标识
// 随机生成
string prefix = "--", line_end = "\r\n";
string content_type = "multipart/form-data"; // 内容类型
int res = 0;
url url;
inputstream is=null;
try {
url = new url(urlstr);
httpurlconnection conn = (httpurlconnection) url
.openconnection();
conn.setreadtimeout(time_out);
conn.setconnecttimeout(time_out);
conn.setdoinput(true); // 允许输入流
conn.setdooutput(true); // 允许输出流
conn.setusecaches(false); // 不允许使用缓存
conn.setrequestmethod("post"); // 请求方式
conn.setrequestproperty("charset", charset); // 设置编码
conn.setrequestproperty("connection", "keep-alive");
conn.setrequestproperty("content-type", content_type
+ ";boundary=" + boundary);
file f = new file(filepath);
if(!f.exists()){
string write_str ="此文件已被删除";
try{
fileoutputstream fout = new fileoutputstream(f);
byte [] bytes = write_str.getbytes();
fout.write(bytes);
fout.close();
}
catch(exception e){
e.printstacktrace();
}
}
is = new fileinputstream(f);
//is = context.openfileinput(filepath);// 打开文件流
if (is != null) {
/**
* 当文件不为空,把文件包装并且上传
*/
dataoutputstream dos = new dataoutputstream(
conn.getoutputstream());
stringbuffer sb = new stringbuffer();
sb.append(prefix);
sb.append(boundary);
sb.append(line_end);
* 这里重点注意: name里面的值为服务器端需要key 只有这个key 才可以得到对应的文件
* filename是文件的名字,包含后缀名的 比如:abc.png
sb.append("content-disposition: form-data; name=\"uploadedfile\"; filename=\""
+ base64.encode(filepath) + "\"" + line_end);
sb.append("content-type: application/octet-stream; charset="
+ charset + line_end);
dos.write(sb.tostring().getbytes());
byte[] bytes = new byte[1024];
int len = 0;
while ((len = is.read(bytes)) != -1) {
dos.write(bytes, 0, len);
system.out.println("aaaa"+len);
is.close();
dos.write(line_end.getbytes());
byte[] end_data = (prefix + boundary + prefix + line_end)
.getbytes();
dos.write(end_data);
dos.flush();
* 获取响应码 200=成功 当响应成功,获取响应的流
res = conn.getresponsecode();
log.e(tag, "response code:" + res);
log.e(tag, "request success");
inputstream input = conn.getinputstream();
stringbuffer sb1 = new stringbuffer();
int ss;
while ((ss = input.read()) != -1) {
sb1.append((char) ss);
//result = sb1.tostring();
//log.e(tag, "result : " + result);
} catch (malformedurlexception e) {
// todo auto-generated catch block
e.printstacktrace();
} catch (ioexception e1) {
e1.printstacktrace();
if(res == 200){
return true;}
else{return false;}
public static string postmsg(string pathurl, string msg) {
stringbuffer sb = null;
sb = new stringbuffer();
try {
// 建立连接
url url = new url(pathurl);
httpurlconnection httpconn = (httpurlconnection) url.openconnection();
//设置连接属性
httpconn.setdooutput(true);// 使用 url 连接进行输出
httpconn.setdoinput(true);// 使用 url 连接进行输入
httpconn.setusecaches(false);// 忽略缓存
httpconn.setrequestmethod("post");// 设置url请求方法
string requeststring = base64.encode(msg);
// 设置请求属性
// 获得数据字节数据,请求数据流的编码,必须和下面服务器端处理请求流的编码一致
byte[] requeststringbytes = requeststring.getbytes();
httpconn.setrequestproperty("content-length", "" + requeststringbytes.length);
httpconn.setrequestproperty("content-type", "application/octet-stream");
httpconn.setrequestproperty("connection", "keep-alive");// 维持长连接
httpconn.setrequestproperty("charset", "utf-8");
// 建立输出流,并写入数据
outputstream outputstream = httpconn.getoutputstream();
outputstream.write(requeststringbytes);
outputstream.close();
//system.out.println("1");
// 获得响应状态
int responsecode = httpconn.getresponsecode();
if (httpurlconnection.http_ok == responsecode) {// 连接成功
// 当正确响应时处理数据
string readline;
bufferedreader responsereader;
// 处理响应流,必须与服务器响应流输出的编码一致
responsereader = new bufferedreader(new inputstreamreader(
httpconn.getinputstream()));
while ((readline = responsereader.readline()) != null) {
sb.append(readline).append("\n");
}
responsereader.close();
}
} catch (exception ex) {
ex.printstacktrace();
}
return sb.tostring();
}
转载:http://blog.csdn.net/chaoyu168/article/details/49076149