文件上传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. pycharm更新之后pip显示没有main

    更新pip之后,Pycharm安装package出现报错:module 'pip' has no attribute 'main' 找到安装目录下 helpers/packaging_tool.py文 ...

  2. Linux记录-安装LAMP和R环境

    2.2 Apache httpd2.2.1 执行命令进行安装:yum install -y httpd2.2.2 开启服务:service httpd start2.2.3 设置开机自启动:chkco ...

  3. 阿里云部署Web项目

    1.首先最基本的购买服务器和域名(学生党可以享受每月9块钱的优惠,不知道为什么,pc端不能购买,只能下载阿里云APP购买)  下载APP后打开:学生专区-学生特权-购买(我选择的是ubuntu,这个随 ...

  4. TIMESTAMP使用遇到得麻烦

    mysql按日期查询报空,怎么查看日志发现是14:36:01.709(Timestamp), 参数出了问题 2018-04-17 14:36:16,887 [http-nio-8080-exec-5] ...

  5. Centos7安装官方JDK

    一.下载jdk最新版本版本 链接地址:官方地址 二.上传jdk到centos下 三.检查当前linux系统上是否有jdk,linux命令:rpm -qa | grep java 查询结果: 卸载掉系统 ...

  6. python之所以强大很大一部分原因在于他众多的取之不尽的库

    GUI 的 自动任务用这个pyautogui库,web 页面的用 selenium + webdriver 同类型的还有 sikuli ,低配版 按键精灵 本教程译自大神Al Sweigart的PyA ...

  7. 在 CentOS6 上安装 Zabbix2.4 Server

    #!/bin/bash # # .配置无人值守的安装,定义安装过程中需要用到的一些信息 # mysql_root_pw=root_pw mysql_zabbix_pw=zabbix_pw DBPass ...

  8. 魔改版BBR

    魔改版bbr加速: wget -N --no-check-certificate "https://raw.githubusercontent.com/chiakge/Linux-NetSp ...

  9. vue插件 使用use注册Vue全局组件和全局指令

    插件一般会注册到全局使用 官方编辑插件介绍:https://vuefe.cn/v2/guide/plugins.html 全局组件: .首先建一个自定义组件的文件夹,比如叫loading,里面有一个i ...

  10. JS 比较两个数组是否相等 是否拥有相同元素

    Javascript怎么比较两个数组是否相同?JS怎么比较两个数组是否有完全相同的元素?Javascript不能直接用==或者===来判断两个数组是否相等,无论是相等还是全等都不行,以下两行JS代码都 ...