这里用的是org.apache.http进行模拟http的文件的上传。
public String uploadSubmit(String baseUrl,File file) throws Exception {
HttpPost post = new HttpPost(baseUrl);//post提交
MultipartEntity entity = new MultipartEntity();
// 添加文件参数
if (file != null && file.exists()) {
entity.addPart("Filed", new FileBody(file));
}
post.setEntity(entity);//设置提交的实体
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(post);
int stateCode = response.getStatusLine().getStatusCode();
StringBuffer sb = new StringBuffer();
if (stateCode == HttpStatus.SC_OK) {
//成功之后获取网络中返回的数据
HttpEntity result = response.getEntity();
if (result != null) {
InputStream is = result.getContent();
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
String tempLine;
while ((tempLine = br.readLine()) != null) {
sb.append(tempLine);
}
}
}
post.abort();
Log.d(TAG, "执行的结果" + sb.toString());
if (sb.length() == 0) {
return null;
} else {
String s = StringUtil.getContentByRegex("http.*?\"", sb.toString());
if (s == null) {
return null;
}
return s.replaceAll("\"", "");
}
}