安卓上传图片,playframework服务器接收保存

写一个类继承AsyncHttpResponseHandler ,如:

class HttpResponse extends AsyncHttpResponseHandler {
private int type;
private RequestParams params;


public HttpResponse(int type, RequestParams params) {
this.type = type;
this.params = params;
}


@Override
public void onStart() {
// Initiated the request
Log.v("post", "upload In Turn on start...");
}


@Override
public void onSuccess(int statusCode, Header[] headers,
byte[] responseBody) {
// Successfully got a response
try {
String result = new String(responseBody, "utf-8");
Log.v("post", "on Success请求成功结果为:" + result);


JSONObject resultJson = new JSONObject(result);
String code = resultJson.getString("status_code");
if ("0".equals(code)) {
// 上传成功
Log.v("post", "success-------------");
Toast.makeText(RegActivity.this, "注册成功", Toast.LENGTH_SHORT)
.show();

//上传成功后需要跳转的页面
Intent intent = new Intent(RegActivity.this,
LoginActivity.class);
startActivity(intent);
} else {
Log.v("post", "fail------------");
}
} catch (Exception e) {
e.printStackTrace();
}


}


@Override
public void onFailure(int statusCode, Header[] headers,
byte[] responseBody, Throwable error) {
// Response failed :(
}

private void doUploadFailure(String error) {
}

@Override
public void onRetry() {
// Request was retried
}

@Override
public void onProgress(int bytesWritten, int totalSize) {
// Progress notification
Log.v("post", "on Progress上传进度:" + (int) (bytesWritten * 100 / totalSize) + "%");
load_prgress.setVisibility(View.VISIBLE);
}

@Override
public void onFinish() {
// Completed the request (either success or failure)
Log.v("post", "on Finish上传完成!");
load_prgress.setVisibility(View.GONE);
}
}

playframework服务器提供的接口如:http://127.0.0.1/webservice/registerUser

返回值:json 

请求方式:/webservice/registerUser

参数列表:

输入:

参数

类型

说明

 

userName

long

用户名

非空

userPwd

String

用户密码

非空

confirmPwd

String

确认密码

非空

uploadFile

File

头像

 

 

输出结果:

 

节点

类型

说明

status_code

int

状态码,0为成功,1为失败

msg

String

返回说明(包含失败时的信息)

result[user]

String

用户信息

构造上传的参数:

public static RequestParams getRegParams(String userName, String userPwd,String confirmPwd, String uploadFile) throws FileNotFoundException{
        RequestParams params = new RequestParams();
        params.put("userName", userName); 
        params.put("userPwd", userPwd);
        params.put("confirmPwd", confirmPwd); 
       
        if(uploadFile != null && !uploadFile.isEmpty()){
        File file = new File(uploadFile);
        InputStream is = new FileInputStream(file);
        params.put("uploadFile", is, file.getName());
        }else{
//       params.put("uploadFile", null);
        }
        return params;
    }

客户端通过接口上传图片:

RequestParams params;
try {
params = PrefUtil.getRegParams(UserName, PassWord, ConfirmPassWord, picPath);
AsyncHttpClient  client = new AsyncHttpClient();
client.post(Const.requestURL, params, new HttpResponse(0, params));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

playframework中后台的代码:

/**
* 注册普通用户
* @param userName
* @param userPwd
* @param confirmPwd
* @throws IOException 
*/
public static void registerUser(String userName, String userPwd, String confirmPwd, java.io.File uploadFile) throws IOException{
if(userName.isEmpty() || userPwd.isEmpty() || confirmPwd.isEmpty()){
status_code = Const.FAIL;
msg = Const.LOGIN_ERROR_EMPTY;
}else{
System.out.println("upload user photo");
VUser user = new VUser(userName, userPwd, confirmPwd);
user.checked = Const.CHECKED;
user.role = Const.USER;
user.save();

String fileName = user.userName + "_" + uploadFile.getName();
writePhoto(uploadFile, fileName);

VUser u = VUser.findById(user.getId());
u.photoPath = fileName;
u.save();  

status_code = Const.SUCCESS;
msg = "";
dataMap.put("result", VUser.singleUserBean(user));
}

dataMap.put("status_code", status_code);
dataMap.put("msg", msg);
renderJSON(dataMap);
}

public static String writePhoto(java.io.File pFile, String fileName) throws IOException{
// java.io.File pFile = new java.io.File(Blob.getStore(), "xiaohong_lyc.jpg");
BufferedImage image = ImageIO.read(pFile);
//要想保存这个对象的话你要把image声明为BufferedImage 类型
// String fileName = user.userName + "_" + pFile.getName();
String type = fileName.substring(fileName.indexOf('.') + 1, fileName.length());
java.io.File fo = new java.io.File(Blob.getStore(), fileName); // 将要转换出的小图文件
ImageIO.write(image, type, fo); 
return "";
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值