zoukankan      html  css  js  c++  java
  • 常用方法

    一、文件导出、下载的时候,文件名有时候会中文乱码或不显示,

    1 /**
    2  *@Description 文件导出、下载  防止中文乱码或不显示
    3  */  
    4     public static ResponseEntity<byte[]> download(String fileName, File file) throws IOException {
    5         HttpHeaders headers = new HttpHeaders(); 
    6         headers.setContentType(MediaType.APPLICATION_OCTET_STREAM); 
    7         headers.setContentDispositionFormData("attachment", new String(fileName.getBytes(), "ISO8859-1"));
    8         return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), headers, HttpStatus.CREATED); 
    9         }

    二、给下载或导出的文件名加当前时间

    1 /**
    2      * 查询日期字符串
    3      * @return String 带前缀的日期字符串
    4      */
    5     public static String getDateString(String prefix){
    6         SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
    7         Date today = new Date();
    8         return prefix + formatter.format(today);
    9     }

    三、数据加、解密,算法(DES)

     1 /**
     2      * DES算法密钥
     3      */
     4     private static final byte[] DES_KEY = { 21, 1, -110, 82, -32, -85, -128, -65 };
     5 
     6     
     7     /**
     8      * 数据加密,算法(DES)
     9      * 
    10      * @param data
    11      *            要进行加密的数据
    12      * @return 加密后的数据
    13      */
    14     public static String encryptBasedDes(String data) {
    15         String encryptedData = null;
    16         try {
    17             // DES算法要求有一个可信任的随机数源
    18             SecureRandom sr = new SecureRandom();
    19             DESKeySpec deskey = new DESKeySpec(DES_KEY);
    20             // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
    21             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    22             SecretKey key = keyFactory.generateSecret(deskey);
    23             // 加密对象
    24             Cipher cipher = Cipher.getInstance("DES");
    25             cipher.init(Cipher.ENCRYPT_MODE, key, sr);
    26             // 加密,并把字节数组编码成字符串
    27             encryptedData = new BASE64Encoder().encode(cipher.doFinal(data.getBytes()));
    28         } catch (Exception e) {
    29             log.error("加密错误,错误信息:", e);
    30             throw new RuntimeException("加密错误,错误信息:", e);
    31         }
    32         return encryptedData;
    33     }
    34 
    35     /**
    36      * 数据解密,算法(DES)
    37      * 
    38      * @param cryptData
    39      *            加密数据
    40      * @return 解密后的数据
    41      */
    42     public static String decryptBasedDes(String cryptData) {
    43         String decryptedData = null;
    44         try {
    45             // DES算法要求有一个可信任的随机数源
    46             SecureRandom sr = new SecureRandom();
    47             DESKeySpec deskey = new DESKeySpec(DES_KEY);
    48             // 创建一个密匙工厂,然后用它把DESKeySpec转换成一个SecretKey对象
    49             SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
    50             SecretKey key = keyFactory.generateSecret(deskey);
    51             // 解密对象
    52             Cipher cipher = Cipher.getInstance("DES");
    53             cipher.init(Cipher.DECRYPT_MODE, key, sr);
    54             // 把字符串解码为字节数组,并解密
    55             decryptedData = new String(cipher.doFinal(new sun.misc.BASE64Decoder().decodeBuffer(cryptData)));
    56         } catch (Exception e) {
    57             log.error("解密错误,错误信息:", e);
    58             throw new RuntimeException("解密错误,错误信息:", e);
    59         }
    60         return decryptedData;
    61     }

    四、排序,以一个对象集合中的对象的某个属性进行排序

      1、该属性为字符串类型String

    1 Collections.sort(list,new Comparator<T>() {
    2 
    3             @Override
    4             public int compare(T t1, T t2){
    5                 
    6                 return -1*t1.getString().compareTo(t2.getString());
    7             }
    8         });

     五、将一个list集合分页

     /**
          * list集合实现分页
         * @param <T>
          * @param pageNo  当前页码
          * @param pageSize 每页显示记录数
          * @param list  集合,数据源
          * @return
          * @throws Exception
          */
         public static <T> List<T> page(int pageNo,int pageSize,List<T> list) throws Exception{
          List<T> result = new ArrayList<T>();
          if(list != null && list.size() > 0){
           int allCount = list.size();
           int pageCount = (allCount + pageSize-1) / pageSize;
           if(pageNo >= pageCount){
            pageNo = pageCount;
           }
           int start = (pageNo-1) * pageSize;
           int end = pageNo * pageSize;
           if(end >= allCount){
            end = allCount;
           }
           for(int i = start; i < end; i ++){
            result.add(list.get(i));
           }
          }
          return (result != null && result.size() > 0) ? result : null;
         }
  • 相关阅读:
    __attribute__ 总结
    linux文件夹打包命令
    学习ARM的一些基本知识,个人整理
    备忘录之 —— .bashrc(IC工具篇)
    GitHub的基本使用
    02: SocketServer服务
    01: socket模块
    08: python基础练习题
    07: 高阶函数&异常处理
    06: 面向对象
  • 原文地址:https://www.cnblogs.com/my0901/p/7116774.html
Copyright © 2011-2022 走看看