文件上传action处理:

boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (isMultipart) {
DiskFileItemFactory factory = new DiskFileItemFactory();
factory.setSizeThreshold(2048);
// 设置临时文件路径,通过设置临时文件可以在上传大文件时候达到多少先写入临时文件,不会造成卡死情况
String tempPath = "/mnt/disk1/tomcat/tomcat1/upload/tmp/";
File tempFile = new File(tempPath);
if (!tempFile.exists())
tempFile.mkdirs();
factory.setRepository(tempFile);
ServletFileUpload upload = new ServletFileUpload(factory);
       //设置可上传文件大小 
upload.setSizeMax(1024000000);
        //设置文件进度监听器
UploadProgressListener progressListener = new UploadProgressListener();
        //放入session中,下次访问可以取出,获取文件进度
session.setAttribute("progress", progressListener);
upload.setProgressListener(progressListener);
List<FileItem> fileldItems = null;
try {
fileldItems = upload.parseRequest(request);
} catch (FileUploadException e1) {
e1.printStackTrace();
}
Map<String, String> fieldMap = new HashMap<String, String>();
List<FileItem> fileList = new ArrayList<FileItem>();
Iterator<FileItem> iter = fileldItems.iterator();
StringBuffer url = new StringBuffer("request=");
url.append(request.getRequestURI());
while (iter.hasNext()) {
FileItem item = iter.next();
          //获取非文件字段
if (item.isFormField()) {
String name = item.getFieldName();
String value = item.getString();
url.append("&" + name + "=" + value);
fieldMap.put(name, value);
} else {
fileList.add(item);
}
}
      //记录非文件字段
logger.error(url.toString().replaceFirst("&", "?"));
for (FileItem item : fileList) {
String fileName = item.getName();
if (fileName == null || fileName.equals(""))
continue;
fileName = fileName
.substring(fileName.lastIndexOf("\\") + 1);
String ext = fileName.substring(fileName.lastIndexOf("."));
fileName = fileName.substring(0, fileName.lastIndexOf("."));
String dateStr = DateUtil.formatDate(new Date(),
"yyyyMMddHHmmss");
String filename = fileName + "_" + dateStr + ext;
String filePath = "";
filePath = "/mnt/disk1/tomcat/tomcat1/upload/files/"
+ DecoderUtil.toChinese(fieldMap.get("user"));
File fp = new File(filePath);
if (!fp.exists())
fp.mkdirs();
File uploadedFile = new File(fp, filename);
try {
item.write(uploadedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
ProgressListener:由于我这个不只记录了文件进度,还有别的信息,懒得删了,就这样黏上来了,可以根据情况自己修改。
package lbi.flow.util;

import java.text.DecimalFormat;

import org.apache.commons.fileupload.ProgressListener;

public class UploadProgressListener implements ProgressListener {
private long megaBytes = -1;
private long readBytes = 0;
private long totalBytes = 0;
private long processedCount = 0;
private long totalCount = 0;
private String speed = "0";// k/s
private long starttime;
private int step = 1; private final DecimalFormat df = new DecimalFormat("0.##%");
private final DecimalFormat df2 = new DecimalFormat("0.##"); public UploadProgressListener() {
this.starttime = (long) System.currentTimeMillis();
} public String getProgress() {
if (this.step == 1)
return df.format(this.readBytes * 1.0 / this.totalBytes);
else
return df.format(this.processedCount * 1.0 / this.totalCount);
} public String getOverallProgress() {
if (this.step == 1)
return df.format(this.readBytes * 1.0 / this.totalBytes / 3);
else
return df.format(this.processedCount * 1.0 / this.totalCount / 3
+ (this.step - 1) * 1.0 / 3);
} public void update(long pBytesRead, long pContentLength, int pItems) {
long mBytes = pBytesRead / 1000000;
if (megaBytes == mBytes && pBytesRead != pContentLength) {
return;
}
megaBytes = mBytes;
long deadline = System.currentTimeMillis();
speed = df2.format(pBytesRead / 1024
/ ((deadline - starttime) * 1.0 / 1000));
readBytes = pBytesRead;
totalBytes = pContentLength;
} public long getProcessedCount() {
return processedCount;
} public void setProcessedCount(long processedCount) {
this.processedCount = processedCount;
} public long getTotalCount() {
return totalCount;
} public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
} public int getStep() {
return step;
} public void setStep(int step) {
this.step = step;
} public String getSpeed() {
return this.speed;
}
}

这样,再写一个接口,通过session获取存储的ProgressListener即可获取上传文件的进度,可以支持多文件上传的。

apache fileupload 文件上传,及文件进度设置获取的更多相关文章

  1. java进行文件上传,带进度条

    网上看到别人发过的一个java上传的代码,自己写了个完整的,附带源码 项目环境:jkd7.tomcat7. jar包:commons-fileupload-1.2.1.jar.commons-io-1 ...

  2. Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)

    Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...

  3. Retrofit2文件上传下载及其进度显示

    序 前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示. 文件上传 定义接口 1 2 3 @Multip ...

  4. 【原创】用JAVA实现大文件上传及显示进度信息

    用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...

  5. SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库

    SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库  /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...

  6. SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)

    林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...

  7. struts2文件上传,文件类型 allowedTypes

    struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...

  8. webAPI文件上传时文件过大404错误的问题

    背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...

  9. servlet多文件上传(带进度条)

    需要commons-fileupload-1.3.jar和commons-io-2.4.jar的支持 页面效果:(图片文件都可以) (1)进度标识类 public class UploadStatus ...

  10. Asp.Net 无刷新文件上传并显示进度条的实现方法及思路

    相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦苦来 实现呢?我并不否认”拿来主义“,只是我个人更喜欢凡是求个所以 ...

随机推荐

  1. (链表 双指针) leetcode 160. Intersection of Two Linked Lists

    Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...

  2. python自动化开发-[第十三天]-前端Css续

    今日概要: 1.伪类选择器 2.选择器优先级 3.vertical-align属性 4.backgroud属性 5.边框border属性 6.display属性 7.padding,margine(见 ...

  3. Tensorflow object detection API 搭建物体识别模型(一)

    一.开发环境 1)python3.5 2)tensorflow1.12.0 3)Tensorflow object detection API :https://github.com/tensorfl ...

  4. Hadoop生态圈-构建企业级平台安全方案

    Hadoop生态圈-构建企业级平台安全方案 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 能看到这篇文章的小伙伴,估计你对大数据集群的部署对于你来说应该是手到擒来了吧.我之前分享过 ...

  5. TensorFlow tensor张量拼接concat - split & stack - unstack

    TensorFlow提供两种类型的拼接: tf.concat(values, axis, name='concat'):按照指定的已经存在的轴进行拼接 tf.stack(values, axis=0, ...

  6. Nginx记录-nginx 负载均衡5种配置方式(转载)

    nginx 负载均衡5种配置方式 1.轮询(默认)   每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除.  2.weight 指定轮询几率,weight和访问比率成 ...

  7. [JVM-1]Java运行时数据区域

    Java虚拟机(JVM)内部定义了程序在运行时需要使用到的内存区域 这些区域都有自己的用途,以及创建和销毁的时间.有些区域随着虚拟机进程的启动而存在,有的区域则依赖用户线程的启动和结束而销毁和建立. ...

  8. System.Web.Optimization 合并压缩技术的使用

    捆绑和压缩原理是:将多个css文件动态合并和压缩为一个css文件.多个js文件动态合并和压缩为一个js文件,如此达到减少浏览器对服务器资源文件的请求数量.缩小资源文件的尺寸来提高页面反应速度的目的.A ...

  9. html中src与href的区别

    概述 src和href之间存在区别,能混淆使用.src用于替换当前元素,href用于在当前文档和引用资源之间确立联系. src src是source的缩写,指向外部资源的位置,指向的内容将会嵌入到文档 ...

  10. 指定so动态链接库连接器

    在学习x86_64汇编时, 发现一旦使用glibc库函数, 如printf时, 一般是需要使用为ld传递命令行参数-lc来动态连接libc.so的, 但是, 生成的可执行文件却无法运行: 气煞我也! ...