Base64-Note

  • js加密: 用FileReader中的readAsDataURL(file)即可, 但注意, 得到的字符串中, 第一个(也是唯一一个),之前保存的是文件类型和加密类型, 之后保存的才是加密后的字符串

    • FileReader: The FileReader object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or data to read.
      • 异步方法的处理: 当FileReader触发了一些事件后, 会返回相应的数据:
        • onload: 加载完成时触发, 会返回结果
        • onerror: 读取操作发生错误时触发
        • 等等
    • 例子:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      var reader = new FileReader();
      reader.readAsDataURL(fileObj);
      reader.onload = function () {
      var encodeString = reader.result;
      console.log(encodeString);
      customFormField1.fieldName = "customFormField1Name";
      customFormField1.fieldValue = "customFormField1Value";
      customFormField0.fieldName = "customFormField0Name";
      customFormField0.fieldValue = "customFormField0Value";
      customFormFileField0.name = "customFormFileField0Name";
      customFormFileField0.encodedContent = encodeString;
      customFormFileField0.postfix = "docx";
      customFormFields[0] = customFormField0;
      customFormFields[1] = customFormField1;
      customFiles[0] = customFormFileField0;
      customForm.customFormFields = customFormFields;
      customForm.type = 0;
      customForm.senderId = 19;
      request.customForm = customForm;
      request.customFiles = customFiles;
      request.integer = 7;
      request.string = "秘书长";
      request = JSON.stringify(request);
      $.ajax({
      type: "POST",
      url: "http://192.168.0.113:8090/form/addForm.jspx",
      data: request,
      dataType: "json",
      contentType: "application/json",
      cache: false,
      processData: false,
      success: function (data) {
      console.log("success: " + data);
      },
      error: function (data) {
      console.log("fail: " + data);
      }
      });
      }
  • java

    • 加密:
      • 先使用NIO中的Files.readAllBytes读取文件为byte[]
      • 再用java.util中的Base64.getEncoder().encodeToString()加密
    • 解密:
      • 如果加密的string中包含文件类型和加密类型信息, 先截取掉
      • 利用Base64.getDecoder().decode将String解密成byte[]
      • 利用Files.write将byte[]写入指定位置

Java-Note-NIO中的Files

  • 介绍: This class consists exclusively of static methods that operate on files, directories, or other types of files. In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.
  • 包含的方法:
    • Files.readAllBytes(): 根据路径读取所有byte, 实现中用到大量Channel
    • Files.write(): 向给定路径写入给定byte, StandardOpenOption defines the standard open options

Java-Note-泛型的尖括号中没有类的问题

  • 这其实是一个语法糖, 可以简化代码
  • 详见:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38

    MORE LIKE THIS
    6-7-8 hopscotch
    Java 101: The essential Java language features tour, Part 5
    A stack of coins
    Java 9's other new enhancements, Part 2: Milling Project Coin
    Detecting Class Innards in Groovy
    RELATED ARTICLES
    cloud ladder climb sky
    AWS Lambda tutorial: Get started with serverless computing
    NASA Apollo 8 - December 24, 1968 - Earthrise
    Cosmos DB review: Database for a small planet
    open source license primary.jpg
    GitHub tutorial: Get started with GitHub
    See all Insider
    Project Coin provides numerous "small language enhancements" as a subset of the new JDK 7 features. I recently blogged on Project Coin's switching on Strings and in this post I write about the new Diamond Operator (<>).

    The Diamond Operator reduces some of Java's verbosity surrounding generics by having the compiler infer parameter types for constructors of generic classes. The original proposal for adding the Diamond Operator to the Java language was made in February 2009 and includes this simple example:

    For example, consider the following assignment statement:

    Map<String, List<String>> anagrams = new HashMap<String, List<String>>();

    This is rather lengthy, so it can be replaced with this:

    Map<String, List<String>> anagrams = new HashMap<>();

    The above example provided in Jeremy Manson's proposal (which was one of the first in response to a call for Project Coin ideas) is simple, but adequately demonstrates how the Diamond Operator is applied in JDK 7. Manson's proposal also provides significant into why this addition was desirable:

    The requirement that type parameters be duplicated unnecessarily like

    this encourages an unfortunate

    overabundance of static factory methods, simply because type inference

    works on method invocations.

    In other words, the JDK 7 Project Coin addition of a Diamond Operator brings type inference to constructors that has been available with methods. With methods type inference is implicitly done when one leaves off the explicit parameter type specification. With instantiation, on the other hand, the diamond operator must be specified explicitly to "tell" the compiler to infer the type.
  • 参考文档: https://www.javaworld.com/article/2074080/core-java/jdk-7--the-diamond-operator.html

Java-Note-Mybatis-resultSet中的数据映射到collection中有相同字段的resultMap中出现的问题

  • 原因: 当resultMap中包含collection时, 如果collection中的字段的名称与resultMap中的相同且未对字段名用as重命名时, resultSet映射到resultMap上时, 会同时映射到resultMap和collection中相同的字段
  • 解决方法: 改column的名称
  • 例:

Read More

Java-Note-Mybatis-无法使用insert一次插入一对多的数据

  • 原因: 一次性插入一对多的数据

Java-Note-Mybatis-resultSet中一对多关系的转换

  • 使用collection来将一对多的result set转换为实体类最后转化为json
  • 例子:

    • result set:

      1
      2
      3
      4
      5
      Columns: id, gmt_create, gmt_modify, is_active, sender_id, receiver_id, type, is_agreed, process_url, process_reason, id, gmt_create, gmt_modify, is_active, form_id, field_name, field_value
      Row: 1, 2018-04-18 18:58:06.0, 2018-04-18 18:58:06.0, 1, 19, 18, 0, null, null, null, 1, 2018-04-18 19:01:52.0, 2018-04-18 19:05:15.0, 1, 1, name, 德玛西亚
      Row: 1, 2018-04-18 18:58:06.0, 2018-04-18 18:58:06.0, 1, 19, 18, 0, null, null, null, 2, 2018-04-18 19:02:04.0, 2018-04-18 19:05:02.0, 1, 1, gender, 男
      Row: 1, 2018-04-18 18:58:06.0, 2018-04-18 18:58:06.0, 1, 19, 18, 0, null, null, null, 3, 2018-04-18 19:02:10.0, 2018-04-18 19:04:58.0, 1, 1, nation, 中国
      Row: 1, 2018-04-18 18:58:06.0, 2018-04-18 18:58:06.0, 1, 19, 18, 0, null, null, null, 4, 2018-04-18 19:02:16.0, 2018-04-18 19:04:54.0, 1, 1, birth, 19910526
    • 转换后的json:

Read More

Java-Note-反射原理

  1. 通过Class类的getDeclaredMethod可以获取指定方法名和参数的方法对象Method

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public Method getDeclaredMethod(String name, Class<?>... parameterTypes)
    throws NoSuchMethodException, SecurityException {
    checkMemberAccess(Member.DECLARED, Reflection.getCallerClass(), true);
    Method method = searchMethods(privateGetDeclaredMethods(false), name, parameterTypes);
    if (method == null) {
    throw new NoSuchMethodException(getName() + "." + name + argumentTypesToString(parameterTypes));
    }
    return method;
    }
    1. 其中privateGetDeclaredMethods方法从缓存或JVM中获取该Class中声明的方法列表,

Read More

Java-Note-Mybatis-SqlSessionFactory和SqlSession

  • SqlSessionFactory: 是Mybatis的关键对象, 它是单个数据库映射关系经过编译后的内存镜像. SqlSessionFactory对象的实例可以通过SqlSessionFactoryBuilder对象类获得, 而SqlSessionFactoryBuilder则可以从XML配置文件或一个预先定制的Configuration的实例构建出SqlSessionFactory的实例, 每一个Mybatis的应用程序都以一个SqlSessionFactory对象的实例为核心, 同时SqlSessionFactory也是线程安全的, SqlSessionFactory是创建SqlSession的工厂
  • SqlSession: 是MyBatis的关键对象, 是执行持久化操作的独享, SqlSession对象完全包含以数据库为背景的所有执行SQL操作的方法, 其底层封装了JDBC连接, 可以用SqlSession实例来直接执行被映射的SQL语句.
Read More

Java-Note-事务管理

  • 一个数据库事务通常包含了一个序列的对数据库的读/写操作. 它的存在包含有以下两个目的:
    1. 为数据库操作序列提供了一个从失败中恢复到正常状态的方法, 同时提供了数据库即使在异常状态下仍能保持一致性的方法
    2. 当多个应用程序在并发访问数据库时, 可以在这些应用程序之间提供一个隔离方法, 以防止彼此的操作互相干扰
  • ACID性质
    • 原子性(Atomicity): 事务作为一个整体被执行, 包含在其中的对数据库的操作要么全部被执行, 要么都不执行
    • 一致性(Consistency): 事务应确保数据库的状态从一个一致状态转变为另一个一致状态. 一致状态的含义是数据库中的数据应满足完整性约束
    • 隔离性(Isolation): 多个事务并发执行时, 一个事务的执行不应该影响其他事务的执行
    • 持久性(Durability): 已被提交的事务对数据库的修改应该永久保存在数据库中
  • 事务并发
Read More

Java-Note-类加载

  • 类加载过程: 每个编写的”.java”拓展名类文件都存储着需要执行的程序逻辑,这些”.java”文件经过Java编译器编译成拓展名为”.class”的文件,”.class”文件中保存着Java代码经转换后的虚拟机指令,当需要使用某个类时,虚拟机将会加载它的”.class”文件,并创建对应的class对象,将class文件加载到虚拟机的内存,这个过程称为类加载
  • 类加载过程分解:
Read More