- 浏览: 69517 次
文章分类
最新评论
struts2文件的上传和下载
(一)从底层透析文件上传的实现,此时并没有介入struts2
1、upload.jsp,在form中属性method默认为get,涉及文件上传时必须改为post,默认enctype="application/x-www-form-urlencoded"
,我们暂且不修改,看会有什么结果
1<%@pagelanguage="java"contentType="text/html;charset=GBK"
2pageEncoding="GBK"%>
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
7<title>Inserttitlehere</title>
8</head>
9<body>
10<formaction="result.jsp"method="post"
11enctype="application/x-www-form-urlencoded">
12Information:
13<inputtype="text"name="info">
14<br>
15File:
16<inputtype="file"name="file">
17<br>
18<inputtype="submit"name="submit"value="submit">
19</form>
20</body>
21</html>
2pageEncoding="GBK"%>
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
7<title>Inserttitlehere</title>
8</head>
9<body>
10<formaction="result.jsp"method="post"
11enctype="application/x-www-form-urlencoded">
12Information:
13<inputtype="text"name="info">
14<br>
15File:
16<inputtype="file"name="file">
17<br>
18<inputtype="submit"name="submit"value="submit">
19</form>
20</body>
21</html>
result.jsp
1<%@pagelanguage="java"contentType="text/html;charset=GBK"
2pageEncoding="GBK"%>
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
7<title>Inserttitlehere</title>
8</head>
9<body>
10Information:<%=request.getParameter("info")%><br>
11File:<%=request.getParameter("file")%><br>
12</body>
13</html>
2pageEncoding="GBK"%>
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
7<title>Inserttitlehere</title>
8</head>
9<body>
10Information:<%=request.getParameter("info")%><br>
11File:<%=request.getParameter("file")%><br>
12</body>
13</html>
结果:
2、修改result.jsp页面代码,输出读入的流
<%@pageimport="java.io.*"%>
<body>
<%
InputStreamis=request.getInputStream();
BufferedReaderbr=newBufferedReader(newInputStreamReader(is));
Stringbuffer=null;
while((buffer=br.readLine())!=null){
out.print(buffer+"<br>");
}
%>
</body>
<%
InputStreamis=request.getInputStream();
BufferedReaderbr=newBufferedReader(newInputStreamReader(is));
Stringbuffer=null;
while((buffer=br.readLine())!=null){
out.print(buffer+"<br>");
}
%>
</body>
结果:
这个结果可以断定,文件的上传并没有成功,而仅仅是上传了文件的路径信息而已
3、把upload.jsp中form的enctype属性改为enctype="multipart/form-data"
<formaction="result.jsp"method="post"enctype="multipart/form-data">
Information:
<inputtype="text"name="info">
<br>
File:
<inputtype="file"name="file">
<br>
<inputtype="submit"name="submit"value="submit">
</form>
Information:
<inputtype="text"name="info">
<br>
File:
<inputtype="file"name="file">
<br>
<inputtype="submit"name="submit"value="submit">
</form>
结果:
说明文件上传是成功的。
(二)手动采用fileupload组建进行文件上传
upload2.jsp
1<%@pagelanguage="java"contentType="text/html;charset=GB18030"
2pageEncoding="GB18030"%>
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
7<title>Inserttitlehere</title>
8</head>
9<body>
10<formaction="/MyStruts2/UploadServlet"method="post"enctype="multipart/form-data">
11username:
12<inputtype="text"name="username">
13<br>
14password:
15<inputtype="password"name="password">
16<br>
17file1:
18<inputtype="file"name="file1">
19<br>
20file2:
21<inputtype="file"name="file2">
22<br>
23<inputtype="submit"value="submit">
24</form>
25</body>
26</html>
2pageEncoding="GB18030"%>
3<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
4<html>
5<head>
6<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
7<title>Inserttitlehere</title>
8</head>
9<body>
10<formaction="/MyStruts2/UploadServlet"method="post"enctype="multipart/form-data">
11username:
12<inputtype="text"name="username">
13<br>
14password:
15<inputtype="password"name="password">
16<br>
17file1:
18<inputtype="file"name="file1">
19<br>
20file2:
21<inputtype="file"name="file2">
22<br>
23<inputtype="submit"value="submit">
24</form>
25</body>
26</html>
web.xml中的配置
<servlet>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.test.servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<servlet-class>com.test.servlet.UploadServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadServlet</servlet-name>
<url-pattern>/UploadServlet</url-pattern>
</servlet-mapping>
UploadServle.java
1packagecom.test.servlet;
2
3importjava.io.File;
4importjava.io.FileOutputStream;
5importjava.io.IOException;
6importjava.io.InputStream;
7importjava.io.OutputStream;
8importjava.util.List;
9
10importjavax.servlet.ServletException;
11importjavax.servlet.http.HttpServlet;
12importjavax.servlet.http.HttpServletRequest;
13importjavax.servlet.http.HttpServletResponse;
14
15importorg.apache.commons.fileupload.FileItem;
16importorg.apache.commons.fileupload.FileUploadException;
17importorg.apache.commons.fileupload.disk.DiskFileItemFactory;
18importorg.apache.commons.fileupload.servlet.ServletFileUpload;
19
20@SuppressWarnings("serial")
21publicclassUploadServletextendsHttpServlet{
22@SuppressWarnings({"unchecked","deprecation"})
23publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
24throwsServletException,IOException{
25
26DiskFileItemFactoryfactory=newDiskFileItemFactory();
27
28Stringpath=request.getRealPath("/upload");
29
30factory.setRepository(newFile(path));
31
32factory.setSizeThreshold(1024*1024);
33
34ServletFileUploadupload=newServletFileUpload(factory);
35
36try{
37List<FileItem>list=upload.parseRequest(request);
38
39for(FileItemitem:list){
40if(item.isFormField()){
41Stringname=item.getFieldName();
42
43Stringvalue=item.getString("gbk");
44
45System.out.println(name);
46
47request.setAttribute(name,value);
48}else{
49Stringname=item.getFieldName();
50
51Stringvalue=item.getName();
52
53intstart=value.lastIndexOf("\\");
54
55StringfileName=value.substring(start+1);
56
57request.setAttribute(name,fileName);
58
59item.write(newFile(path,fileName));
60
61OutputStreamos=newFileOutputStream(newFile(path,
62fileName));
63
64InputStreamis=item.getInputStream();
65
66byte[]buffer=newbyte[400];
67
68intlength=0;
69
70while((length=is.read(buffer))>0){
71os.write(buffer,0,length);
72}
73
74os.close();
75
76is.close();
77
78}
79}
80}
81
82catch(Exceptionex){
83ex.printStackTrace();
84}
85request.getRequestDispatcher("upload/result2.jsp").forward(request,
86response);
87}
88
89}
2
3importjava.io.File;
4importjava.io.FileOutputStream;
5importjava.io.IOException;
6importjava.io.InputStream;
7importjava.io.OutputStream;
8importjava.util.List;
9
10importjavax.servlet.ServletException;
11importjavax.servlet.http.HttpServlet;
12importjavax.servlet.http.HttpServletRequest;
13importjavax.servlet.http.HttpServletResponse;
14
15importorg.apache.commons.fileupload.FileItem;
16importorg.apache.commons.fileupload.FileUploadException;
17importorg.apache.commons.fileupload.disk.DiskFileItemFactory;
18importorg.apache.commons.fileupload.servlet.ServletFileUpload;
19
20@SuppressWarnings("serial")
21publicclassUploadServletextendsHttpServlet{
22@SuppressWarnings({"unchecked","deprecation"})
23publicvoiddoPost(HttpServletRequestrequest,HttpServletResponseresponse)
24throwsServletException,IOException{
25
26DiskFileItemFactoryfactory=newDiskFileItemFactory();
27
28Stringpath=request.getRealPath("/upload");
29
30factory.setRepository(newFile(path));
31
32factory.setSizeThreshold(1024*1024);
33
34ServletFileUploadupload=newServletFileUpload(factory);
35
36try{
37List<FileItem>list=upload.parseRequest(request);
38
39for(FileItemitem:list){
40if(item.isFormField()){
41Stringname=item.getFieldName();
42
43Stringvalue=item.getString("gbk");
44
45System.out.println(name);
46
47request.setAttribute(name,value);
48}else{
49Stringname=item.getFieldName();
50
51Stringvalue=item.getName();
52
53intstart=value.lastIndexOf("\\");
54
55StringfileName=value.substring(start+1);
56
57request.setAttribute(name,fileName);
58
59item.write(newFile(path,fileName));
60
61OutputStreamos=newFileOutputStream(newFile(path,
62fileName));
63
64InputStreamis=item.getInputStream();
65
66byte[]buffer=newbyte[400];
67
68intlength=0;
69
70while((length=is.read(buffer))>0){
71os.write(buffer,0,length);
72}
73
74os.close();
75
76is.close();
77
78}
79}
80}
81
82catch(Exceptionex){
83ex.printStackTrace();
84}
85request.getRequestDispatcher("upload/result2.jsp").forward(request,
86response);
87}
88
89}
结果:
(三)使用struts2进行文件上传、下载
需引入两个jar包
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
commons-io-1.3.2.jar
这两个jar包在struts2.1.6版本中已经自带,较低版本需到apache网站下载,网址:http://commons.apache.org/
1、单文件上传
upload3.jsp
1<%@pagelanguage="java"contentType="text/html;charset=GB18030"
2pageEncoding="GB18030"%>
3<%@taglibprefix="s"uri="/struts-tags"%>
4<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
8<title>Inserttitlehere</title>
9</head>
10<body>
11<s:formaction="upload"method="post"theme="simple"
12enctype="multipart/form-data">
13<tablealign="center"width="50%"border="1">
14<tr>
15<td>
16username
17</td>
18<td>
19<s:textfieldname="username"></s:textfield>
20</td>
21</tr>
22<tr>
23<td>
24password
25</td>
26<td>
27<s:passwordname="password"></s:password>
28</td>
29</tr>
30<tr>
31<td>
32file
33</td>
34
35<td>
36<s:filename="file"></s:file>
37
38</td>
39</tr>
40<tr>
41<td>
42<s:submitvalue="submit"></s:submit>
43</td>
44<td>
45<s:resetvalue="reset"></s:reset>
46</td>
47</tr>
48</table>
49</s:form>
50</body>
51</html>
2pageEncoding="GB18030"%>
3<%@taglibprefix="s"uri="/struts-tags"%>
4<!DOCTYPEhtmlPUBLIC"-//W3C//DTDHTML4.01Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
5<html>
6<head>
7<metahttp-equiv="Content-Type"content="text/html;charset=GB18030">
8<title>Inserttitlehere</title>
9</head>
10<body>
11<s:formaction="upload"method="post"theme="simple"
12enctype="multipart/form-data">
13<tablealign="center"width="50%"border="1">
14<tr>
15<td>
16username
17</td>
18<td>
19<s:textfieldname="username"></s:textfield>
20</td>
21</tr>
22<tr>
23<td>
24password
25</td>
26<td>
27<s:passwordname="password"></s:password>
28</td>
29</tr>
30<tr>
31<td>
32file
33</td>
34
35<td>
36<s:filename="file"></s:file>
37
38</td>
39</tr>
40<tr>
41<td>
42<s:submitvalue="submit"></s:submit>
43</td>
44<td>
45<s:resetvalue="reset"></s:reset>
46</td>
47</tr>
48</table>
49</s:form>
50</body>
51</html>
web.xml中的配置
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
struts.xml中的配置
1<?xmlversion="1.0"encoding="GBK"?>
2<!DOCTYPEstrutsPUBLIC
3"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
4"http://struts.apache.org/dtds/struts-2.0.dtd">
5
6<struts>
7<constantname="struts.i18n.encoding"value="gbk"></constant>
8<constantname="struts.multipart.saveDir"value="c:\"></constant>
9<packagename="struts2"extends="struts-default">
10<actionname="upload"class="com.test.action.UploadAction">
11<resultname="success">/upload/result3.jsp</result>
12</action>
13</package>
14</struts>
2<!DOCTYPEstrutsPUBLIC
3"-//ApacheSoftwareFoundation//DTDStrutsConfiguration2.0//EN"
4"http://struts.apache.org/dtds/struts-2.0.dtd">
5
6<struts>
7<constantname="struts.i18n.encoding"value="gbk"></constant>
8<constantname="struts.multipart.saveDir"value="c:\"></constant>
9<packagename="struts2"extends="struts-default">
10<actionname="upload"class="com.test.action.UploadAction">
11<resultname="success">/upload/result3.jsp</result>
12</action>
13</package>
14</struts>
UploadAction.java
1packagecom.test.action;
2
3importjava.io.File;
4importjava.io.FileInputStream;
5importjava.io.FileOutputStream;
6importjava.io.InputStream;
7importjava.io.OutputStream;
8
9importorg.apache.struts2.ServletActionContext;
10
11importcom.opensymphony.xwork2.ActionSupport;
12
13@SuppressWarnings("serial")
14publicclassUploadActionextendsActionSupport{
15privateStringusername;
16privateStringpassword;
17privateFilefile;
18privateStringfileFileName;
19privateStringfileContentType;
20
21publicStringgetUsername(){
22returnusername;
23}
24
25publicvoidsetUsername(Stringusername){
26this.username=username;
27}
28
29publicStringgetPassword(){
30returnpassword;
31}
32
33publicvoidsetPassword(Stringpassword){
34this.password=password;
35}
36
37publicFilegetFile(){
38returnfile;
39}
40
41publicvoidsetFile(Filefile){
42this.file=file;
43}
44
45publicStringgetFileFileName(){
46returnfileFileName;
47}
48
49publicvoidsetFileFileName(StringfileFileName){
50this.fileFileName=fileFileName;
51}
52
53publicStringgetFileContentType(){
54returnfileContentType;
55}
56
57publicvoidsetFileContentType(StringfileContentType){
58this.fileContentType=fileContentType;
59}
60
61@SuppressWarnings("deprecation")
62@Override
63publicStringexecute()throwsException{
64InputStreamis=newFileInputStream(file);
65Stringroot=ServletActionContext.getRequest().getRealPath("/upload");
66FiledestFile=newFile(root,this.getFileFileName());
67OutputStreamos=newFileOutputStream(destFile);
68byte[]buffer=newbyte[400];
69
70intlength=0;
71
72while((length=is.read(buffer))>0){
73os.write(buffer,0,length);
74}
75is.close();
76os.close();
77returnSUCCESS;
78}
79}
2
3importjava.io.File;
4importjava.io.FileInputStream;
5importjava.io.FileOutputStream;
6importjava.io.InputStream;
7importjava.io.OutputStream;
8
9importorg.apache.struts2.ServletActionContext;
10
11importcom.opensymphony.xwork2.ActionSupport;
12
13@SuppressWarnings("serial")
14publicclassUploadActionextendsActionSupport{
15privateStringusername;
16privateStringpassword;
17privateFilefile;
18privateStringfileFileName;
19privateStringfileContentType;
20
21publicStringgetUsername(){
22returnusername;
23}
24
25publicvoidsetUsername(Stringusername){
26this.username=username;
27}
28
29publicStringgetPassword(){
30returnpassword;
31}
32
33publicvoidsetPassword(Stringpassword){
34this.password=password;
35}
36
37publicFilegetFile(){
38returnfile;
39}
40
41publicvoidsetFile(Filefile){
42this.file=file;
43}
44
45publicStringgetFileFileName(){
46returnfileFileName;
47}
48
49publicvoidsetFileFileName(StringfileFileName){
50this.fileFileName=fileFileName;
51}
52
53publicStringgetFileContentType(){
54returnfileContentType;
55}
56
57publicvoidsetFileContentType(StringfileContentType){
58this.fileContentType=fileContentType;
59}
60
61@SuppressWarnings("deprecation")
62@Override
63publicStringexecute()throwsException{
64InputStreamis=newFileInputStream(file);
65Stringroot=ServletActionContext.getRequest().getRealPath("/upload");
66FiledestFile=newFile(root,this.getFileFileName());
67OutputStreamos=newFileOutputStream(destFile);
68byte[]buffer=newbyte[400];
69
70intlength=0;
71
72while((length=is.read(buffer))>0){
73os.write(buffer,0,length);
74}
75is.close();
76os.close();
77returnSUCCESS;
78}
79}
结果:
2、多文件上传
修改action
privateList<File>file;
privateList<String>fileFileName;
privateList<String>fileContentType;
privateList<String>fileFileName;
privateList<String>fileContentType;
publicStringexecute()throwsException{
for(inti=0;i<file.size();++i){
InputStreamis=newFileInputStream(file.get(i));
Stringroot=ServletActionContext.getRequest().getRealPath(
"/upload");
FiledestFile=newFile(root,this.getFileFileName().get(i));
OutputStreamos=newFileOutputStream(destFile);
byte[]buffer=newbyte[400];
intlength=0;
while((length=is.read(buffer))>0){
os.write(buffer,0,length);
}
is.close();
os.close();
}
returnSUCCESS;
}
for(inti=0;i<file.size();++i){
InputStreamis=newFileInputStream(file.get(i));
Stringroot=ServletActionContext.getRequest().getRealPath(
"/upload");
FiledestFile=newFile(root,this.getFileFileName().get(i));
OutputStreamos=newFileOutputStream(destFile);
byte[]buffer=newbyte[400];
intlength=0;
while((length=is.read(buffer))>0){
os.write(buffer,0,length);
}
is.close();
os.close();
}
returnSUCCESS;
}
修改upload3.jsp
<tr>
<td>
file1
</td>
<td>
<s:filename="file"></s:file>
</td>
</tr>
<tr>
<td>
file2
</td>
<td>
<s:filename="file"></s:file>
</td>
</tr>
<tr>
<td>
file3
</td>
<td>
<s:filename="file"></s:file>
</td>
</tr>
<td>
file1
</td>
<td>
<s:filename="file"></s:file>
</td>
</tr>
<tr>
<td>
file2
</td>
<td>
<s:filename="file"></s:file>
</td>
</tr>
<tr>
<td>
file3
</td>
<td>
<s:filename="file"></s:file>
</td>
</tr>
结果:
3、任意数量文件上传
在多文件上传的基础上修改upload3.jsp
<scripttype="text/javascript">
functionaddMore()
{
vartd=document.getElementById("more");
varbr=document.createElement("br");
varinput=document.createElement("input");
varbutton=document.createElement("input");
input.type="file";
input.name="file";
button.type="button";
button.value="Remove";
button.onclick=function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>
functionaddMore()
{
vartd=document.getElementById("more");
varbr=document.createElement("br");
varinput=document.createElement("input");
varbutton=document.createElement("input");
input.type="file";
input.name="file";
button.type="button";
button.value="Remove";
button.onclick=function()
{
td.removeChild(br);
td.removeChild(input);
td.removeChild(button);
}
td.appendChild(br);
td.appendChild(input);
td.appendChild(button);
}
</script>
<tr>
<td>
file1
</td>
<tdid="more">
<s:filename="file"></s:file>
<inputtype="button"value="AddMore.."onclick="addMore()">
</td>
</tr>
<td>
file1
</td>
<tdid="more">
<s:filename="file"></s:file>
<inputtype="button"value="AddMore.."onclick="addMore()">
</td>
</tr>
结果:
(四)文件上传类型、大小的限制
使用struts的拦截器,struts2-core-2.1.6.jar/org.apache.struts2.interceptor.FileUploadInterceptor.class的源码中我们可以看到:
publicclassFileUploadInterceptorextendsAbstractInterceptor{
privatestaticfinallongserialVersionUID=-4764627478894962478L;
protectedstaticfinalLoggerLOG=LoggerFactory.getLogger(FileUploadInterceptor.class);
privatestaticfinalStringDEFAULT_MESSAGE="no.message.found";
protectedbooleanuseActionMessageBundle;
protectedLongmaximumSize;
protectedSet<String>allowedTypesSet=Collections.emptySet();
protectedSet<String>allowedExtensionsSet=Collections.emptySet();
privatestaticfinallongserialVersionUID=-4764627478894962478L;
protectedstaticfinalLoggerLOG=LoggerFactory.getLogger(FileUploadInterceptor.class);
privatestaticfinalStringDEFAULT_MESSAGE="no.message.found";
protectedbooleanuseActionMessageBundle;
protectedLongmaximumSize;
protectedSet<String>allowedTypesSet=Collections.emptySet();
protectedSet<String>allowedExtensionsSet=Collections.emptySet();
所以我们只需的struts.xml中配置它的属性allowedTypesSet即可。在action节点中修改拦截器(默认的拦截器中已经有fileUpload拦截器,我们必须提取出来进行参数设置,然后在加上默认的拦截器)。
<actionname="upload"class="com.test.action.UploadAction">
<resultname="success">/upload/result3.jsp</result>
<resultname="input">/upload/upload3.jsp</result>
<interceptor-refname="fileUpload">
<paramname="maximumSize">409600</param>
<paramname="allowedTypes">
application/vnd.ms-powerpoint
</param>
</interceptor-ref>
<interceptor-refname="defaultStack"></interceptor-ref>
</action>
<resultname="success">/upload/result3.jsp</result>
<resultname="input">/upload/upload3.jsp</result>
<interceptor-refname="fileUpload">
<paramname="maximumSize">409600</param>
<paramname="allowedTypes">
application/vnd.ms-powerpoint
</param>
</interceptor-ref>
<interceptor-refname="defaultStack"></interceptor-ref>
</action>
其中<paramname="allowedTypes">application/vnd.ms-powerpoint</param>的allowedTypes的值可在C:\Tomcat
6.0\conf的web.xml文件中查找。
报错信息:
严重:Content-Typenotallowed:file"intrl.txt""upload__138d8aca_120b73e9cf4__8000_00000002.tmp"text/plain
(五)文件的下载
download.jsp
<s:ahref="/MyStruts2/download.action">download</s:a>
DownloadAction.java
1packagecom.test.action;
2
3importjava.io.InputStream;
4
5importorg.apache.struts2.ServletActionContext;
6
7importcom.opensymphony.xwork2.ActionSupport;
8
9publicclassDownloadActionextendsActionSupport{
10publicInputStreamgetDownloadFile(){
11returnServletActionContext.getServletContext().getResourceAsStream(
12"/upload/intrl.ppt");
13}
14
15@Override
16publicStringexecute()throwsException{
17returnSUCCESS;
18}
19}
20
2
3importjava.io.InputStream;
4
5importorg.apache.struts2.ServletActionContext;
6
7importcom.opensymphony.xwork2.ActionSupport;
8
9publicclassDownloadActionextendsActionSupport{
10publicInputStreamgetDownloadFile(){
11returnServletActionContext.getServletContext().getResourceAsStream(
12"/upload/intrl.ppt");
13}
14
15@Override
16publicStringexecute()throwsException{
17returnSUCCESS;
18}
19}
20
web.xml中action配置
<actionname="download"
class="com.test.action.DownloadAction">
<resultname="success"type="stream">
<paramname="contentType">
application/vnd.ms-powerpoint
</param>
<paramname="contentDisposition">
filename="intrl.ppt"
</param>
<paramname="inputName">downloadFile</param>
</result>
</action>
class="com.test.action.DownloadAction">
<resultname="success"type="stream">
<paramname="contentType">
application/vnd.ms-powerpoint
</param>
<paramname="contentDisposition">
filename="intrl.ppt"
</param>
<paramname="inputName">downloadFile</param>
</result>
</action>
相关推荐
在Struts2中,文件上传和下载是常见的功能需求,对于用户交互和数据交换至关重要。以下是对这些知识点的详细阐述: 1. **文件上传**: 在Struts2中,文件上传主要依赖于`Commons FileUpload`库,它是一个Apache提供...
总的来说,Struts2的文件上传和下载功能通过集成Apache Commons FileUpload库,大大简化了开发者的工作,提供了友好的API和配置选项,使得处理文件上传和下载变得轻而易举。开发者只需要关注业务逻辑,无需过多关注...
在Struts2中,文件上传和下载是常见的功能需求,特别是在处理用户交互和数据交换时。这篇博客文章提供的"struts2文件上传下载源代码"旨在帮助开发者理解和实现这些功能。 文件上传功能允许用户从他们的设备上传文件...
在Struts2中,文件上传和下载是常见的功能,对于用户交互和数据交换至关重要。以下是对这一主题的详细阐述。 1. **文件上传** - **MultipartResolver**: 在处理文件上传时,Struts2首先需要一个MultipartResolver...
这个“Struts2 文件上传和下载示例程序”是一个演示如何在Struts2中实现这两个功能的实例。 文件上传是Web应用程序中的一项重要功能,允许用户从本地计算机选择文件并将其传输到服务器。在Struts2中,这一过程涉及...
### Struts2文件上传与下载教程 #### 一、文件上传原理及实现 **1.1 基础概念** 文件上传是Web开发中的常见需求之一。在Struts2框架中,实现文件上传主要依赖于表单的`enctype`属性设置为`multipart/form-data`。...
Struts2是一个强大的Java web框架,它为开发者提供了丰富的功能,包括文件上传和下载。在Struts2中处理文件上传和下载是常见的需求,对于构建交互式的Web应用来说至关重要。以下将详细介绍Struts2中如何实现这两个...
在实际项目中,文件上传和下载功能是必不可少的,本实例将详细讲解如何在Struts2框架下实现单个文件及多个文件的上传与下载。 首先,我们需要在Struts2的配置文件(struts.xml)中添加相关的Action配置,以便处理文件...
Struts2是一个强大的MVC(模型-视图-控制器)框架,广泛应用于Java ...以上就是使用Struts2框架实现文件上传下载的基本步骤和关键知识点。在实际开发中,可以根据项目需求进行调整和优化,确保功能的稳定性和安全性。
Struts2 文件上传和下载是Web开发中常见的情景,主要涉及到HTML表单、HTTP请求、文件处理以及服务器端的响应。以下将详细介绍Struts2框架下如何进行文件的上传和下载。 首先,理解文件上传的基本原理至关重要。当在...
Struts2是一个流行的Java web框架,它为开发者提供了一种优雅的方式来构建动态、结构化的Web...希望本文能为你提供关于Struts2文件上传和下载的基本指导,进一步学习可参考给定的博文链接或其他相关文档和示例代码。
在Struts2中,文件上传和下载是常见的功能需求,主要用于处理用户在Web表单中提交的文件,如图片、文档等。下面将详细介绍Struts2中文件上传和下载的实现方法。 ### 1. 文件上传 #### 1.1 配置Struts2 首先,我们...
在这个特定的项目中,我们关注的是"struts2文件上传下载"的功能,这涉及到用户通过Web界面上传文件到服务器,以及从服务器下载文件到用户的设备。 文件上传是Web应用中的常见需求,例如用户可能需要提交图片、文档...
Struts2是一个强大的Java web框架,它为开发者提供了丰富的功能,包括处理用户表单提交、进行文件上传和下载。在Web应用中,文件上传和下载是常见的需求,例如用户上传头像、下载文档等。Struts2通过其Action类和...
例如,Struts 1和Struts 2在处理文件上传和下载的方式上有显著区别,Struts 2引入了更多面向Action的API和拦截器机制。 5. **源代码分析** 在提供的压缩包文件`upload`中,可能包含了Action类、Struts配置文件、...
这个压缩包包含了实现Struts2文件上传所需的全部jar包,这些库文件对于理解和实现文件上传功能至关重要。 首先,我们要了解Struts2文件上传的基本流程。当用户通过表单提交包含文件输入字段的请求时,Struts2框架会...
综上所述,Struts2文件上传下载和表单重复提交涉及多个技术点,包括Struts2的配置、文件操作、HTTP响应头设置、安全性和异常处理。理解并熟练掌握这些知识点,对于构建健壮的Web应用程序至关重要。