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();
	          }
		}
    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谭祖爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值