struts 2 框架为处理文件上传提供了内置支持,它使用“在 html 中基于表单的文件上传”。当上传一个文件时,它通常会被存储在一个临时目录中,而且它们应该由 action 类进行处理或移动到一个永久的目录,用来确保数据不丢失。服务器在恰当的位置可能有一个安全策略,它会禁止你写到除了临时目录以外的目录,而且这个目录属于你的web应用应用程序。
通过预定义的名为文件上传的拦截器,struts 的文件上传是可能的,这个拦截器在 org.apache.struts2.interceptor.fileuploadinterceptor 类是可用的,而且是 defaultstack 的一部分。
创建视图文件
让我们开始创建需要浏览和上传选定的文件的视图。因此,让我们创建一个带有简单的 html 上传表单的 index.jsp,它允许用户上传文件:(表单的编码类型设置为multipart/form-data)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<%--
created by intellij idea.
user: yzjxiaoyue
date: 2017 / 7 / 28
time: 19 : 11
to change this template use file | settings | file templates.
--%>
<%@ page language= "java" contenttype= "text/html; charset=utf-8"
pageencoding= "utf-8" %>
<%@ taglib prefix= "s" uri= "/struts-tags" %>
<!doctype html public "-//w3c//dtd html 4.01 transitional//en"
"http://www.w3.org/tr/html4/loose.dtd" >
<html>
<head>
<title>file upload</title>
</head>
<body>
<form action= "upload" method= "post" enctype= "multipart/form-data" >
<label for = "myfile" >upload your file</label>
<input type= "file" name= "myfile" id= "myfile" />
<input type= "submit" value= "upload" />
</form>
</body>
</html>
|
之后创建success.jsp页面:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%--
created by intellij idea.
user: yzjxiaoyue
date: 2017 / 7 / 28
time: 19 : 14
to change this template use file | settings | file templates.
--%>
<%@ page contenttype= "text/html; charset=utf-8" %>
<%@ taglib prefix= "s" uri= "/struts-tags" %>
<html>
<head>
<title>file upload success</title>
</head>
<body>
you have successfully uploaded <s:property value= "myfilefilename" />
</body>
</html>
|
创建error.jsp页面
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<%--
created by intellij idea.
user: yzjxiaoyue
date: 2017 / 7 / 28
time: 20 : 05
to change this template use file | settings | file templates.
--%>
<%@ page contenttype= "text/html; charset=utf-8" %>
<%@ taglib prefix= "s" uri= "/struts-tags" %>
<html>
<head>
<title>file upload error</title>
</head>
<body>
there has been an error in uploading the file.
</body>
</html>
|
创建 action 类
接下来让我们创建一个称为 uploadfile.java 的 java 类,它负责上传文件,并且把这个文件存储在一个安全的位置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
package com.action;
import com.opensymphony.xwork2.actionsupport;
import org.apache.commons.io.fileutils;
import java.io.file;
import java.io.ioexception;
public class uploadfile extends actionsupport{
private file myfile;
public file getmyfile() {
return myfile;
}
public void setmyfile(file myfile) {
this .myfile = myfile;
}
private string myfilecontenttype;
private string myfilefilename;
private string destpath;
public string execute()
{
/* copy file to a safe location */
destpath = "e:\\program files\\apache-tomcat-9.0.0\\apache-tomcat-9.0.0.m22\\work\\" ;
try {
system.out.println( "src file name: " + myfile);
system.out.println( "dst file name: " + myfilefilename);
file destfile = new file(destpath, myfilefilename);
fileutils.copyfile(myfile, destfile);
} catch (ioexception e){
e.printstacktrace();
return error;
}
return success;
}
public string getmyfilecontenttype() {
return myfilecontenttype;
}
public void setmyfilecontenttype(string myfilecontenttype) {
this .myfilecontenttype = myfilecontenttype;
}
public string getmyfilefilename() {
return myfilefilename;
}
public void setmyfilefilename(string myfilefilename) {
this .myfilefilename = myfilefilename;
}
}
|
配置文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<?xml version= "1.0" encoding= "utf-8" ?>
<!doctype struts public
"-//apache software foundation//dtd struts configuration 2.3//en"
"http://struts.apache.org/dtds/struts-2.3.dtd" >
<struts>
<constant name= "struts.devmode" value= "true" />
<constant name= "struts.multipart.maxsize" value= "10000000" />
<constant name= "struts.multipart.savedir" value= "/tmp" />
<constant name= "struts.custom.i18n.resources" value= "struts" ></constant>
< package name= "default" namespace= "/" extends = "struts-default" >
<action name= "upload" class = "com.action.uploadfile" >
<!--<interceptor-ref name= "basicstack" />-->
<interceptor-ref name= "defaultstack" />
<interceptor-ref name= "fileupload" >
<param name= "allowedtypes" >image/jpeg,image/jpg,image/gif</param>
</interceptor-ref>
<result name= "success" >/success.jsp</result>
<result name= "error" >/error.jsp</result>
</action>
</ package >
</struts>
|
界面截图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/yzj_xiaoyue/article/details/76284279