文件上传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. (map)What Are You Talking About hdu1075

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  2. Linux 普通用户免密码切换到root用户

    Linux 普通用户免密码切换到root用户 # 添加用户 useradd user_name # 修改密码 echo "user_name@pwd" | passwd --std ...

  3. bzoj1027 状压dp

    https://www.lydsy.com/JudgeOnline/problem.php?id=1072 题意 给一个数字串s和正整数d, 统计s有多少种不同的排列能被d整除 试了一下发现暴力可过 ...

  4. 信用评分卡 (part 4 of 7)

    python信用评分卡(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_camp ...

  5. Java Web之HTML5

    终于学到Java Web这一章节了,首先来了解一下HTML5的一些新知识点吧,我直接贴出HTML5代码看一下: <!DOCTYPE html> <html lang="en ...

  6. CentOS7 下编译 Hadoop

    准备工作 下载 Hadoop 源码 Source (当前最新 2.9.2) https://hadoop.apache.org/releases.html 打开压缩包会看到 BUILDING.txt ...

  7. mssql 数据库表行转列,列转行 比较经典

    --行列互转 /******************************************************************************************** ...

  8. MyBatis SQL语句操作Mysql

    本文记录使用Mybatis操作数据库时碰到的一些语句,供以后参考. 一,多条件查询 示意SQL语句:SELECT t_field1, t_field2 FROM table_name WHERE t_ ...

  9. adb server version (31) doesn't match this client (40); killing...

    删除360的手机助手即可解决,进程名字360MoblieMgr.exe

  10. Win下安装nvm

    nvm 是 windows 下切换 node 版本的管理工具,mac 下可以使用 TJ 写的 n . 1.https://github.com/coreybutler/nvm-windows/rele ...