后台servlet代码:
File file1 = null,file2=null;
String description1 = null,description2 = null;
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
DiskFileUpload diskFileUpload = new DiskFileUpload();
try{
List<FileItem> list = diskFileUpload.parseRequest(request);
out.println("下面开始遍历所有的FileItem<br/><br/>");
for(FileItem fileItem : list){
if(fileItem.isFormField()){
if("description1".equals(fileItem.getFieldName())){
out.println("遍历到description1 ... 现在description1是空-----批注1---<br/><br/>");
description1 = new String(fileItem.getString().getBytes(),"UTF-8");
}
if("description2".equals(fileItem.getFieldName())){
out.println("遍历到description2 ... 现在description2是空-----批注2---<br/><br/>");
description2 = new String(fileItem.getString().getBytes(),"UTF-8");
}
} else{
if("file1".equals(fileItem.getFieldName())){
File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
out.println("已经检测到用户上传了文件1,并且已经遍历到file1...<br/>");
out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"<br/><br/>");
/*将客户端路径放到session中*/
HttpSession session = request.getSession();
session.setAttribute("clientAddress", remoteFile.getAbsolutePath());
file1 = new File(this.getServletContext().getRealPath("attachment"),remoteFile.getName());
file1.getParentFile().mkdirs();
file1.createNewFile();
InputStream ins = fileItem.getInputStream();
OutputStream ous = new FileOutputStream(file1);
try{
];
;
)
,len);
out.println("已经保存文件到绝对路径——"+file1.getAbsolutePath()+"<br/><br/>");
/*将绝对路径放到session中*/
session.setAttribute("JueDuiAddress", file1.getAbsolutePath());
out.println("<img src="+file1.getAbsolutePath() +" alt='商务小人'/><br/><br/>");
}finally{
ous.close();
ins.close();
}
}
if("file2".equals(fileItem.getFieldName())){
File remoteFile = new File(new String(fileItem.getName().getBytes(),"UTF-8"));
out.println("已经检测到用户上传了文件2,并且已经遍历到file2...<br/><br/>");
out.println("客户端文件位置:"+remoteFile.getAbsolutePath()+"<br/><br/>");
HttpSession session = request.getSession();
session.setAttribute("clientAddress", remoteFile.getAbsolutePath());
file2 = new File(this.getServletContext().getRealPath("attachment"),remoteFile.getName());
file2.getParentFile().mkdirs();
file2.createNewFile();
InputStream ins = fileItem.getInputStream();
OutputStream ous = new FileOutputStream(file2);
try{
];
;
)
,len);
out.println("已经保存文件到绝对路径——"+file2.getAbsolutePath()+"<br/><br/>");
/*将绝对路径放到session中*/
session.setAttribute("JueDuiAddress", file2.getAbsolutePath());
}finally{
ous.close();
ins.close();
}
}
}
out.println("Request 解析完毕<br/><br/>");
}
}
前台jsp代码:
<form action="/login/UploadSer" method="post" enctype="multipart/form-data">
<div align="center">
<fieldset style="width:80%">
<legend>上传文件</legend><br/>
<div align="left">上传文件1</div>
<div align="left">
<input type="file" name="file1"/>
</div>
<div align="left">上传文件2</div>
<div align="left">
<input type="file" name="file2"/>
</div>
<div>
<div align='left'>上传文件说明1</div>
<div align='left'>
<input type="text" name="description1"/>
</div>
</div>
<div>
<div align='left'>上传文件说明2</div>
<div align='left'>
<input type="text" name="description2"/>
</div>
</div>
<div>
<div align='left'>
<input type='submit' value="上传文件"/>
</div>
</div>
</fieldset>
</div>
- Java中的文件上传2(Commons FileUpload:commons-fileupload.jar)
相比上一篇使用Servlet原始去实现的文件上传(http://www.cnblogs.com/EasonJim/p/6554669.html),使用组件去实现相对来说功能更多,省去了很多需要配置和处 ...
- 上传文件出错:org.apache.commons.fileupload.FileUploadBase$IOFileUploadException: Processing of multipart/form-data request failed. Stream ended unexpectedly
最近做一个web项目中有上传文件的功能,已经写出并在本地和部署到服务器上测试了好几个文件上传都没问题(我用的是tomcat).后来又上传了一个700多K的文件(前边的都是不足600K的,并且这个wor ...
- (转)文件上传org.apache.tomcat.util.http.fileupload.FileUploadException: Stream closed
文件上传时,tomcat报错org.springframework.web.multipart.MultipartException: Failed to parse multipart servle ...
- WEB文件上传之apache common upload使用(一)
文件上传一个经常用到的功能,它有许多中实现的方案. 页面表单 + RFC1897规范 + http协议上传 页面控件(flash/html5/activeX/applet) + RFC1897规范 + ...
- atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7
atitit.文件上传带进度条的实现原理and组件选型and最佳实践总结O7 1. 实现原理 1 2. 大的文件上传原理::使用applet 1 3. 新的bp 2 1. 性能提升---分割小文件上传 ...
- Spring MVC的文件上传
1.文件上传 文件上传是项目开发中常用的功能.为了能上传文件,必须将表单的method设置为POST,并将enctype设置为multipart/form-data.只有在这种情况下,浏览器才会把用户 ...
- Spring中MultipartHttpServletRequest实现文件上传
Spring中MultipartHttpServletRequest实现文件上传 转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传 用户必须能 ...
- Spring中MultipartHttpServletRequest实现文件上传 生成缩略图
转贴自:http://my.oschina.net/nyniuch/blog/185266 实现图片上传 用户必须能够上传图片,因此需要文件上传的功能.比较常见的文件上传组件有Commons Fil ...
- Spring中实现文件上传
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt110 实现图片上传 用户必须能够上传图片,因此需要文件上传的功能.比较常见 ...
随机推荐
- 通过FileHandle获取FileObject对象
<div id="wrap"> <!-- google_ad_section_start --> NTSTATUS MyNtReadFile(<br& ...
- [原创]Centos7 从零编译配置Redis
序言 Memcached 是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的速度. Memca ...
- Maven的配置和使用(二)
Maven仓库 使用Maven带给我们一个比较直接的好处是管理jar包,这些jar包默认地址是在C:\Users\用户名.m2 目录下,我们来修改下这个地址: 现在自己想建立的仓库地址的路径下新建文件 ...
- 返水bug-中澳
NOOK(Y) CSBFB(30000) off(N) QQ(2652880032) G(1) off1(Y) QQ1(3479301404) G1(1) off2(N) QQ2(309235846) ...
- Java多线程之构造与发布
资料来源 http://www.ibm.com/developerworks/library/j-jtp0618/ http://www.javaspecialists.eu/archive/Issu ...
- 移动web资源整理
[原]移动web资源整理 2013年初接触移动端,简单做下总结,首先了解下移动web带来的问题 设备更新换代快--低端机遗留下问题.高端机带来新挑战 浏览器厂商不统一--兼容问题多 网络更复杂--弱网 ...
- 【爬虫】Python2 爬虫初学笔记
爬虫,个人理解就是:利用模拟“操作浏览器”的过程,自动获取我们想要的数据(或者说信息,比如图片啊) 为何要学爬虫:爬取数据,为我所用(相当于可以把一类数据整合起来) 一.简单静态网页爬虫架构: 1.B ...
- React Native MAC上环境搭建笔记
今天花了一点时间搭建了一下react native环境,在这个过程中遇到了一些问题,处理并总结一下,年纪大了记性不好,只能多写写...真是岁月不饶人啊! 第一步:安装最新版本的Xcode工具 第二步: ...
- WinForm开发笔记
Button Button默认不产生DoubleClick事件 (MSDN说明:https://msdn.microsoft.com/zh-cn/library/system.windows.form ...
- 【NEUQACM OJ】1018: A+B again
1018: A+B again 题目描述 谷学长有一个非常简单的问题给你,给你两个整数A和B,你的任务是计算A+B. 输入 输入的第一行包含一个整数T(T<=20)表示测试实例的个数,然后2*T ...