依据图片路径下载文件转base64编码

public List downloadFile(String id, String AppId, String cid){
  
  //判断传入图像编码imgstr是否为空
   if( StrUtil.isNullOrBlank(id)){
   System.out.println("errorCode:"+"010200");
    return null;
   }
  //判断传入文件名称fileName是否为空
   if( StrUtil.isNullOrBlank(AppId)){
    System.out.println("errorCode:"+"010200");
    return null;
   }
  ITranslatAttributeService translatAttributeService = new TranslatAttributeService(); 
  TranslatAttribute translatAttribute = new TranslatAttribute(); 
  ArrayList pathList = new ArrayList();
  ArrayList paths = new ArrayList();;
  //查询数据库
  List  list = translatAttributeService.getObjectByUserId(id,cid);
  if ((list != null) && (list.size() != 0)) {  
   for(int i=0;i<list.size();i++){
    translatAttribute=(TranslatAttribute)list.get(i);
    String path = translatAttribute.getImage_path();
    if((path != null) && (path.length() != 0)){
     pathList.add(path);
    }else{
     continue;
    }
   }
        } else{
         System.out.println("-----------当前无相关文件信息-----------");
         return null;
        }
  //初始化转换文件流所需的变量
  URL url;
  HttpURLConnection conn = null;
  byte[] data;
  String base64Img;
  String lines = null;
  BufferedInputStream bis = null;
  byte[] in_b = null;
  byte[] buffer;
  //判断取出的文件路径值
  if ((pathList != null) && (pathList.size() != 0)) {  
   for(int i=0;i<pathList.size();i++){
    //判断当前路径是否为空或null
    String filePath = (String) pathList.get(i);
    if(StrUtil.isNotNullOrBlank(filePath)){
     try {
        //创建URL对象
           url = new URL(filePath);
           //返回一个URLConnection对象,它表示到URL所引用的远程对象的连接
           conn = (HttpURLConnection) url.openConnection();
           //设定请求方式
           conn.setRequestMethod("GET");
          //建立到远程对象的实际连接
           conn.connect();
             //返回打开连接读取的输入流
          InputStream in = conn.getInputStream();
          // 取HTTP请求流长度
          int size = conn.getContentLength();
          // 用于缓存每次读取的数据
          buffer = new byte[size];
          // 用于存放结果的数组
          in_b = new byte[size];
          int count = 0;
       int rbyte = 0;
          // 循环读取
           while (count < size){
          // 每次实际读取长度存于rbyte中
            rbyte = in.read(buffer);
            for (int j = 0; j < rbyte; j++){
             in_b[count + j] = buffer[j];
            }
            count += rbyte;
           }
        }catch (Exception e){
             e.printStackTrace();
             System.out.println("-----------当前转码异常-----------");
        }
       base64Img = new String(Base64.encodeBase64(in_b));
       paths.add(base64Img);
     }else{
         System.out.println("-----------当前图片不存在-----------");
         continue;
     }
   }
  }else{
         System.out.println("-----------当前无文件路径-----------");
         return null;
  }
  
  return paths;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值