关于安卓上传图片到服务器 我有个图片上传服务器的demo 但用的是HttpConnection来做的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 后台用的是struct2框架来接受图片的上传 客户端代码/** * 上传文件到服务器 * @param file * @return * @throws Exception */ public Json uploadLocal(File file) throws Exception { fileSize = (int) file.length(); if (null != progress) {progress.setMax(fileSize);} String url = "http://192.168.1.58:8088/test/other/httpTakeFile.htm"; HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection(); conn.setDoOutput(true);conn.setDoInput(true); conn.setChunkedStreamingMode(1024 * 1024); conn.setRequestMethod(Domain.REQUEST_POST); conn.setRequestProperty("connection", "Keep-Alive"); conn.setRequestProperty("Charsert", HTTP.UTF_8); conn.setRequestProperty("Content-Type", "multipart/form-data;file=" + file.getName()); conn.setRequestProperty("ext", file.getName().substring(file.getName().lastIndexOf("."))); OutputStream output = conn.getOutputStream();InputStream input = new FileInputStream(file); int i = 0, total = 0; byte[] buffer = new byte[1024]; while ((i = input.read(buffer)) != -1) { output.write(buffer, 0, i); total += i; if (null != progress) {progress.setProgress(total);} } String response = readFile(conn.getInputStream(), HTTP.UTF_8, 1, false); input.close();output.flush();output.close();conn.disconnect(); JSONObject object = new JSONObject(response); Json vo = new Json(object.getBoolean("success"), object.getString("message")); JSONObject data = null;try {data = (JSONObject) object.get("data");} catch (Exception e) {} if (null != data) { Iterator iterator = data.keys(); while (iterator.hasNext()) { String key = (String) iterator.next();vo.getData().put(key, data.get(key)); } } return vo; }服务器端代码:/** * http上传文件 * @return */ public Json httpTakeFile() { try { CommUtil.checkRequestMethod(Constant.REQUEST_POST); String folder = FileUtil.UPLOAD_TEMP_PATH + "10/"; return new Json(true, Json.OPERATE_RIGHT_MESSAGE). addData("fileUrl", FileUtil.readRootUrl() + folder + FileUtil.httpTakeFile(FileUtil.getBasePath() + folder)); } catch (Exception e) { log.error("http上传文件", e); return new Json(false, e.getMessage()); } } 现在googlemap v1的apikey已经不能申请了,v2的怎么申请呢 求教安卓按键监听 已经做好了一个 通讯录,现在想做一个来电显示,显示联系人的一些信息。要如何实现?? 关于屏幕分辨率为 320*533的问题 全志A10 android4.1编译出错 【请教】Ubuntu 编译错误 切换acivity时,如何保存各个activity的状态 netbeans安装android插件问题 android socket创建失败 finish问题 android静默卸载怎么实现? sqlite 查询 遍历 datetime
* 上传文件到服务器
* @param file
* @return
* @throws Exception
*/
public Json uploadLocal(File file) throws Exception {
fileSize = (int) file.length();
if (null != progress) {progress.setMax(fileSize);}
String url = "http://192.168.1.58:8088/test/other/httpTakeFile.htm";
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoOutput(true);conn.setDoInput(true);
conn.setChunkedStreamingMode(1024 * 1024);
conn.setRequestMethod(Domain.REQUEST_POST);
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("Charsert", HTTP.UTF_8);
conn.setRequestProperty("Content-Type", "multipart/form-data;file=" + file.getName());
conn.setRequestProperty("ext", file.getName().substring(file.getName().lastIndexOf(".")));
OutputStream output = conn.getOutputStream();InputStream input = new FileInputStream(file);
int i = 0, total = 0;
byte[] buffer = new byte[1024];
while ((i = input.read(buffer)) != -1) {
output.write(buffer, 0, i);
total += i;
if (null != progress) {progress.setProgress(total);}
}
String response = readFile(conn.getInputStream(), HTTP.UTF_8, 1, false);
input.close();output.flush();output.close();conn.disconnect();
JSONObject object = new JSONObject(response);
Json vo = new Json(object.getBoolean("success"), object.getString("message"));
JSONObject data = null;try {data = (JSONObject) object.get("data");} catch (Exception e) {}
if (null != data) {
Iterator iterator = data.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();vo.getData().put(key, data.get(key));
}
}
return vo;
}服务器端代码:
/**
* http上传文件
* @return
*/
public Json httpTakeFile() {
try {
CommUtil.checkRequestMethod(Constant.REQUEST_POST);
String folder = FileUtil.UPLOAD_TEMP_PATH + "10/";
return new Json(true, Json.OPERATE_RIGHT_MESSAGE).
addData("fileUrl", FileUtil.readRootUrl() + folder + FileUtil.httpTakeFile(FileUtil.getBasePath() + folder));
} catch (Exception e) {
log.error("http上传文件", e);
return new Json(false, e.getMessage());
}
}