zoukankan      html  css  js  c++  java
  • 文件上传工具类 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. }  
  • 相关阅读:
    关于工作
    5种风格的 jQuery 分页效果【附代码】
    轮播图jquery
    Microsoft.AspNetCore.Localization 自定义多语言获取
    10-微信小程序 WXS
    9-微信小程序引用
    8-微信小程序 事件
    7-微信小程序 模板(template)
    6-微信小程序 条件渲染 wx:if
    5-微信小程序 列表渲染 wx:for
  • 原文地址:https://www.cnblogs.com/swite/p/5168719.html
Copyright © 2011-2022 走看看