Commons FileUpLoad 两种上传方式解
traditional API (传统方式)
//上传路径
File file = new File("C:/upload");
//临时文件路径
File tempFile = new File("C:/uploadtemp"); if(!file.exists()) {
file.mkdir();
} if(!tempFile.exists()) {
tempFile.mkdir();
}
//判断 request 是一个文件上传的 request
//通过 ServletFileUpload 类的静态方法 isMultipartContent 来判断
if(ServletFileUpload.isMultipartContent(request)) {
//用 DiskFileItemFactory 创建新的 file items (只是临时的)
//DiskFileItemFactory 创建FileItem 实例,并保存其内容在<b>内存</b>或者<b>硬盘中</b>
//通过一个阀值来决定这个 FileItem 实例是存放在<b>内存</b>或者<b>硬盘中</b>
DiskFileItemFactory factory = new DiskFileItemFactory();
//设置阀值大小
//不设置的话,默认10k
//超过这个阀值,FileItem将直接写入到磁盘
factory.setSizeThreshold(1024*10);
//设置临时文件夹
//不设置,默认为系统默认Temp文件路径,调用 System.getProperty("java.io.tmpdir") 获取
//超过阀值的 FileItem 实例将存放在这个目录中
factory.setRepository(tempFile);
//构造servletFileUpload 的实例,该实例提供工厂模式创建FileItem的DiskFileItemFactory实例
ServletFileUpload fileUpload = new ServletFileUpload(factory);
//设置一个<b>完整的请求</b>允许的最大大小(注意是完整请求,包括非file类型的表单,比如Text类型)
fileUpload.setSizeMax(10*1024*1024);
//设置所允许的最大的单个上传文件大小(对应一个FileItem对象)
//fileUpload.setFileSizeMax(10*1024*1024); try {
//每一个FileItem 对应一个 request 请求中from表单中的 input 元素
//解析 request 请求,将request中提交的值存入List数组
List<FileItem> items = fileUpload.parseRequest(request); for(FileItem item : items) {
//是不是一个文件上传组件
if(!item.isFormField()) {
String name = item.getName();
System.out.println("name:" + name);
System.out.println("length:" + item.getSize());
item.write(new File("C:/upload/"+name));
} else {
String desc = item.getString("UTF-8");
System.out.println("文件描述:" + desc);
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
throw new RuntimeException("请设置form表单的enctype属性");
}
Streaming API
//设置文件上传路径
String UploadFilePath = "d:/upload";
//限制文件大小
//通过 request.getHeader("Content-Length") 获取request 请求内容长度来限制
if(ServletFileUpload.isMultipartContent(request)) { //创建ServletFileUpload实例
ServletFileUpload upload = new ServletFileUpload();
try {
//解析request 请求 并返回FileItemStream 的iterator 实例
FileItemIterator iter = upload.getItemIterator(req);
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
System.out.println("Form field " + name + " with value "
+ Streams.asString(stream) + " detected.");
} else {
System.out.println("File field " + name + " with file name "
+ item.getName() + " detected.");
// Process the input stream
//System.out.println(Streams.asString(stream));
System.out.println("shuru:");
String filename = new Scanner(System.in).next();
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(new File("e:/upload"),"ez.zip")));
BufferedInputStream bis = new BufferedInputStream(stream);
byte[] buffer = new byte[1024];
int len = -1;
while (-1 != (len = bis.read(buffer))){
bos.write(buffer,0,len);
}
bis.close();
bos.flush();
bos.close(); PrintWriter out = resp.getWriter();
out.write("完成");
System.out.println("wanchengle ");
}
}
} catch (FileUploadException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} else {
throw new RuntimeException("请设置form表单的enctype属性");
}
Commons FileUpLoad 两种上传方式解的更多相关文章
- GitHub两种上传方式的对比----SSH / https
https://www.jianshu.com/p/1ac06bcd8ab5 https://www.cnblogs.com/lqfxyy/p/5740720.html https://blog.cs ...
- git commit -a -m "DM 1、获取aliOssSTS值,计算签名,实现视频PUT/POST2种上传方式上传;"
git commit -a -m "DM 1.获取aliOssSTS值,计算签名,实现视频PUT/POST2种上传方式上传:" 微信小程序的视频上传
- Selenium系列(十一) - 针对两种上传文件方式的实现方案
如果你还想从头学起Selenium,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1680176.html 其次,如果你不懂前端基础知识, ...
- web 中常用的两种上传文件的方法总结
这里我们来总结整理一下常用的两种文件上传方式以及要注意的东西: 1.springmvc .MultipartFile 的上传方式. 2.org.apache.commons.fileupload 使用 ...
- upload三种上传方式(上)---Servlet---post---commons-fileupload.1.2.1.jar方式请求上传文件
上传前进行的配置选项: 1.在下方的Servers中,右键你的tomcat--open,选中下面两个配置. 第一个:Serve modules without publishing 作用:tomcat ...
- Web的几种上传方式总结
问题 文件上传在WEB开发中应用很广泛. 文件上传是指将本地图片.视频.音频等文件上传到服务器上,可以供其他用户浏览或下载的过程. 以下总结了常见的文件(图片)上传的方式和要点处理. 表单上传 这是传 ...
- hadoop中两种上传文件方式
记录如何将本地文件上传至HDFS中 前提是已经启动了hadoop成功(nodedate都成功启动) ①先切换到HDFS用户 ②创建一个user件夹 bin/hdfs dfs -mkdir /user ...
- Apache Commons FileUpload实现文件上传
一.Apache Commons-FileUpload简介 Apache Commons是一个专注于可重用Java组件的所有方面的 Apache 项目. Apache Commons项目由三个部分组成 ...
- Apache Commons fileUpload实现文件上传之一
需要两个jar包: commons-fileupload.jar Commons IO的jar包(本文使用commons-io-2.4.jar) 利用Servlet来实现文件上传. package ...
随机推荐
- 斐波那契数列(python实现)
描述 一个斐波那契序列,F(0) = 0, F(1) = 1, F(n) = F(n-1) + F(n-2) (n>=2),根据n的值,计算斐波那契数F(n),其中0≤n≤1000. 输入 输入 ...
- bzoj 3932: [CQOI2015]任务查询系统 -- 主席树 / 暴力
3932: [CQOI2015]任务查询系统 Time Limit: 20 Sec Memory Limit: 512 MB Description 最近实验室正在为其管理的超级计算机编制一套任务管 ...
- SPOJ 10628. SPOJ COT Count on a tree 可持久化线段树
这题是裸的主席树,每个节点建一棵主席树,再加个lca就可以了. 历尽艰辛,终于A掉了这一题,这般艰辛也显示出了打代码的不熟练. 错误:1.lca倍增的时候i和j写反了,RE了5次,实在要吸取教训 2. ...
- HDU 5651 xiaoxin juju needs help 数学
xiaoxin juju needs help 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5651 Description As we all k ...
- CentOS 6.9/7通过yum安装指定版本的Redis
一.安装 // 安装依赖 # wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm && ...
- LT1072 -- Wide-range voltage regulator automatically selects operating mode
The circuit in Figure 1 delivers programming voltages to an EEPROM under the control of an external ...
- 博雅PHP高级工程师面试题-自拟
作者:元如枫 2010年 1.现有学校课程内容系统简单需求描述,试着提供解决方案. 需求简单描述如下: 1)对象及属性 学校: 学校名称,学校所属分类,学校介绍,学校地图标记,学校所属地区,标签, ...
- 设计原则:消除Switch...Case的过程,可能有点过度设计了。
备注 不要重复自己,也不要重复别人,一旦养成了“拷贝和粘贴”的习惯,写程序的时候非常容易导致重复,好在一直暗示自己要稍后进行重构,本文给出一个重构的示例. 需求 需求:按照年.月和日显示销售数据,根据 ...
- Netty Channel 接口名词理解
1.Channel channel 是负责数据读,写的对象,有点类似于老的io里面的stream.它和stream的区别,channel是双向的,既可以write 也可以read,而stream要分o ...
- Message Box Position
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...