1. package com.util;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.Calendar;
  10. /**
  11. * 文件上传工具类
  12. *
  13. */
  14. public class UploadUtil {
  15. private static final int BUFFER_SIZE = 16 * 1024;
  16. //保存图片
  17. public static synchronized void copy(File src, File newFile) {
  18. try {
  19. InputStream is = null;
  20. OutputStream os = null;
  21. try {
  22. is = new BufferedInputStream(new FileInputStream(src),
  23. BUFFER_SIZE);
  24. os = new BufferedOutputStream(new FileOutputStream(newFile),
  25. BUFFER_SIZE);
  26. byte[] buffer = new byte[BUFFER_SIZE];
  27. while (is.read(buffer) > 0) {
  28. os.write(buffer);
  29. }
  30. } finally {
  31. if (null != is) {
  32. is.close();
  33. }
  34. if (null != os) {
  35. os.close();
  36. }
  37. }
  38. } catch (Exception e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. /**
  43. * 返回 年号+月号+天+时+分+秒+随机码
  44. * @return
  45. */
  46. @SuppressWarnings("static-access")
  47. public static synchronized String getTime() {
  48. Calendar calendar = Calendar.getInstance();
  49. String year = calendar.get(calendar.YEAR) + "";
  50. String month = (calendar.get(calendar.MONTH) + 1) + "";
  51. String day = calendar.get(calendar.DAY_OF_MONTH) + "";
  52. String hour = calendar.get(calendar.HOUR_OF_DAY) + "";
  53. String minute = calendar.get(calendar.MINUTE) + "";
  54. String second = calendar.get(calendar.SECOND) + "";
  55. String milliSecond = calendar.get(calendar.MILLISECOND) + "";
  56. int r = (int)(Math.random()*100000);
  57. String random = String.valueOf(r);
  58. return year + month + day + hour + minute + second + milliSecond + random+"a";
  59. }
  60. }

文件上传工具类 UploadUtil.java的更多相关文章

  1. spring mvc 文件上传工具类

    虽然文件上传在框架中,已经不是什么困难的事情了,但自己还是开发了一个文件上传工具类,是基于springmvc文件上传的. 工具类只需要传入需要的两个参数,就可以上传到任何想要上传的路径: 参数1:Ht ...

  2. spring boot 文件上传工具类(bug 已修改)

    以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...

  3. FastDFS 文件上传工具类

    FastDFS文件上传工具类 import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; imp ...

  4. Java 使用 commons-fileupload 实现文件上传工具类

    依赖包 文件上传可以使用 Apache 文件上传组件, commons-fileupload, 它依赖于 commons-io commons-io.jar: https://repo1.maven. ...

  5. Java一个文件上传工具类

    /** * 文件上传 * * @author cary * @since 2012-12-19 下午2:22:12 */ public class FileUploader { static fina ...

  6. commons-fileload图片文件上传工具 , servlet文件图片上传案列

    本案列是java  maven工程小项目,提供个大家学习! 1.在pom.xml文件中导入依赖: <!--文件上传依赖--><dependency> <groupId&g ...

  7. 文件上传工具swfupload[转]

    转至:http://zhangqgc.iteye.com/blog/906419 文件上传工具swfupload 示例: 1.JavaScript设置SWFUpload部分(与官方例子类似): var ...

  8. PHP 图片上传工具类(支持多文件上传)

    ====================ImageUploadTool======================== <?php class ImageUploadTool { private ...

  9. [原创]Struts2奇葩环境任意文件上传工具(解决菜刀无法传文件或上传乱码等问题)

    上面这问题问得好  1 不知道大家有没碰到有些Strus2站点  上传JSP后访问404 或者503    注意我说的是404或503不是403(要是403换个css/img等目录或许可以)    但 ...

随机推荐

  1. apache2将http自动指向https

    <VirtualHost *:80> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_H ...

  2. <td colspan="2" > 一个td占两个 td空间

    <tr> <td>机构名称: ${accreditInfo.companyName}</td> <td>初始授信额度: ${accreditInfo.i ...

  3. Tomcat9源码编译及导入Eclipse(转)

    1.下载tomcat源码.建议下载最新版本tomcat9. svn地址:http://svn.apache.org/repos/asf/tomcat/tc9.0.x/branches/gsoc-jas ...

  4. MBR主引导扇区解析

    最近在制作镜像的时候由于需要简单研究了下MBR主引导扇区的结构,这里记录下便于后期温习,下面就直接进入正题: MBR主引导扇区位于磁盘的第一个扇区,即0号扇区,主要由引导代码.分区表.结束标志三部分构 ...

  5. Cloud Foundry 在 Azure 中国正式发布

    Cloud Foundry 今天在 Azure 中国上正式发布了!这对于 Azure 平台,以及开源社区都是一个令人振奋的里程碑. Cloud Foundry 简化了云计算应用程序的构建,测试,发布和 ...

  6. DataTableToExcel

    public static string CreateExcel(DataTable dt, string FileName, string path, string columns)         ...

  7. web项目中各种路径的获取HttpServletRequest

    以工程名为/DemoWeb为例: 访问的jsp为:http://localhost:8080/DemoWeb/test/index.jsp 1 JSP中获得当前应用的相对路径和绝对路径 (1)得到工程 ...

  8. MySQL常用配置

    查看MySQL的参数信息 mysql> show variables; 查看key_buffer_size的使用情况 mysql> show status like 'key_read%' ...

  9. 全国信息学奥林匹克联赛(NOIP2014)复赛 模拟题Day2 长乐一中

    题目名称 改造二叉树 数字对 交换 英文名称 binary pair swap 输入文件名 binary.in pair.in swap.in 输出文件名 binary.out pair.out sw ...

  10. 解决dede搜索页面只能显示10条信息解决方案

    解决dede搜索页面只能显示10条信息解决方案,感觉显示的信息太少,这时就要想办法去解决一下.看看有什么好办法来解决一下这个问题. dede搜索页模板中,默认只能显示10条记录. 打开dede搜索页模 ...