H5 上傳文件

jsp:代碼

<input type="file" name="applyFile" id="applyFile" capture="camera" accept="application/pdf,image/*" multiple="multiple" >

  <img alt="" src="" id = "testImg" width="140" height="180">

var filedata = new Array();

    $("#applyFile").change(function(){

    debugger

      var file = document.getElementById("applyFile").files[0];              

       if(file) {

           var reader = new FileReader();

           reader.readAsDataURL(file);

           reader.onload = function(){

               var imgstr = this.result; //這就是base64字符串

                var image = document.getElementById("testImg");

               image.src = imgstr;

               filedata[0] = imgstr;

               alert(imgstr);

              var imageName = '111.pdf';

             //這裏可換成ajax請求因爲本框架不是springmvc沒有使用ajax

               PlatformContext.getJSONBean('registerJsBean',appId).uploadImage(imageName,imgstr)

              .done(function(returnProduct){

                 alert("成功");

                  }).fail(function(err){

                    alert(err);

                  });

           };  

       }

    });

java代碼:

public TransResult<Boolean> uploadImage(String imageName,String imageBase64){

    Map<String, String> imgMap = new HashMap<>();

     imageBase64=imageBase64.substring(imageBase64.indexOf(",")+1);

    imgMap.put(imageName, imageBase64);

    VisitService visitService = (VisitService) ApplicationContextUtil.getBean("visitServiceImpl");

    TransResult<String> resList = visitService.uploadFile(imgMap);

     return null;

}

@Override

   public TransResult<String> uploadFile(Map<String, String> imgMap) {

     // TODO Auto-generated method stub

     TransResult<String> result = new TransResult<>();

     OutputStream out = null;

     String imgTempPath = PropertiesUtil.getProperties("ftp.properties").getProperty(VisitConstant.VISIT_IMAGE_TEMP_PATH);

     try {

        for(String imgName : imgMap.keySet()){

          BASE64Decoder decoder = new BASE64Decoder();

          String imageBase64=imgMap.get(imgName);

          imageBase64=imageBase64.substring(imageBase64.indexOf(",")+1);

          byte[] bytes = decoder.decodeBuffer(imageBase64);

          for (int i = 0; i < bytes.length; ++i) {

             if (bytes[i] < 0) {// 調整異常數據

               bytes[i] += 256;

             }

          }

          String path = imgTempPath+ imgName;

          // 生成jpeg圖片

          out = new FileOutputStream(path);

          out.write(bytes);

          out.flush();

        }

        result.success();

        result.setObject(imgTempPath);

     } catch (Exception e) {

        result.failure("保存圖片過程中異常");

        Logger.info("[回訪服務][保存圖片][異常]");

        Logger.printStackTrace(e);

     }finally {

        try {

          out.close();

        } catch (IOException e) {

          // TODO Auto-generated catch block

          e.printStackTrace();

        }

     }

     return result;

   }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章