ssm实现图片预览

先写一个方法把图片转换成64位字符串
public static String getImgPath(String ImgPath) {
    InputStream inputStream = null;
    byte[] data = null;
    try {
        inputStream = new FileInputStream(ImgPath);
        data = new byte[inputStream.available()];
        inputStream.read(data);
        inputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // 加密
    BASE64Encoder encoder = new BASE64Encoder();
    return encoder.encode(data);
}

 

//下面就是升级版的

 

@RequestMapping("/preview/{id}") 
    @ResponseBody
    public List preview(HttpServletRequest request,@PathVariable("fid") Integer fid){


        List<Picture> picture= new ArrayList<Picture>();
        List<byte[]>  list=new ArrayList<byte[]>();
         InputStream inputStream = null;
            byte[] datas = null;
         try {
             if(fid==null){
                 picture=null;
             }else{
                 picture=pictureService.selectPicture(fid);
                 if(picture!=null){
                     //循环把图片转换成64位字符串
                     for (int j = 0; j < picture.size(); j++) {    
                     inputStream = new FileInputStream(picture.get(j).getFilePath());
                        datas = new byte[inputStream.available()];
                        inputStream.read(datas);

                       //把64位字符串存入list中
                        list.add(datas);
                        inputStream.close();
                 }             
                 }         
             }
        } catch (Exception e) {
            e.printStackTrace();
        }

      //直接返回list
         return list;
}  

//在js中循环生成图片。

 

$.ajax({
                    type: 'post',
                    url: "/file/preview+"/"+id",
                    dataType: 'json',
                    data: formData,
                    async: true,
                    cache: false,
                    success: function(list) {
                   var imgListBox = $(".xxxx");

                 var content = " ";

      for (var i = 0; i < list.length; i++) {

     <img src=" + "data:image/png;base64," + list[i] + ">

}

imgListBox.append(content);

                    }error: function() {
                        toastr.error("Upload failed");
                    }
            

});
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值