解决方案 »
- 新的eclipse生成的Android项目默认的是相对布局,怎么修改可使生成Android项目时直接就为线性布局?
- EXCEL表里的数据怎么倒到sqlite数据库里?
- 如何在Setting的listItem中添加箭头表示又下级内容
- 小弟android新手,请教一个xml解析的问题
- 亲们。一个窗口问题
- 为什么返回之后上一项会被覆盖?
- activity和service通信问题
- ListView遇到很奇怪的问题
- android 分享功能中如何去除不必要的分享程序 如360助手这些等等
- CI 中的jenkins和Hudson有什么区别吗?
- OpenCV4Android-FaceRecognition-例程中出现的错误
- 微信里对图片的处理效果
public void postDataFile(String serverUrl, String fileurl, String data) {
Log.e("test", "load" + serverUrl);
Log.e("test", "load" + fileurl);
Log.e("test", "load" + data);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(serverUrl);
MultipartEntity mpEntity = new MultipartEntity();
try {
File file = new File(fileurl);
FileBody fileBody = new FileBody(file);
mpEntity.addPart("file", fileBody);
mpEntity.addPart(
"data",
new StringBody(data, Charset
.forName(org.apache.http.protocol.HTTP.UTF_8)));
post.setEntity(mpEntity);
HttpResponse response = client.execute(post);
if (response.getStatusLine().getStatusCode() == 200) {
String content = EntityUtils.toString(response.getEntity(),
"utf-8");
ho.handleEvent(content);//回调函数
}
Log.e("test", "IsOk");
} catch (Exception e) {
Log.e("test", "出错了:"+e);
} finally {
if (mpEntity != null) {
try {
mpEntity.consumeContent();
} catch (UnsupportedOperationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
client.getConnectionManager().shutdown();
}
}
private static final String TAG = "uploadFile";
private static final int TIME_OUT = 10 * 1000; // 超时时间
private static final String CHARSET = "utf-8"; // 设置编码
public static int uploadFile(File file, String RequestURL,Context context) {
int res=0; String result = null;
String BOUNDARY = UUID.randomUUID().toString();
// 边界标识 随机生成
String PREFIX = "--", LINE_END = "\r\n";
String CONTENT_TYPE = "multipart/form-data"; // 内容类型
try {
URL url = new URL(RequestURL);
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);
Toast.makeText(context, "--------------------"+file.getPath(), 3).show();
if (file !=null) { /** * 当文件不为空时执行上传 */
Toast.makeText(context, "--name----"+file.getName(), 3).show();
DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
StringBuffer sb = new StringBuffer();
sb.append(PREFIX);
sb.append(BOUNDARY);
sb.append(LINE_END);
sb.append("Content-Disposition: form-data; name=\"file\"; filename=\""+file.getName()+"\""+LINE_END);
sb.append("Content-Type: application/octet-stream; charset="+CHARSET+LINE_END);
sb.append(LINE_END);
dos.write(sb.toString().getBytes());
InputStream is = new FileInputStream(file);
byte[] bytes = new byte[2048];
int len = 0;
while ((len = is.read(bytes)) != -1) {
dos.write(bytes, 0, len);
}
is.close(); dos.write(LINE_END.getBytes());
byte[] end_data = (PREFIX + BOUNDARY + PREFIX + LINE_END) .getBytes();
dos.write(end_data);
dos.flush();
res = conn.getResponseCode();
Toast.makeText(context, "上传成功"+res, 3);
if (res==200) {
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();
Toast.makeText(context, "上传成功", 3).show();
} else {
Toast.makeText(context, "上传成功-----", 3).show();
}
}
}
catch (MalformedURLException e) {
e.printStackTrace();
Toast.makeText(context, "上传失败+++", 3).show();
} catch (IOException e) {
e.printStackTrace();
}
return res;
}