Android 图片发送到服务器
关于Android 图片发送到服务器处理的问题,倒腾了我好几天,终于弄出来了!再次小记一下,方便大家学习,也贡自己记忆!
Android端代码:
public void addGoodsTask() { // 获取Http工具类 mAbHttpUtil = AbHttpUtil.getInstance(this); mAbHttpUtil.setTimeout(10000); String url = Constant.getRealPath("addGoods.do"); String[] types=mtype.split(":");//商品类型 // 绑定参数 AbRequestParams params = new AbRequestParams(); params.put("name", mgoodsname); params.put("price", mpay_price); params.put("type", ""+1); params.put("amount",mpayment_amount); params.put("typeId", types[0]); params.put("typeName", types[1]); params.put("userId", application.mUser.getUserId()); params.put("description", mdescription); String image=""; try { StringBuilder sb = new StringBuilder(); for (int i = 0; i < mPhotoList.size() - 1; i++) { String path = mPhotoList.get(i); File file = new File(path); sb.append(FileUtil.image2String(file));//将图片转换成String sb.append(","); } image=sb.toString(); params.put("image",image); //System.out.println(images); System.out.println("------------------------"); } catch (Exception e) { e.printStackTrace(); } mAbHttpUtil.post(url, params, new AbStringHttpResponseListener() { @Override public void onSuccess(int statusCode, String content) { String responsedto = Constant.getResponse(content); AbToastUtil.showToast(AddGoodsActivity.this, responsedto); Intent intent = new Intent(AddGoodsActivity.this,MainActivity.class); startActivity(intent); } @Override public void onFailure(int arg0, String arg1, Throwable arg2) { AbToastUtil.showToast(AddGoodsActivity.this, arg2.getMessage()); } @Override public void onFinish() { // 移除进度框 AbDialogUtil.removeDialog(AddGoodsActivity.this); } @Override public void onStart() { // 显示进度框 AbDialogUtil.showProgressDialog(AddGoodsActivity.this, 0,"正在发布"); } }); }
服务端代码:
@POST @Path("addGoods.do") public String addGoods( @FormParam("name") String name, @FormParam("price") String price, @FormParam("type") Integer type, @FormParam("amount") String amount, @FormParam("typeId") String typeId, @FormParam("typeName") String typeName, @FormParam("userId") String userId, @FormParam("description") String description, @FormParam("image") String image ) { String[] image11= image.split(","); for (int i = 0; i < image11.length; i++) { byte[] imgbyte = FileUtil.hex2byte(image11[i].toString()); byte[] bytes =imgbyte; ImageIcon icon = null; if (bytes != null && bytes.length > 0) { icon=new ImageIcon(bytes); } Image images =icon.getImage(); try { BufferedImage inputbig = new BufferedImage(256, 256, BufferedImage.TYPE_INT_BGR); Graphics2D g = (Graphics2D) inputbig.getGraphics(); g.drawImage(images, 0, 0,256,256,null); //画图 g.dispose(); inputbig.flush(); File file2 = new File("F:/JAVA_Work/orderServer/wechat/WebRoot/WEB-INF/images/"); //此目录保存缩小后的关键图 if (file2.exists()) { System.out.println("多级目录已经存在不需要创建!!"); } else { file2.mkdirs();//如果要创建的多级目录不存在才需要创建。 } File files=new File("F:/JAVA_Work/orderServer/wechat/WebRoot/WEB-INF/images/" + System.currentTimeMillis() + ".jpg");//保持图片到指定路径 list_images.add("http://localhost:8080/order/WebRoot/WEB-INF/images/"+files.getName());//添加图片路径到list中 ImageIO.write(inputbig, "jpg", files); //将其保存在C:/imageSort/targetPIC/下 } catch (Exception ex) { ex.printStackTrace(); } }
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。