apache fileupload 文件上传,及文件进度设置获取
文件上传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 文件上传,及文件进度设置获取的更多相关文章
- java进行文件上传,带进度条
网上看到别人发过的一个java上传的代码,自己写了个完整的,附带源码 项目环境:jkd7.tomcat7. jar包:commons-fileupload-1.2.1.jar.commons-io-1 ...
- Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现)(转)
Asp.Net实现无刷新文件上传并显示进度条(非服务器控件实现) 相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦 ...
- Retrofit2文件上传下载及其进度显示
序 前面一篇文章介绍了Retrofit2的基本使用,这篇文章接着介绍使用Retrofit2实现文件上传和文件下载,以及上传下载过程中如何实现进度的显示. 文件上传 定义接口 1 2 3 @Multip ...
- 【原创】用JAVA实现大文件上传及显示进度信息
用JAVA实现大文件上传及显示进度信息 ---解析HTTP MultiPart协议 (本文提供全部源码下载,请访问 https://github.com/grayprince/UploadBigFil ...
- SpringMVC文件上传 Excle文件 Poi解析 验证 去重 并批量导入 MYSQL数据库
SpringMVC文件上传 Excle文件 Poi解析并批量导入 MYSQL数据库 /** * 业务需求说明: * 1 批量导入成员 并且 自主创建账号 * 2 校验数据格式 且 重复导入提示 已被 ...
- SpringMVC单文件上传、多文件上传、文件列表显示、文件下载(转)
林炳文Evankaka原创作品.转载请注明出处http://blog.csdn.net/evankaka 本文详细讲解了SpringMVC实例单文件上传.多文件上传.文件列表显示.文件下载. 本文工程 ...
- struts2文件上传,文件类型 allowedTypes
struts2文件上传,文件类型 allowedTypes 1 '.a' : 'application/octet-stream', 2 '.ai' : 'application/postscript ...
- webAPI文件上传时文件过大404错误的问题
背景:最近公司有个需求,外网希望自动保存数据到内网,内网有2台服务器可以相互访问,其中一台服务器外网可以访问,于是想在 这台服务器上放个中转的接口.后来做出来以后测试发现没有问题就放线上去了,不顾发现 ...
- servlet多文件上传(带进度条)
需要commons-fileupload-1.3.jar和commons-io-2.4.jar的支持 页面效果:(图片文件都可以) (1)进度标识类 public class UploadStatus ...
- Asp.Net 无刷新文件上传并显示进度条的实现方法及思路
相信通过Asp.Net的服务器控件上传文件在简单不过了,通过AjaxToolkit控件实现上传进度也不是什么难事,为什么还要自己辛辛苦苦来 实现呢?我并不否认”拿来主义“,只是我个人更喜欢凡是求个所以 ...
随机推荐
- HTML学习笔记Day12
一.CSS3基础 (一)css3概念及优势 概念:CSS3是css技术的升级版本,CSS3语言开发是朝着模块化发展的.以前的规范作为一个模块实在是太庞大而且比较复杂,所以,把它分解为一些小的模块,更多 ...
- scrapy中css选择器初识
由于最近做图片爬取项目,涉及到网页中图片信息的选择,所以边做边学了点皮毛,有自己的心得 百度图库是ajax加载的,所以解析json数据即可 hjsons = json.loads(response.b ...
- python: 多态与虚函数;
通过python的abc模块能够实现虚函数: 首先在开头from abc import ABCMeta, abstractmethod 例子 : #!/usr/bin/python #coding ...
- day13-(事务&mvc&反射补充)
回顾: jsp: java服务器页面 jsp的脚本 jsp的注释 html注释 java注释 jsp注释 <%-- --%> jsp的指令 page:声明页面一些属性 重要的属性: imp ...
- 降维方法PCA与SVD的联系与区别
在遇到维度灾难的时候,作为数据处理者们最先想到的降维方法一定是SVD(奇异值分解)和PCA(主成分分析). 两者的原理在各种算法和机器学习的书籍中都有介绍,两者之间也有着某种千丝万缕的联系.本文在简单 ...
- java socket / No buffer space available
s https://www.cnblogs.com/yiwangzhibujian/p/7107785.html Socket用在哪呢,主要用在进程间,网络间通信. https://www.cnblo ...
- sp_change_users_login 'Update_One', '用户名', '登录名';
每次从服务器上备份好数据库(Sql Server数据库),如果将备份数据库文件在本地恢复,总会产生用户权限的问题. 经过很多次的实验后,我发现有那么一条语句可以发挥作用,就是sp_change_use ...
- Mysql数据库进阶之(分表分库,主从分离)
前言:数据库的优化是一个程序员的分水岭,作为小白我也得去提前学习这方面的数据的 (一) 三范式和逆范式 听起范式这个迟非常专业我来举个简单的栗子: 第一范式就是: 把能够关联的每条数据都拆分成一个 ...
- SpringBoot入门笔记(一)、HelloWorld
本文是一篇SprintBoot学习入门笔记 1.打开Eclipse,版本为Oxygen 4.7.0 2.新建项目NewProject->MavenProject->Next->Nex ...
- 大规模数据导入和导出(oracle)
请期待... http://www.cnblogs.com/xwdreamer/archive/2012/06/08/2541678.html Oracle sqlldr的用法 (这个最完整) htt ...