zoukankan      html  css  js  c++  java
  • 文件流无法找到地址问题

    public class JsonImage {
        /**
         * TODO:将byte数组以Base64方式编码为字符串
         * @param bytes 待编码的byte数组
         * @return 编码后的字符串
         * */
        public static String encode(byte[] bytes){
            return new BASE64Encoder().encode(bytes);
        }
        
        /**
         * TODO:将以Base64方式编码的字符串解码为byte数组
         * @param encodeStr 待解码的字符串
         * @return 解码后的byte数组
         * @throws IOException 
         * */
        public static byte[] decode(String encodeStr) throws IOException{
            byte[] bt = null;  
            BASE64Decoder decoder = new BASE64Decoder();  
            bt = decoder.decodeBuffer(encodeStr);
            return bt;
        }
        
        /**
         * TODO:将两个byte数组连接起来后,返回连接后的Byte数组
         * @param front 拼接后在前面的数组
         * @param after 拼接后在后面的数组
         * @return 拼接后的数组
         * */
        public static byte[] connectBytes(byte[] front, byte[] after){
            byte[] result = new byte[front.length + after.length];
            System.arraycopy(front, 0, result, 0, after.length);
            System.arraycopy(after, 0, result, front.length, after.length);
            return result;
        }
        
        /**
         * TODO:将图片以Base64方式编码为字符串
         * @param imgUrl 图片的绝对路径(例如:D:\jsontest\abc.jpg)
         * @return 编码后的字符串
         * @throws IOException 
         * */
        public static String encodeImage(String imgUrl) throws IOException{
            URL url = new URL(imgUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            DataInputStream fis1 = new DataInputStream(connection.getInputStream());
            byte[] rs = new byte[fis1.available()];
            fis1.read(rs);
            fis1.close();
            return encode(rs);
        }
    public static void main(String[] args) {
     String str;

    //若为服务器文件路径    则 : URL url = new URL(imgUrl); HttpURLConnection connection = (HttpURLConnection) url.openConnection();

     String a="http://172.19.0.45:9080/wdhacinfo/MainServlet?action=DOWNLOAD&FILE_ID=1007953211&FILE_OWNER=10011000029625";
           try {
               str = encodeImage(a);
               System.out.println(str);
           } catch (IOException e) {
               e.printStackTrace();
           }
    }
  • 相关阅读:
    [小技巧] micropython 如何执行 *.mpy 文件
    从零开始深入 Linux 底层(软件工程)
    从嵌套结构中取值时如何编写兜底逻辑
    学习JUC源码(2)——自定义同步组件
    学习JUC源码(1)——AQS同步队列(源码分析结合图文理解)
    Java多线程中的wait/notify通信模式
    详解Java锁的升级与对比(1)——锁的分类与细节(结合部分源码)
    认识Redis集群——Redis Cluster
    工作三年多的感慨与总结(二)
    工作三年多的感慨与总结(一)
  • 原文地址:https://www.cnblogs.com/liuhx/p/8534886.html
Copyright © 2011-2022 走看看