文件上传工具类 UploadUtil.java
- package com.util;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.Calendar;
- /**
- * 文件上传工具类
- *
- */
- public class UploadUtil {
- private static final int BUFFER_SIZE = 16 * 1024;
- //保存图片
- public static synchronized void copy(File src, File newFile) {
- try {
- InputStream is = null;
- OutputStream os = null;
- try {
- is = new BufferedInputStream(new FileInputStream(src),
- BUFFER_SIZE);
- os = new BufferedOutputStream(new FileOutputStream(newFile),
- BUFFER_SIZE);
- byte[] buffer = new byte[BUFFER_SIZE];
- while (is.read(buffer) > 0) {
- os.write(buffer);
- }
- } finally {
- if (null != is) {
- is.close();
- }
- if (null != os) {
- os.close();
- }
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- /**
- * 返回 年号+月号+天+时+分+秒+随机码
- * @return
- */
- @SuppressWarnings("static-access")
- public static synchronized String getTime() {
- Calendar calendar = Calendar.getInstance();
- String year = calendar.get(calendar.YEAR) + "";
- String month = (calendar.get(calendar.MONTH) + 1) + "";
- String day = calendar.get(calendar.DAY_OF_MONTH) + "";
- String hour = calendar.get(calendar.HOUR_OF_DAY) + "";
- String minute = calendar.get(calendar.MINUTE) + "";
- String second = calendar.get(calendar.SECOND) + "";
- String milliSecond = calendar.get(calendar.MILLISECOND) + "";
- int r = (int)(Math.random()*100000);
- String random = String.valueOf(r);
- return year + month + day + hour + minute + second + milliSecond + random+"a";
- }
- }
文件上传工具类 UploadUtil.java的更多相关文章
- spring mvc 文件上传工具类
虽然文件上传在框架中,已经不是什么困难的事情了,但自己还是开发了一个文件上传工具类,是基于springmvc文件上传的. 工具类只需要传入需要的两个参数,就可以上传到任何想要上传的路径: 参数1:Ht ...
- spring boot 文件上传工具类(bug 已修改)
以前的文件上传都是之前前辈写的,现在自己来写一个,大家可以看看,有什么问题可以在评论中提出来. 写的这个文件上传是在spring boot 2.0中测试的,测试了,可以正常上传,下面贴代码 第一步:引 ...
- FastDFS 文件上传工具类
FastDFS文件上传工具类 import org.csource.common.NameValuePair; import org.csource.fastdfs.ClientGlobal; imp ...
- Java 使用 commons-fileupload 实现文件上传工具类
依赖包 文件上传可以使用 Apache 文件上传组件, commons-fileupload, 它依赖于 commons-io commons-io.jar: https://repo1.maven. ...
- Java一个文件上传工具类
/** * 文件上传 * * @author cary * @since 2012-12-19 下午2:22:12 */ public class FileUploader { static fina ...
- commons-fileload图片文件上传工具 , servlet文件图片上传案列
本案列是java maven工程小项目,提供个大家学习! 1.在pom.xml文件中导入依赖: <!--文件上传依赖--><dependency> <groupId&g ...
- 文件上传工具swfupload[转]
转至:http://zhangqgc.iteye.com/blog/906419 文件上传工具swfupload 示例: 1.JavaScript设置SWFUpload部分(与官方例子类似): var ...
- PHP 图片上传工具类(支持多文件上传)
====================ImageUploadTool======================== <?php class ImageUploadTool { private ...
- [原创]Struts2奇葩环境任意文件上传工具(解决菜刀无法传文件或上传乱码等问题)
上面这问题问得好 1 不知道大家有没碰到有些Strus2站点 上传JSP后访问404 或者503 注意我说的是404或503不是403(要是403换个css/img等目录或许可以) 但 ...
随机推荐
- android onConfigurationChanged讲解
本文来自http://blog.csdn.net/liuxian13183/ ,引用必须注明出处! 相信大家对这个属性已经耳熟能详,如果大家受过转屏的折磨的话! 老规矩,先讲讲官方文档是怎么说的.为什 ...
- MVC 构造新Model实现内容搜索
当前在使用MVC开发一个网站,习惯了使用ASP.NET中控件,转到MVC之后突然对于页面和后台代码的传值感觉无从下手.花了点时间在网上看了写帖子后,想到了一个方法,重新构造一个新Model, 然后利用 ...
- 转: ASP.NET+ExtJs4.0+表单提交submit,上传图片到服务器
http://blog.csdn.net/lmaohuanl/article/details/6792057 <!DOCTYPE html PUBLIC "-//W3C//DTD XH ...
- 解决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 ...
- Kubuntu麦克风音频无声音
前段时间买了新本,装了双系统,win8和kubuntu 14.04,使用的过程感觉都不错,因为平时玩游戏看视频是用win8,但最近打算在kubuntu上听音乐时,发现音频没有声音,麦克风也没有声音,这 ...
- Linux上的free命令学习
Linux新手,今天使用了free命令来查看电脑内存的使用情况.如下:-m表示以M来显示. 1.基本信息介绍 (1)其中纵向信息: Mem:表示物理内存大小 -/+ buffers/cached:表示 ...
- 7-zip的压缩的时候排除某目录
安装暂且不说了. 看一下帮助. [root@localhost Server]# 7z -Zip [] - Igor Pavlov -- p7zip Version ,Utf16=on,HugeFil ...
- HDU 5944 暴力
Fxx and string Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)T ...
- 2015GitWebRTC编译实录4
2015.07.17 libg711 编译通过[422/1600 ] CC obj /webrtc/modules/audio_coding/codecs/g711/g711.g711.o[423/1 ...
- MySQL复制的基本概念和实现
MySQL的复制的概念是完成水平扩展的架构 MySQL性能方面的扩展方式有scale on(向上扩展,垂直扩展) scale out(向外扩展,水平扩 ...