• 文件上传工具类 UploadUtil.java


    1. package com.util;  
    2.   
    3. import java.io.BufferedInputStream;  
    4. import java.io.BufferedOutputStream;  
    5. import java.io.File;  
    6. import java.io.FileInputStream;  
    7. import java.io.FileOutputStream;  
    8. import java.io.InputStream;  
    9. import java.io.OutputStream;  
    10. import java.util.Calendar;  
    11.   
    12. /** 
    13.  * 文件上传工具类 
    14.  * 
    15.  */  
    16. public class UploadUtil {  
    17.     private static final int BUFFER_SIZE = 16 * 1024;  
    18.     //保存图片  
    19.     public static synchronized void copy(File src, File newFile) {  
    20.           
    21.         try {  
    22.             InputStream is = null;  
    23.             OutputStream os = null;  
    24.             try {  
    25.                 is = new BufferedInputStream(new FileInputStream(src),  
    26.                         BUFFER_SIZE);  
    27.                 os = new BufferedOutputStream(new FileOutputStream(newFile),  
    28.                         BUFFER_SIZE);  
    29.                 byte[] buffer = new byte[BUFFER_SIZE];  
    30.                 while (is.read(buffer) > 0) {  
    31.                     os.write(buffer);  
    32.                 }  
    33.             } finally {  
    34.                 if (null != is) {  
    35.                     is.close();  
    36.                 }  
    37.                 if (null != os) {  
    38.                     os.close();  
    39.                 }  
    40.             }  
    41.         } catch (Exception e) {  
    42.             e.printStackTrace();  
    43.         }  
    44.     }  
    45.   
    46.     /** 
    47.      * 返回 年号+月号+天+时+分+秒+随机码 
    48.      * @return 
    49.      */  
    50.     @SuppressWarnings("static-access")  
    51.     public static synchronized String getTime() {  
    52.         Calendar calendar = Calendar.getInstance();  
    53.         String year = calendar.get(calendar.YEAR) + "";  
    54.         String month = (calendar.get(calendar.MONTH) + 1) + "";  
    55.         String day = calendar.get(calendar.DAY_OF_MONTH) + "";  
    56.         String hour = calendar.get(calendar.HOUR_OF_DAY) + "";  
    57.         String minute = calendar.get(calendar.MINUTE) + "";  
    58.         String second = calendar.get(calendar.SECOND) + "";  
    59.         String milliSecond = calendar.get(calendar.MILLISECOND) + "";  
    60.         int r = (int)(Math.random()*100000);  
    61.         String random = String.valueOf(r);  
    62.         return year + month + day + hour + minute + second + milliSecond + random+"a";  
    63.     }  
    64.   
    65. }  
  • 相关阅读:
    快排算法的一点思考
    imglab .xml 标签格式转coco .json格式
    Ubuntu18.04 编译 sparse-ncnet
    Detectron2 keypoint_rcnn 网络c++版本部署
    技术部斗争
    我的程序人生
    关于ddd落地体验
    DevOps关于制定适合自身生产环境的redis集群
    从前端到后端的跨域攻击与防御
    DKIM对发送邮件的好处及使用方法
  • 原文地址:https://www.cnblogs.com/swite/p/5168719.html
Copyright © 2020-2023  润新知