博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Struts2文件的上传
阅读量:6076 次
发布时间:2019-06-20

本文共 8491 字,大约阅读时间需要 28 分钟。

一、单文件的上传 

表单的enctype属性:

            application/x-www-form-urlencoded:默认的编码方式。

            multipart/form-data:会以二进制的方式处理表单数据,会将文件域指定文件的内容也封装到请求参数里。

            text/plain:当action属性是的形式方便。

     常用的上传框架是Common-FileUpload和COS,默认是Common-FileUpload,一旦框架获得了文件域对应的文件内容,我们就可以通过IO流将文件内容写入服务器的任意位置,从而完成文件上传。

     可以通过struts.properties配置文件中,修改默认值:struts.multipart.parser=jakarta

1、处理上传文件的jsp页面

   upload.jsp

1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:     pageEncoding="UTF-8"%>
3: <%@ page import="java.util.*" %>
4: <%@ taglib prefix="s" uri="/struts-tags" %>
5: 
6: 
7: <%
8: String path = request.getContextPath();
9: String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
10: %>
11: 
12:     
13:     
14:     Struts2文件上传
15: 
16: 
17:       

Struts 2完成上传

18:       
19:         
20:         
21:             
22:                 用户名:
23:                 
24:             
25:             
26:                 上传文件:
27:                 
28:             
29:             
30:                 
31:                 
32:             
33:         
34:       
35:   
36: 
37: 

2、UploadAction.java

1: package cc.openhome;
2: import java.io.File;
3: import java.io.FileInputStream;
4: import java.io.FileOutputStream;
5: import java.io.InputStream;
6: import java.io.OutputStream;
7: import org.apache.struts2.ServletActionContext;
8: import com.opensymphony.xwork2.ActionSupport;
9: public class UploadAction extends ActionSupport {
10:   // username属性用来封装用户名
11:   private String username;
12:
13:   // myFile属性用来封装上传的文件
14:   private File myFile;
15:
16:   // myFileContentType属性用来封装上传文件的类型
17:   private String myFileContentType;
18:
19:   // myFileFileName属性用来封装上传文件的文件名
20:   private String myFileFileName;
21:
22:
23:   //获得username值
24:   public String getUsername() {
25:     return username;
26:   }
27:
28:   //设置username值
29:   public void setUsername(String username) {
30:     this.username = username;
31:   }
32:
33:   //获得myFile值
34:   public File getMyFile() {
35:     return myFile;
36:   }
37:
38:   //设置myFile值
39:   public void setMyFile(File myFile) {
40:     this.myFile = myFile;
41:   }
42:
43:   //获得myFileContentType值
44:   public String getMyFileContentType() {
45:     return myFileContentType;
46:   }
47:
48:   //设置myFileContentType值
49:   public void setMyFileContentType(String myFileContentType) {
50:     this.myFileContentType = myFileContentType;
51:   }
52:
53:   //获得myFileFileName值
54:   public String getMyFileFileName() {
55:     return myFileFileName;
56:   }
57:
58:   //设置myFileFileName值
59:   public void setMyFileFileName(String myFileFileName) {
60:     this.myFileFileName = myFileFileName;
61:   }
62:
63:   public String execute() throws Exception {
64:
65:     //基于myFile创建一个文件输入流
66:     InputStream is = new FileInputStream(myFile);
67:
68:     // 设置上传文件目录
69:     String uploadPath = ServletActionContext.getServletContext()
70:         .getRealPath("/upload");
71:
72:     // 设置目标文件
73:     File toFile = new File(uploadPath, this.getMyFileFileName());
74:
75:     // 创建一个输出流
76:     OutputStream os = new FileOutputStream(toFile);
77:
78:     //设置缓存
79:     byte[] buffer = new byte[1024*1024];
80:
81:     int length = 0;
82:
83:     //读取myFile文件输出到toFile文件中
84:     while ((length = is.read(buffer)) > 0) {
85:       os.write(buffer, 0, length);
86:     }
87:     //关闭输入流
88:     is.close();
89:     //关闭输出流
90:     os.close();
91:     return SUCCESS;
92:   }
93: }
94:
95:

    3、配置struts.xml

1: 
2: 
3:   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4:   "http://struts.apache.org/dtds/struts-2.3.dtd">
5:
6: 
7:   
8:   
9:     
10:       
11:         image/bmp,image/png,image/gif,image/jpeg
12:         2000
13:       
14:       
15:       
/WEB-INF/page/succ.jsp
16:       
/upload.jsp
17:     
18:   
19:     
20: 
21:

    4、succ.jsp

1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:     pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags" %>
4: 
5: 
6: 
7:     
8:     Insert title here
9: 
10: 
11:     上传成功!
12:     上传用户:${username}
13:     文件标题:${ myFileFileName }
14:     文件类型:${ myFileContentType }
15:     文件为:
"/>
16: 
17: 

     5、运行结果

          此时,上传文件必须满足:大小小于2000kb,而且是图片文件。

     

二、多文件的上传

    1、uploadmany.jsp

1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:     pageEncoding="UTF-8"%>
3: 
4: 
5: 
6:   
7:   数组多文件上传
8: 
9: 
10:   
11:     上传用户:
12:     选择第一个文件:
13:     选择第二个文件:
14:     选择第三个文件:
15:     
16:         
17:   
18: 
19: 

    2、UploadAction.jsp

1: package cc.openhome;
2:
3: import java.io.File;
4: import java.io.FileInputStream;
5: import java.io.FileOutputStream;
6: import java.io.InputStream;
7: import java.io.OutputStream;
8: import java.util.List;
9: import org.apache.struts2.ServletActionContext;
10: import com.opensymphony.xwork2.ActionSupport;
11: public class UploadAction extends ActionSupport {
12:   // username属性用来封装用户名
13:   private String username;
14:
15:   // myFile属性用来封装上传的文件
16:   //private File[] myFile;
17:   private List
myFile;
18:
19:   // myFileContentType属性用来封装上传文件的类型
20:   private List
myFileContentType;
21:
22:   // myFileFileName属性用来封装上传文件的文件名
23:   private List
myFileFileName;
24:
25:   //定义所有Action允许上传的类型
26:   private String allowTypes;
27:
28:   public String getAllowTypes() {
29:     return allowTypes;
30:   }
31:
32:   public void setAllowTypes(String allowTypes) {
33:     this.allowTypes = allowTypes;
34:   }
35:
36:   //获得username值
37:   public String getUsername() {
38:     return username;
39:   }
40:
41:   //设置username值
42:   public void setUsername(String username) {
43:     this.username = username;
44:   }
45:
46:   //获得myFile值
47:   public List
getMyFile() {
48:     return myFile;
49:   }
50:
51:   //设置myFile值
52:   public void setMyFile(List
myFile) {
53:     this.myFile = myFile;
54:   }
55:
56:   //获得myFileContentType值
57:   public List
getMyFileContentType() {
58:     return myFileContentType;
59:   }
60:
61:   //设置myFileContentType值
62:   public void setMyFileContentType(List
myFileContentType) {
63:     this.myFileContentType = myFileContentType;
64:   }
65:
66:   //获得myFileFileName值
67:   public List
getMyFileFileName() {
68:     return myFileFileName;
69:   }
70:
71:   //设置myFileFileName值
72:   public void setMyFileFileName(List
myFileFileName) {
73:     this.myFileFileName = myFileFileName;
74:   }
75:   public String execute() throws Exception {
76:     List
files=getMyFile();
77:     for(int i=0;i
78:       //基于myFile创建一个文件输入流
79:       InputStream is = new FileInputStream(files.get(i));
80:
81:       // 设置上传文件目录
82:       String uploadPath = ServletActionContext.getServletContext()
83:           .getRealPath("/upload");
84:       // 设置目标文件
85:       //File[] toFile = new File(uploadPath, this.getMyFileFileName());
86:       File toFile = new File(uploadPath, this.getMyFileFileName().get(i));
87:       // 创建一个输出流
88:       OutputStream os = new FileOutputStream(toFile);
89:
90:       //设置缓存
91:       byte[] buffer = new byte[1024*1024];
92:
93:       int length = 0;
94:
95:       //读取myFile文件输出到toFile文件中
96:       while ((length = is.read(buffer)) > 0) {
97:         os.write(buffer, 0, length);
98:       }
99:       //关闭输入流
100:       is.close();
101:
102:       //关闭输出流
103:       os.close();
104:     }
105:     return SUCCESS;
106:   }
107: }
108:
109:
110:

    3、struts.xml

1: 
2: 
3:   "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
4:   "http://struts.apache.org/dtds/struts-2.3.dtd">
5:
6: 
7:   
8:       
9:           
10:             
11:         image/bmp,image/png,image/gif,image/jpeg
12:         20000000
13:       
14:       
15:             
/succ.jsp
16:             
/upload.jsp
17:         
18:   
19: 

    4、succ.jsp

1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2:     pageEncoding="UTF-8"%>
3: <%@ taglib prefix="s" uri="/struts-tags" %>
4: 
5: 
6:   
7:     
8:     图片显示
9:   
10:   
11:     

上传成功!

12:     上传用户:${ username }

13:     第一个文件名:${ myFileFileName[0] }
14:     第一个文件类型:${ myFileContentType[0] }
15:     第一个文件为:
"/>

16:     第二个文件名:${ myFileFileName[1] }
17:     第二个文件类型:${ myFileContentType[1] }
18:     第二个文件为:
"/>

19:     第三个文件名:${ myFileFileName[2] }
20:     第三个文件类型:${ myFileContentType[2] }
21:     第三个文件为:
"/>

22:   
23: 

    5、运行结果

                                   

当神已无能为力,那便是魔渡众生

转载地址:http://nhigx.baihongyu.com/

你可能感兴趣的文章
RHEL6.3配置文件共享(2) autofs服务
查看>>
第 10 章 容器监控 - 083 - Prometheus 架构
查看>>
Linux RedHat 6.4 MySQL5.6源码包安装
查看>>
需要auth验证的post请求(python)
查看>>
Java IO2:RandomAccessFile
查看>>
Linux下软件的安装
查看>>
面向对象:继承
查看>>
Docker私有仓库--Harbor搭建
查看>>
我的友情链接
查看>>
js中的闭包简单总结
查看>>
恳求分页治理方法完成虚拟内存
查看>>
学习笔记--mysql5.7新版本
查看>>
global变量:$GLOBALS
查看>>
samba
查看>>
osp选路
查看>>
MyEclipse 快捷键1(CTRL)
查看>>
EVE模拟器如何设置预配
查看>>
分布式架构真正适用于大型互联网项目的架构! dubbo+zookeeper+springmvc+mybatis
查看>>
金融互联网之网络征信技术接口
查看>>
window mysql5.5错误
查看>>