ajax post 数据过长导致后台属性驱动接受不到值的处理

//ajax post 数据过长导致后台属性驱动接受不到值的处理

参数中有%%这种是由于encode了,需要decode就正常

  var jsonshuzu={"imgName":"111.png","bluckname":"telesale-weixin","imgType":"identityFrontPicUrl","orderNo":"A001","imgSource":"additional"};

  // 图片选择改变事件

  function inputchange(tag){

  alert(1);

  var inputTag = 'file' + tag;

  var file = document.getElementById(inputTag);

  if(window.FileReader){//chrome,firefox7+,opera,IE10+

  alert(2);

  oFReader = new FileReader();

  oFReader.readAsDataURL(file.files[0]);

  oFReader.onload = function (oFREvent) {

  alert(3);

  uploadImage(oFREvent.target.result,tag);

  alert(4);

  // oFREvent.target.result  结果就是base64的数据

  };

  }

  }

  // 图片上传

  var jsonshuzu={"imgName":"111.png","bluckname":"telesale","imgType":"identityFrontPicUrl","orderNo":"001"};

  function uploadImage(imageData,tag){

  alert(5);

  $.ajax({

  url: '${ctx}/itm/weixin/additional1.action',

  data: {

  imgData: imageData ,// 图片数据流

  jsonsImg:JSON.stringify(jsonshuzu)

  },

//  processData : false,

//  contentType : false,

  dataType: 'json',

  type: 'post',

  success: function(data) {

  var dataJsonObject = JSON.parse(data);

  if (dataJsonObject.returnCode == 'A0001' ) {

  var img = '#imgsrc';

  var imgurl=dataJsonObject.result.imgUrl;

  var fileNameForm=dataJsonObject.result.fileNameForm;

  $(img).attr('src','${ctx}/itm/weixin/getObject.action?imgurl='+imgurl);

  $("#fNameForm").val(fileNameForm);

  }else{

  alert('图片上传失败!');

  }

  },

  error: function(xhr, type, errorThrown) {

  alert('网络异常,请稍后再试!');

  }

  });

  }

public String additional1() throws Exception{

//InputStream is = new FileInputStream(fileName.getPath());

InputStream sbs = null;

System.currentTimeMillis();

//获取ajax传过来的json参数

imgInfoReq=(ImgInfoReq)JSONObject.toBean(JSONObject.fromObject(jsonsImg),ImgInfoReq.class);

HttpServletRequest request = ServletActionContext.getRequest();

request.setCharacterEncoding("UTF-8");

StringBuilder sb = new StringBuilder();

try(BufferedReader reader = request.getReader();) {

char[] buff = new char[1024];

int len;

while((len = reader.read(buff)) != -1) {

sb.append(buff,0, len);

}

}catch (IOException e) {

e.printStackTrace();

}

if(imgInfoReq==null){

String tre = this.splitString(sb.toString(), "jsonsImg");

String dre = URLDecoder.decode(tre,"UTF-8");

imgInfoReq=(ImgInfoReq)JSONObject.toBean(JSONObject.fromObject(dre),ImgInfoReq.class);

}

//形成新的随机图片名称

fileNameForm = imgInfoReq.getImgName() + System.currentTimeMillis();

fileNameForm = Base64Utils.byteArrayToBase64(fileNameForm.getBytes());

fileNameForm=UUID.randomUUID().toString().replaceAll("-", "");

String endpfrx = imgInfoReq.getImgName().substring(imgInfoReq.getImgName().lastIndexOf("."));

fileNameForm = fileNameForm + endpfrx;

//fileNameForm="1.JPG";

//获取图片流

if(imgData==null){

//JSONObject jobject = JSONObject.fromObject("{"+sb.toString()+"}");

String tmg = this.splitString(sb.toString(), "imgData");

String dmg = URLDecoder.decode(tmg,"UTF-8");

imgData = dmg;

logger.info("======图片保存开始imgData====="+imgData);

}

byte[] imgd=  generateImage(imgData.substring(imgData.indexOf(",")+1));

        //保存图片

JSONObject finalJSONObject = new JSONObject();

logger.info("======图片保存开始=====");

if (imgData != null) {

sbs = new ByteArrayInputStream(imgd);

Minoperator.saveObject(sbs, imgInfoReq.getBluckname(), imgInfoReq.getOrderNo()+"/"+fileNameForm);

sbs.close();

}

else{

//Minoperator.saveObject(is,fileNameFileName);

}

}

public String splitString(String str, String temp) {
   String result = null;
   if (str.indexOf(temp) != -1) {
      if (str.substring(str.indexOf(temp)).indexOf("&") != -1) {
         result = str.substring(str.indexOf(temp)).substring(str.substring(str.indexOf(temp)).indexOf("=") + 1,
               str.substring(str.indexOf(temp)).indexOf("&"));

      } else {
         result = str.substring(str.indexOf(temp)).substring(str.substring(str.indexOf(temp)).indexOf("=") + 1);

      }
   }
   return result;
}

猜你喜欢

转载自yuhuiblog6338999322098842.iteye.com/blog/2400825