1. struts2中的文件上传
第一步:在WEB=INF/lib下加入commons-fileupload-1.2.1.jar , commons-io-1.3.2.jar。
第二步:把form表单的enctype属性设置为"multipart/form-data",如
- <form action="${pageContext.request.contextPath}/control/employee/list_execute.action" enctype="multipart/form-data" method="post">
- 文件:<input type="file" name="image">
- <input type="submit" value="上传"/>
- </form>
-
第三步:在action中添加一下属性,
- public class HelloWorldAction {
- private File image;
- private String imageFileName;
- private String imageContentType ;
-
- public String getImageContentType() {
- return imageContentType;
- }
- public void setImageContentType(String imageContentType) {
- this.imageContentType = imageContentType;
- }
- public String getImageFileName() {
- return imageFileName;
- }
- public void setImageFileName(String imageFileName) {
- this.imageFileName = imageFileName;
- }
- public File getImage() {
- return image;
- }
- public void setImage(File image) {
- this.image = image;
- }
- public String execute() throws Exception{
- System.out.println("imageFileName = "+imageFileName);
- System.out.println("imageContentType = "+imageContentType);
-
- String realpath = ServletActionContext.getServletContext().getRealPath("/images");
- System.out.println(realpath);
- if(image!=null){
- File savefile = new File(new File(realpath), imageFileName);
- if(!savefile.getParentFile().exists()) savefile.getParentFile().mkdirs();
- FileUtils.copyFile(image, savefile);
- ActionContext.getContext().put("message", "上传成功");
- }else{
- ActionContext.getContext().put("message", "上传失败");
- }
- return "success";
- }
- }
此外,可以在struts.xml中配置上传文件的大小
<constant name="struts.multipart.maxSize" value="10701096"/> //最大上传配置成10M
默认的上传大小为2M
思维拓展:如果要上传的文件非常大,如上传的是电影,好几百M ,用web上传一般是不可能难上传成功的,这时候要安装一个插件,类似于应用程序
socket ,通过网络通讯上传。
2 . 多文件上传
在上面的基础上略加改动
1.jsp表单
- <form action="${pageContext.request.contextPath}/control/employee/list_execute.action" enctype="multipart/form-data" method="post">
- 文件1:<input type="file" name="image"><br/>
- 文件2:<input type="file" name="image"><br/>
- 文件3:<input type="file" name="image"><br/>
- <input type="submit" value="上传"/>
- </form>
2. action中用数组接收
- public class HelloWorldAction {
- private File[] image;
- private String[] imageFileName;
- private String[] imageContentType ;
-
- public String execute() throws Exception{
-
- String realpath = ServletActionContext.getServletContext().getRealPath("/images");
- System.out.println(realpath);
- if(image!=null){
- File savedir = new File(realpath);
- if(!savedir.exists())
- {
- savedir.mkdirs();
- }
- System.out.println("image.length = "+image.length);
- for(int i = 0 ; i<image.length ; i++){
-
- System.out.println("imageContentType["+i+"] = "+imageContentType[i]);
- File savefile = new File(savedir, imageFileName[i]);
- FileUtils.copyFile(image[i], savefile);
- }
- ActionContext.getContext().put("message", "上传成功");
- }
- return "success";
- }
- }
分享到:
相关推荐
在struts2基础上实现的包括单文件、多文件的上传及下载
Struts2多个文件上传 多个文件上传分为List集合和数组,下面我们着重介绍一下list集合的上传。都大同小异。
Struts2上传文件(直接用request)
Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码 Struts2文件上传源码
struts2上传单个文件及多个文件,strus2拦截器的使用,里面有个user用户,先执行user.jsp即登陆,可以执行上传文件的操作,反之,不能,有个exit.jsp,退出之后不能执行。里面含有jar文件,导入myeclipse可以执行...
struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传struts2文件上传
struts2上传文件进度条显示,里面有详细的代码
1.能够对多个文件进行上传(可以选择上传文件个数,也即上传文件个数不定) 2.能够对上传路径进行配置文件指定(upload.properties),使用了一些类似单例模式的静态代码块 3.Struts2进行下载处理,能对上传的所有...
struts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileuploadstruts2 文件上传 struts2上传标签file fileupload
struts 单文件上传和多文件上传带进度条
struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传struts2实现的多个文件上传
JavaEE Struts文件上传
struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载 struts2文件上传和下载
使用struts2框架的一个文件上传demo。。大家互相学习学习,里面有注释了,觉得好的顶一下吧。
Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip Struts2文件批量上传.zip
struts2 单文件,多文件上传及异常处理
Struts2 上传文件 过滤文件类型 过滤大小
一个swfupload+struts2实现多文件上传的案例。
struts2文件上传的两种方法,单文件上传与多文件上传,很经典的,是我总结的两种方法,要看struts2文件上传,大家一定要下载看看啊