文件上传工具类 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等目录或许可以) 但 ...
随机推荐
- apache2将http自动指向https
<VirtualHost *:80> RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_H ...
- <td colspan="2" > 一个td占两个 td空间
<tr> <td>机构名称: ${accreditInfo.companyName}</td> <td>初始授信额度: ${accreditInfo.i ...
- Tomcat9源码编译及导入Eclipse(转)
1.下载tomcat源码.建议下载最新版本tomcat9. svn地址:http://svn.apache.org/repos/asf/tomcat/tc9.0.x/branches/gsoc-jas ...
- MBR主引导扇区解析
最近在制作镜像的时候由于需要简单研究了下MBR主引导扇区的结构,这里记录下便于后期温习,下面就直接进入正题: MBR主引导扇区位于磁盘的第一个扇区,即0号扇区,主要由引导代码.分区表.结束标志三部分构 ...
- Cloud Foundry 在 Azure 中国正式发布
Cloud Foundry 今天在 Azure 中国上正式发布了!这对于 Azure 平台,以及开源社区都是一个令人振奋的里程碑. Cloud Foundry 简化了云计算应用程序的构建,测试,发布和 ...
- DataTableToExcel
public static string CreateExcel(DataTable dt, string FileName, string path, string columns) ...
- web项目中各种路径的获取HttpServletRequest
以工程名为/DemoWeb为例: 访问的jsp为:http://localhost:8080/DemoWeb/test/index.jsp 1 JSP中获得当前应用的相对路径和绝对路径 (1)得到工程 ...
- MySQL常用配置
查看MySQL的参数信息 mysql> show variables; 查看key_buffer_size的使用情况 mysql> show status like 'key_read%' ...
- 全国信息学奥林匹克联赛(NOIP2014)复赛 模拟题Day2 长乐一中
题目名称 改造二叉树 数字对 交换 英文名称 binary pair swap 输入文件名 binary.in pair.in swap.in 输出文件名 binary.out pair.out sw ...
- 解决dede搜索页面只能显示10条信息解决方案
解决dede搜索页面只能显示10条信息解决方案,感觉显示的信息太少,这时就要想办法去解决一下.看看有什么好办法来解决一下这个问题. dede搜索页模板中,默认只能显示10条记录. 打开dede搜索页模 ...