zoukankan      html  css  js  c++  java
  • 日记整理---->2017-05-14

      学习一下知识吧,好久没有写博客了。如果他总为别人撑伞,你又何苦非为他等在雨中。

    学习的知识内容

    一、关于base64的图片问题

    byte[] decode = Base64.base64ToByteArray(string);
    String strings = new String(decode);
    IOUtils.write(strings, new FileOutputStream("D:/huhx1.png")); // we can't open this file
    IOUtils.write(decode, new FileOutputStream("D:/huhx2.png")); // 正常显示

     感觉在浏览器上,base64的前缀比较宽松。只要data:.....;base64。....中间的内容可以随意,具体情况也不是很明朗。以下是可以显示图片的。

    上述的src的内容是图片的二进制流经过base编码的,想要保存图片的话。需要对内容进行base的解码操作,再将解码之后的字节保存为图片。

    关于图片base64可以参考博客:http://www.cnblogs.com/coco1s/p/4375774.html

    二、关于classLoader的一些继承关系

    public void classLoader_1() {
        ClassLoader classLoader = getClass().getClassLoader();
        System.out.println(classLoader); // sun.misc.Launcher$AppClassLoader@3b05c7e1
    
        ClassLoader parent = classLoader.getParent();
        System.out.println(parent); // sun.misc.Launcher$ExtClassLoader@7885a30c
    
        ClassLoader parent1 = parent.getParent();
        System.out.println(parent1); // null
    }

     三、关于new File的路径问题

    @Test
    public void relativePath_1() {
        File file = new File("path/huhx.png"); // G:JavaJavaEEProgram2016-05-15SpringLearnhuhx-testpathhuhx.png
        System.out.println(file.getAbsolutePath());
    }
    
    @Test
    public void absolutePath_1() {
        File file = new File("/path/huhx.png"); // G:pathhuhx.png
        System.out.println(file.getAbsolutePath());
    }

    四、关于classLoader的getResource方法

    // programPath = G:/Java/JavaEE/Program/2016-05-15/SpringLearn/huhx-test
    public void classStreamPath_1() { System.out.println(Thread.currentThread().getContextClassLoader().getResource("")); // file:/programPath/target/test-classes/ System.out.println(getClass().getClassLoader().getResource("")); // file:/programPath/target/test-classes/ System.out.println(ClassLoader.getSystemResource("")); // file:/programPath/target/test-classes/ System.out.println(getClass().getResource("")); // file:/programPath/target/test-classes/com/linux/huhx/filepath/ System.out.println(getClass().getResource("/")); // file:/programPath/target/test-classes/ System.out.println(new File("/").getAbsolutePath()); // G: System.out.println(System.getProperty("user.dir")); // G:JavaJavaEEProgram2016-05-15SpringLearnhuhx-test }

    五、关于编码的一些知识

    public void charsetTest_1() {
        String string = "刘玲";
        String str = new String(string.getBytes()); // 默认的是utf-8的
        System.out.println(str); // 刘玲
    
        Charset charset = Charset.forName("gbk");
        ByteBuffer byteBuffer = charset.encode("刘玲");
    
        byte[] buffers = byteBuffer.array();
        System.out.println(new String(buffers)); // �����
    
        CharBuffer charBuffer = charset.decode(byteBuffer); // 刘玲
        System.out.println(charBuffer);
    
        try {
            System.out.println(new String(buffers, "gbk")); // 刘玲
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    六、嵌套目录的创建和删除

    public void fileTest_1() {
        File file = new File("D:/liuling/liuling/huhx.txt");
        if (!file.exists()) {
            // 现在的目录D:/liuling/liuling/huhx.txt,注意这里的huhx.txt是一个目录名。
            System.out.println(file.mkdirs()); // true
        }
        File deletefile = new File("D:/liuling");
        System.out.println(deletefile.delete()); // false
        // huhx.txt目录被删除了
        System.out.println(file.delete()); // true
    }

    file.mkdir();是创建一级的子目录, mkdirs()是可以创建多级的嵌套子目录的。

    七、关于java中的foreach的null问题

    public void forEachTest() {
        String[] strings = null;
        for (String string : strings) { // java.lang.NullPointerException
    
            System.out.println(string);
        }
    List
    <String> lists = null; for (String list : lists) { // // java.lang.NullPointerException System.out.println(list); } List<String> list2s = new ArrayList<>(); for (String list : list2s) { System.out.println(list); // 没有任何的打印 } }

    ps:上述的文件不能直接运行的,因为异常的存在,后面的流程指定是不能执行的。

    八、java中的import static使用

    当在java中如果使用类的static方法和static变量时,可以import static该方法或者是变量。在代码中可以不用className.变量,而可以直接使用。下面是一个例子。

    import static com.linux.huhx.utils.RegrexUtils.*;
    
    String requestData = messageFormat(action, arrays[0], arrays[1]);

    而在RegrexUtils中的messageFormat是一个static方法定义如下:

    public static String messageFormat(String string, String... replace) {
        if (ArrayUtils.isEmpty(replace)) {
            return string;
        }
        for (int i = 0; i < replace.length; i++) {
            string = string.replace("{" + i + "}", replace[i]);
        }
        return string;
    }

    这里本来是想使用MessageFormat.format方法的,但是在解析某些字符串时,出现了问题。

    九、关于Servlet中的请求对象的一些路径的方法

    访问的url:http://localhost:8080/ListenerTest1/servlet/FirstServlet,ListenerTest1是项目名。

    String contextPath = request.getContextPath();
    String servletPath = request.getServletPath();
    String realPath = request.getServletContext().getRealPath("/");

    运行的结果如下:

    /ListenerTest1
    /servlet/FirstServlet
    J:projectwebapache-tomcat-7.0.67-windows-x64apache-tomcat-7.0.67webappsListenerTest1

     友情链接

  • 相关阅读:
    leetcode 122. Best Time to Buy and Sell Stock II
    leetcode 121. Best Time to Buy and Sell Stock
    python 集合(set)和字典(dictionary)的用法解析
    leetcode 53. Maximum Subarray
    leetcode 202. Happy Number
    leetcode 136.Single Number
    leetcode 703. Kth Largest Element in a Stream & c++ priority_queue & minHeap/maxHeap
    [leetcode]1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
    正则表达式
    十种排序算法
  • 原文地址:https://www.cnblogs.com/huhx/p/basediary20170514.html
Copyright © 2011-2022 走看看