客户端选择图片:
Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); //"android.intent.action.GET_CONTENT"
innerIntent.setType("image/*");
startActivityForResult(innerIntent, REQUEST_CODE_SCAN_GALLERY);
选择完提交图片:
Uri uriInfor;
public void onActivityResult(final int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_SCAN_GALLERY:
uriInfor = data.getData();
String imagePath = getRealPathFromUri(getActivity(), data.getData());
String[] arrPath = imagePath.split("\\/");
userImage = arrPath[arrPath.length - 1];
FinalHttp finalHttp = new FinalHttp();
// 文件上传到服务器的位置
AjaxParams params = new AjaxParams();
// 获取要上传的本地资源
try {
params.put("userImage", new File(imagePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
finalHttp.post(Consts.URL + Consts.APP.RegisterAction + "?action_flag=uploadImage", params, new AjaxCallBack<Object>() {
@Override
public void onStart() {
// mbtnAdd.setText("开始上传");
super.onStart();
}
@Override
public void onSuccess(Object o) {
// mbtnAdd.setText("上传成功");
// ToastUtil.show(getActivity(), "上传成功");
Log.i("pony_log", "图片上传成功");
// updateUser(false);
super.onSuccess(o);
updateImage(true);
}
@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
// mbtnAdd.setText("上传失败");
super.onFailure(t, errorNo, strMsg);
}
});
break;
}
}
super.onActivityResult(requestCode, resultCode, data);
}
客户端更新用户图片数据:
private void updateImage(boolean isShow) {
AjaxParams params = new AjaxParams();
params.put("action_flag", "updateImage");
params.put("uImg", userImage + "");
params.put("userId", MemberUserUtils.getUid(getActivity()) + "");
httpPost(Consts.URL + Consts.APP.RegisterAction, params, Consts.actionId.resultCode, isShow, "正在注册...");
}
@Override
protected void callBackSuccess(ResponseEntry entry, int actionId) {
super.callBackSuccess(entry, actionId);
switch (actionId) {
case Consts.actionId.resultCode:
musicImage.setImageURI(uriInfor);
ToastUtil.show(getActivity(), "修改成功");
break;
}
}
服务端接收图片数据:
private void uploadImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String imagePath = null;
try {
List<FileItem> items = this.upload.parseRequest(request);
if (items != null && !items.isEmpty()) {
for (FileItem fileItem : items) {
String filename = fileItem.getName();
File real_path = new File(Consts.imgPath + "/" + filename);
InputStream inputSteam = fileItem.getInputStream();
BufferedInputStream fis = new BufferedInputStream(inputSteam);
FileOutputStream fos = new FileOutputStream(real_path);
int f;
while ((f = fis.read()) != -1) {
fos.write(f);
}
fos.flush();
fos.close();
fis.close();
inputSteam.close();
System.out.println("文件:" + filename + "上传成功!");
imagePath = filename;
}
}
} catch (FileUploadException e) {
e.printStackTrace();
}
}
服务端用户修改图片数据:
private void updateImage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String path = request.getContextPath();
String uImg = request.getParameter("uImg");
String uid = request.getParameter("userId");
List<Object> params = new ArrayList<Object>();
params.add(uImg);
params.add(uid);
boolean flag = registerDao.updateImage(params);
if (flag) {
JSONObject jsonmsg = new JSONObject();
jsonmsg.put("repMsg", "修改成功");
jsonmsg.put("repCode", "666");
System.out.println(jsonmsg);
response.getWriter().print(jsonmsg);
} else {
JSONObject jsonmsg = new JSONObject();
jsonmsg.put("repMsg", "修改失败");
jsonmsg.put("repCode", "111");
System.out.println(jsonmsg);
response.getWriter().print(jsonmsg);
}
}