利用ServletFileUpload组件上传文件
自己的运用:
public void UploadNoteFile(HttpServletRequest request,HttpServletResponse response){
String path = request.getRealPath("/");
String url = path + bean.getAttachPath();
String savepath = new String (url);
final long MAX_SIZE =Long.parseLong(bean.getAttachment())*1024;// 设置上传文件大小
// 允许上传的文件格式的列表
final String[] allowedExt = bean.getAttachType().split(",");
long size = 0; //文件大小
try {
request.setCharacterEncoding("gb2312");
if(ServletFileUpload.isMultipartContent(request)){
ServletFileUpload upload = new ServletFileUpload();
upload.setSizeMax(MAX_SIZE);
FileItemIterator iter = upload.getItemIterator(request);
String subject="",content="";
while (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
InputStream stream = item.openStream();
if (item.isFormField()) {
//帖子标题
if(name.equals("subject")){ //获取subjec字段的值(非file字段)
subject=Streams.asString(stream);
continue;
}
//帖子详细内容
if(name.equals("message")){
content=Streams.asString(stream);
continue;
}
//获取控件值
System.out.println("控件名称" + name + " 值: "
+ Streams.asString(stream));
} else {
//上传附件
if(item.getName()!=null && !"".equals(item.getName())){
File file = new File(item.getName());
String filename = file.getName();
System.out.println("控件名称" + name + " 附件名称"
+ filename);
// 得到去除路径的文件名
String t_name = url.substring(url.lastIndexOf("\\") + 1)+filename;
// 得到文件的扩展名(无扩展名时将得到全名)
String t_ext = t_name.substring(t_name.lastIndexOf(".") + 1);
// 得到文件的大小
size =file.length();
if ("".equals(path) || size == 0) {
System.out.println("请选择上传文件<p />");
return;
}
// 拒绝接受规定文件格式之外的文件类型
int allowFlag = 0;
int allowedExtCount = allowedExt.length;
for (; allowFlag < allowedExtCount; allowFlag++) {
if (allowedExt[allowFlag].equals(t_ext))
break;
}
if (allowFlag == allowedExtCount) {
System.out.println("请上传以下类型的文件<p />");
for (allowFlag = 0; allowFlag < allowedExtCount; allowFlag++)
System.out.println("*." + allowedExt[allowFlag]);
return;
}
long now = System.currentTimeMillis();
// 根据系统时间生成上传后保存的文件名
String prefix = String.valueOf(now);
// 保存的最终文件完整路径
BufferedInputStream bis = new BufferedInputStream(stream);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(savepath + prefix + "." + t_ext)));
Streams.copy(bis, bos, true);
System.out.println("文件上传成功. 已保存为: " + prefix + "." + t_ext
+ " 文件大小: " + size + "字节<p />");
}
}
}
//发表新主题
addTopic(request,response,subject,content);
}
}
catch(Exception e) {// 处理文件尺寸过大异常
e.printStackTrace();
if (e instanceof SizeLimitExceededException) {
System.out.println("文件尺寸超过规定大小:" + MAX_SIZE + "字节<p />");
return;
}
}
}
参考文章:http://qmug.javaeye.com/blog/225244
http://ttitfly.javaeye.com/blog/158526
利用ServletFileUpload组件上传文件相关推荐
- javaweb上传文件_javaWeb中,如何通过CommonsFileUpload组件上传文件
大家好,欢迎来到雄雄的小课堂,今天给大家分享的是<javaWeb中,如何通过Commons-FileUpload组件上传文件> 前言:文件上传大家都不陌生,在这个互联网飞速发展的时代,共享 ...
- asp实现无组件上传文件原理
一.上传网页 上传页面代码: <form action="upload.asp" method="post" enctype=&quo ...
- 使用commons-fileupload组件上传文件(随笔)
1.第三方控件又被称为是第三方组件,使用第三方组件,可以避免大量编码,减少开发工作量及由于逻辑或算法造成的程序异常,从而降低开发成本,提高开发效率.由于第三方组件是第三方组织或者个人提供的,在开发时提 ...
- 在jsp中使用smartupload组件上传文件- -
在jsp中使用smartupload组件上传文件- - jsp对上传文件的支持不象php中支持的那么好,直接做成了函数,也不象asp中要通过组件才能实现.jsp中可以通过javabean来实现.但是我 ...
- element-ui upload组件 上传文件类型限制
element-ui upload组件 上传文件类型限制 <el-uploadclass="c-upload"ref="upload":action=&q ...
- 使用SmartUpload组件上传文件,自己踩过的坑
昨天使用SmartUpload组件上传文件,代码很简单: smartupload_demo01.html <html> <head><title>smartuplo ...
- ant design Upload组件上传文件类型
Upload组件上传文件类型设置 1.设置上传文件类型为excel <template><a-uploadname="file":multiple="t ...
- 利用PUT方式上传文件的方法研究
虽然没有POST方法使用广泛,但是PUT方法却是向服务器上传文件最有效率的方法.POST上传文件时,我们通常需要将所有的信息组合成 multipart 传送过去,然后服务器再解码这些信息,解码过程则必 ...
- elementui 上传七牛_element ui使用上传组件上传文件到七牛(qiniu-js)
博主正在重构博客中,刚开始时静态资源都是上传到本地服务器的,但这个项目博主最后打算真正上线运营的.索性就改进了下,把静态资源尽量放到云存储中,方便后续开发.这里把方法和遇到坑给记录下. 1.使用前提注 ...
最新文章
- python生成固定形状的词云图
- CloudStack管理员文档 - 虚拟机
- 用cookie实现保存密码
- 仿照vue实现简易的MVVM框架(一)
- ug添加imachining变量_UG用的不够快?是不是还没建标准库
- php 与 memcache 笔记
- 网易丁磊:中国的安卓分成全世界最贵 比苹果贵20%左右
- 局部临时表 全局临时表 表变量
- 通过pyenv在Mac OS X 10.12.3下安装Python-3.6.0及“*** [install] Error 1”错误的解决方法
- display:inline-block间隙产生的原因以及解决方案
- window服务器查看硬盘有几块,从多个远程Windows服务器获取磁盘空间信息
- Android NetworkLocationProvider and GeocodeProvider
- 前端多语言开发,如何中英文切换
- 英语口语 每日一句 小红书TroyChina
- 一文讲透计算机网络的数据链路层
- Assertion failed: Protocol wrong type for socket [10041] zeromq 4.3.1\src\ip.cpp:417)错误
- flash air java_Flash Air 打包安卓 ane
- Spark 报错 Failed to delete: C:\Users\lvacz\AppData\Local\Temp\spark-*
- tensorflow中squeeze与expand_dims
- 分享一个stm8s003单片机的ADC转换,附加一个冒泡算法(用于减少误差)