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. android onConfigurationChanged讲解

    本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 相信大家对这个属性已经耳熟能详,如果大家受过转屏的折磨的话! 老规矩,先讲讲官方文档是怎么说的.为什 ...

  2. MVC 构造新Model实现内容搜索

    当前在使用MVC开发一个网站,习惯了使用ASP.NET中控件,转到MVC之后突然对于页面和后台代码的传值感觉无从下手.花了点时间在网上看了写帖子后,想到了一个方法,重新构造一个新Model, 然后利用 ...

  3. 转: ASP.NET+ExtJs4.0+表单提交submit,上传图片到服务器

    http://blog.csdn.net/lmaohuanl/article/details/6792057 <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...

  4. 解决github push错误The requested URL returned error: 403 Forbidden while accessing

    来源:http://blog.csdn.net/happyteafriends/article/details/11554043 github push错误: git push error: The  ...

  5. Kubuntu麦克风音频无声音

    前段时间买了新本,装了双系统,win8和kubuntu 14.04,使用的过程感觉都不错,因为平时玩游戏看视频是用win8,但最近打算在kubuntu上听音乐时,发现音频没有声音,麦克风也没有声音,这 ...

  6. Linux上的free命令学习

    Linux新手,今天使用了free命令来查看电脑内存的使用情况.如下:-m表示以M来显示. 1.基本信息介绍 (1)其中纵向信息: Mem:表示物理内存大小 -/+ buffers/cached:表示 ...

  7. 7-zip的压缩的时候排除某目录

    安装暂且不说了. 看一下帮助. [root@localhost Server]# 7z -Zip [] - Igor Pavlov -- p7zip Version ,Utf16=on,HugeFil ...

  8. HDU 5944 暴力

    Fxx and string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)T ...

  9. 2015GitWebRTC编译实录4

    2015.07.17 libg711 编译通过[422/1600 ] CC obj /webrtc/modules/audio_coding/codecs/g711/g711.g711.o[423/1 ...

  10. MySQL复制的基本概念和实现

    MySQL的复制的概念是完成水平扩展的架构 MySQL性能方面的扩展方式有scale on(向上扩展,垂直扩展)                          scale out(向外扩展,水平扩 ...