Android 图片发送到服务器

来源:互联网 发布:c语言 随机模拟 编辑:程序博客网 时间:2024/05/21 23:37

关于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() {@Overridepublic 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);}@Overridepublic void onFailure(int arg0, String arg1, Throwable arg2) {AbToastUtil.showToast(AddGoodsActivity.this, arg2.getMessage());}@Overridepublic void onFinish() {// 移除进度框AbDialogUtil.removeDialog(AddGoodsActivity.this);}@Overridepublic 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();          }}
    }




1 0
原创粉丝点击