struts2文件的下传和上载

struts2文件的上传和下载

【Java EE】struts2文件的上传和下载

(一)从底层透析文件上传的实现,此时并没有介入struts2
1、upload.jsp,在form中属性method默认为get,涉及文件上传时必须改为post,默认enctype="application/x-www-form-urlencoded" ,我们暂且不修改,看会有什么结果
 1struts2文件的下传和上载struts2文件的下传和上载<%struts2文件的下传和上载@ page language="java" contentType="text/html; charset=GBK"
 2struts2文件的下传和上载    pageEncoding="GBK"
%>
 3struts2文件的下传和上载<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4struts2文件的下传和上载<html>
 5struts2文件的下传和上载    <head>
 6struts2文件的下传和上载        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7struts2文件的下传和上载        <title>Insert title here</title>
 8struts2文件的下传和上载    </head>
 9struts2文件的下传和上载    <body>
10struts2文件的下传和上载        <form action="result.jsp" method="post"
11struts2文件的下传和上载            enctype="application/x-www-form-urlencoded">
12struts2文件的下传和上载            Information:
13struts2文件的下传和上载            <input type="text" name="info">
14struts2文件的下传和上载            <br>
15struts2文件的下传和上载            File:
16struts2文件的下传和上载            <input type="file" name="file">
17struts2文件的下传和上载            <br>
18struts2文件的下传和上载            <input type="submit" name="submit" value=" submit ">
19struts2文件的下传和上载        </form>
20struts2文件的下传和上载    </body>
21struts2文件的下传和上载</html>
result.jsp
 1struts2文件的下传和上载struts2文件的下传和上载<%struts2文件的下传和上载@ page language="java" contentType="text/html; charset=GBK"
 2struts2文件的下传和上载    pageEncoding="GBK"
%>
 3struts2文件的下传和上载<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4struts2文件的下传和上载<html>
 5struts2文件的下传和上载    <head>
 6struts2文件的下传和上载        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7struts2文件的下传和上载        <title>Insert title here</title>
 8struts2文件的下传和上载    </head>
 9struts2文件的下传和上载    <body>
10struts2文件的下传和上载        Information:<%=request.getParameter("info")%><br>
11struts2文件的下传和上载        File:<%=request.getParameter("file")%><br>
12struts2文件的下传和上载    </body>
13struts2文件的下传和上载</html>
结果:
struts2文件的下传和上载
struts2文件的下传和上载

2、修改result.jsp页面代码,输出读入的流
struts2文件的下传和上载struts2文件的下传和上载<%struts2文件的下传和上载@ page import="java.io.*"%>
struts2文件的下传和上载    <body>
struts2文件的下传和上载struts2文件的下传和上载        
<%struts2文件的下传和上载
struts2文件的下传和上载            InputStream 
is = request.getInputStream();
struts2文件的下传和上载            BufferedReader br 
= new BufferedReader(new InputStreamReader(is));
struts2文件的下传和上载            
String buffer = null;
struts2文件的下传和上载            
while ((buffer = br.readLine()) != null) {
struts2文件的下传和上载                out.print(buffer 
+ "<br>");
struts2文件的下传和上载            }
struts2文件的下传和上载        
%>
struts2文件的下传和上载    
</body>
结果:
struts2文件的下传和上载

这个结果可以断定,文件的上传并没有成功,而仅仅是上传了文件的路径信息而已

3、把upload.jsp中form的enctype属性改为enctype="multipart/form-data"
struts2文件的下传和上载        <form action="result.jsp" method="post" enctype="multipart/form-data">
struts2文件的下传和上载            Information:
struts2文件的下传和上载            
<input type="text" name="info">
struts2文件的下传和上载            
<br>
struts2文件的下传和上载            File:
struts2文件的下传和上载            
<input type="file" name="file">
struts2文件的下传和上载            
<br>
struts2文件的下传和上载            
<input type="submit" name="submit" value=" submit ">
struts2文件的下传和上载        
</form>
结果:
struts2文件的下传和上载 

说明文件上传是成功的。

(二)手动采用fileupload组建进行文件上传
upload2.jsp
 1struts2文件的下传和上载struts2文件的下传和上载<%struts2文件的下传和上载@ page language="java" contentType="text/html; charset=GB18030"
 2struts2文件的下传和上载    pageEncoding="GB18030"
%>
 3struts2文件的下传和上载<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4struts2文件的下传和上载<html>
 5struts2文件的下传和上载    <head>
 6struts2文件的下传和上载        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7struts2文件的下传和上载        <title>Insert title here</title>
 8struts2文件的下传和上载    </head>
 9struts2文件的下传和上载    <body>
10struts2文件的下传和上载        <form action="/MyStruts2/UploadServlet" method="post" enctype="multipart/form-data">
11struts2文件的下传和上载            username:
12struts2文件的下传和上载            <input type="text" name="username">
13struts2文件的下传和上载            <br>
14struts2文件的下传和上载            password:
15struts2文件的下传和上载            <input type="password" name="password">
16struts2文件的下传和上载            <br>
17struts2文件的下传和上载            file1:
18struts2文件的下传和上载            <input type="file" name="file1">
19struts2文件的下传和上载            <br>
20struts2文件的下传和上载            file2:
21struts2文件的下传和上载            <input type="file" name="file2">
22struts2文件的下传和上载            <br>
23struts2文件的下传和上载            <input type="submit" value=" submit ">
24struts2文件的下传和上载        </form>
25struts2文件的下传和上载    </body>
26struts2文件的下传和上载</html>
web.xml中的配置
struts2文件的下传和上载  <servlet>
struts2文件的下传和上载    
<servlet-name>UploadServlet</servlet-name>
struts2文件的下传和上载    
<servlet-class>com.test.servlet.UploadServlet</servlet-class>
struts2文件的下传和上载  
</servlet>
struts2文件的下传和上载
struts2文件的下传和上载  
<servlet-mapping>
struts2文件的下传和上载    
<servlet-name>UploadServlet</servlet-name>
struts2文件的下传和上载    
<url-pattern>/UploadServlet</url-pattern>
struts2文件的下传和上载  
</servlet-mapping>
UploadServle.java
 1struts2文件的下传和上载package com.test.servlet;
 2struts2文件的下传和上载
 3struts2文件的下传和上载import java.io.File;
 4struts2文件的下传和上载import java.io.FileOutputStream;
 5struts2文件的下传和上载import java.io.IOException;
 6struts2文件的下传和上载import java.io.InputStream;
 7struts2文件的下传和上载import java.io.OutputStream;
 8struts2文件的下传和上载import java.util.List;
 9struts2文件的下传和上载
10struts2文件的下传和上载import javax.servlet.ServletException;
11struts2文件的下传和上载import javax.servlet.http.HttpServlet;
12struts2文件的下传和上载import javax.servlet.http.HttpServletRequest;
13struts2文件的下传和上载import javax.servlet.http.HttpServletResponse;
14struts2文件的下传和上载
15struts2文件的下传和上载import org.apache.commons.fileupload.FileItem;
16struts2文件的下传和上载import org.apache.commons.fileupload.FileUploadException;
17struts2文件的下传和上载import org.apache.commons.fileupload.disk.DiskFileItemFactory;
18struts2文件的下传和上载import org.apache.commons.fileupload.servlet.ServletFileUpload;
19struts2文件的下传和上载
20struts2文件的下传和上载@SuppressWarnings("serial")
21struts2文件的下传和上载struts2文件的下传和上载public class UploadServlet extends HttpServlet struts2文件的下传和上载{
22struts2文件的下传和上载struts2文件的下传和上载    @SuppressWarnings( struts2文件的下传和上载"unchecked""deprecation" })
23struts2文件的下传和上载    public void doPost(HttpServletRequest request, HttpServletResponse response)
24struts2文件的下传和上载struts2文件的下传和上载            throws ServletException, IOException struts2文件的下传和上载{
25struts2文件的下传和上载
26struts2文件的下传和上载        DiskFileItemFactory factory = new DiskFileItemFactory();
27struts2文件的下传和上载
28struts2文件的下传和上载        String path = request.getRealPath("/upload");
29struts2文件的下传和上载
30struts2文件的下传和上载        factory.setRepository(new File(path));
31struts2文件的下传和上载
32struts2文件的下传和上载        factory.setSizeThreshold(1024 * 1024);
33struts2文件的下传和上载
34struts2文件的下传和上载        ServletFileUpload upload = new ServletFileUpload(factory);
35struts2文件的下传和上载
36struts2文件的下传和上载struts2文件的下传和上载        try struts2文件的下传和上载{
37struts2文件的下传和上载            List<FileItem> list = upload.parseRequest(request);
38struts2文件的下传和上载
39struts2文件的下传和上载struts2文件的下传和上载            for (FileItem item : list) struts2文件的下传和上载{
40struts2文件的下传和上载struts2文件的下传和上载                if (item.isFormField()) struts2文件的下传和上载{
41struts2文件的下传和上载                    String name = item.getFieldName();
42struts2文件的下传和上载
43struts2文件的下传和上载                    String value = item.getString("gbk");
44struts2文件的下传和上载
45struts2文件的下传和上载                    System.out.println(name);
46struts2文件的下传和上载
47struts2文件的下传和上载                    request.setAttribute(name, value);
48struts2文件的下传和上载struts2文件的下传和上载                }
 else struts2文件的下传和上载{
49struts2文件的下传和上载                    String name = item.getFieldName();
50struts2文件的下传和上载
51struts2文件的下传和上载                    String value = item.getName();
52struts2文件的下传和上载
53struts2文件的下传和上载                    int start = value.lastIndexOf("\\");
54struts2文件的下传和上载
55struts2文件的下传和上载                    String fileName = value.substring(start + 1);
56struts2文件的下传和上载
57struts2文件的下传和上载                    request.setAttribute(name, fileName);
58struts2文件的下传和上载
59struts2文件的下传和上载                    item.write(new File(path, fileName));
60struts2文件的下传和上载
61struts2文件的下传和上载                    OutputStream os = new FileOutputStream(new File(path,
62struts2文件的下传和上载                            fileName));
63struts2文件的下传和上载
64struts2文件的下传和上载                    InputStream is = item.getInputStream();
65struts2文件的下传和上载
66struts2文件的下传和上载                    byte[] buffer = new byte[400];
67struts2文件的下传和上载
68struts2文件的下传和上载                    int length = 0;
69struts2文件的下传和上载
70struts2文件的下传和上载struts2文件的下传和上载                    while ((length = is.read(buffer)) > 0struts2文件的下传和上载{
71struts2文件的下传和上载                        os.write(buffer, 0, length);
72struts2文件的下传和上载                    }

73struts2文件的下传和上载
74struts2文件的下传和上载                    os.close();
75struts2文件的下传和上载
76struts2文件的下传和上载                    is.close();
77struts2文件的下传和上载
78struts2文件的下传和上载                }

79struts2文件的下传和上载            }

80struts2文件的下传和上载        }

81struts2文件的下传和上载
82struts2文件的下传和上载struts2文件的下传和上载        catch (Exception ex) struts2文件的下传和上载{
83struts2文件的下传和上载            ex.printStackTrace();
84struts2文件的下传和上载        }

85struts2文件的下传和上载        request.getRequestDispatcher("upload/result2.jsp").forward(request,
86struts2文件的下传和上载                response);
87struts2文件的下传和上载    }

88struts2文件的下传和上载
89struts2文件的下传和上载}
结果:
struts2文件的下传和上载
struts2文件的下传和上载


(三)使用struts2进行文件上传、下载
需引入两个jar包
struts2文件的下传和上载commons-fileupload-1.2.1.jar
struts2文件的下传和上载commons-io-1.3.2.jar
这两个jar包在struts2.1.6版本中已经自带,较低版本需到apache网站下载,网址:http://commons.apache.org/

1、单文件上传
upload3.jsp
 1struts2文件的下传和上载struts2文件的下传和上载<%struts2文件的下传和上载@ page language="java" contentType="text/html; charset=GB18030"
 2struts2文件的下传和上载    pageEncoding="GB18030"
%>
 3struts2文件的下传和上载struts2文件的下传和上载<%struts2文件的下传和上载@ taglib prefix="s" uri="/struts-tags"%>
 4struts2文件的下传和上载<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5struts2文件的下传和上载<html>
 6struts2文件的下传和上载    <head>
 7struts2文件的下传和上载        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 8struts2文件的下传和上载        <title>Insert title here</title>
 9struts2文件的下传和上载    </head>
10struts2文件的下传和上载    <body>
11struts2文件的下传和上载        <s:form action="upload" method="post" theme="simple"
12struts2文件的下传和上载            enctype="multipart/form-data">
13struts2文件的下传和上载            <table align="center" width="50%" border="1">
14struts2文件的下传和上载                <tr>
15struts2文件的下传和上载                    <td>
16struts2文件的下传和上载                        username
17struts2文件的下传和上载                    </td>
18struts2文件的下传和上载                    <td>
19struts2文件的下传和上载                        <s:textfield name="username"></s:textfield>
20struts2文件的下传和上载                    </td>
21struts2文件的下传和上载                </tr>
22struts2文件的下传和上载                <tr>
23struts2文件的下传和上载                    <td>
24struts2文件的下传和上载                        password
25struts2文件的下传和上载                    </td>
26struts2文件的下传和上载                    <td>
27struts2文件的下传和上载                        <s:password name="password"></s:password>
28struts2文件的下传和上载                    </td>
29struts2文件的下传和上载                </tr>
30struts2文件的下传和上载                <tr>
31struts2文件的下传和上载                    <td>
32struts2文件的下传和上载                        file
33struts2文件的下传和上载                    </td>
34struts2文件的下传和上载
35struts2文件的下传和上载                    <td>
36struts2文件的下传和上载                        <s:file name="file"></s:file>
37struts2文件的下传和上载                       
38struts2文件的下传和上载                    </td>
39struts2文件的下传和上载                </tr>
40struts2文件的下传和上载                <tr>
41struts2文件的下传和上载                    <td>
42struts2文件的下传和上载                        <s:submit value=" submit "></s:submit>
43struts2文件的下传和上载                    </td>
44struts2文件的下传和上载                    <td>
45struts2文件的下传和上载                        <s:reset value=" reset "></s:reset>
46struts2文件的下传和上载                    </td>
47struts2文件的下传和上载                </tr>
48struts2文件的下传和上载            </table>
49struts2文件的下传和上载        </s:form>
50struts2文件的下传和上载    </body>
51struts2文件的下传和上载</html>
web.xml中的配置
struts2文件的下传和上载    <filter>
struts2文件的下传和上载        
<filter-name>struts2</filter-name>
struts2文件的下传和上载        
<filter-class>
struts2文件的下传和上载            org.apache.struts2.dispatcher.FilterDispatcher
struts2文件的下传和上载        
</filter-class>
struts2文件的下传和上载    
</filter>
struts2文件的下传和上载
struts2文件的下传和上载    
<filter-mapping>
struts2文件的下传和上载        
<filter-name>struts2</filter-name>
struts2文件的下传和上载        
<url-pattern>/*</url-pattern>
struts2文件的下传和上载    
</filter-mapping>

struts.xml中的配置
 1struts2文件的下传和上载<?xml version="1.0" encoding="GBK" ?>
 2struts2文件的下传和上载<!DOCTYPE struts PUBLIC
 3struts2文件的下传和上载    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4struts2文件的下传和上载    "http://struts.apache.org/dtds/struts-2.0.dtd">
 5struts2文件的下传和上载    
 6struts2文件的下传和上载<struts>
 7struts2文件的下传和上载    <constant name="struts.i18n.encoding" value="gbk"></constant>
 8struts2文件的下传和上载    <constant name="struts.multipart.saveDir" value="c:\"></constant>
 9struts2文件的下传和上载    <package name="struts2" extends="struts-default">
10struts2文件的下传和上载        <action name="upload" class="com.test.action.UploadAction">
11struts2文件的下传和上载            <result name="success">/upload/result3.jsp</result>
12struts2文件的下传和上载        </action>
13struts2文件的下传和上载    </package>
14struts2文件的下传和上载</struts>
UploadAction.java
 1struts2文件的下传和上载package com.test.action;
 2struts2文件的下传和上载
 3struts2文件的下传和上载import java.io.File;
 4struts2文件的下传和上载import java.io.FileInputStream;
 5struts2文件的下传和上载import java.io.FileOutputStream;
 6struts2文件的下传和上载import java.io.InputStream;
 7struts2文件的下传和上载import java.io.OutputStream;
 8struts2文件的下传和上载
 9struts2文件的下传和上载import org.apache.struts2.ServletActionContext;
10struts2文件的下传和上载
11struts2文件的下传和上载import com.opensymphony.xwork2.ActionSupport;
12struts2文件的下传和上载
13struts2文件的下传和上载@SuppressWarnings("serial")
14struts2文件的下传和上载struts2文件的下传和上载public class UploadAction extends ActionSupport struts2文件的下传和上载{
15struts2文件的下传和上载    private String username;
16struts2文件的下传和上载    private String password;
17struts2文件的下传和上载    private File file;
18struts2文件的下传和上载    private String fileFileName;
19struts2文件的下传和上载    private String fileContentType;
20struts2文件的下传和上载
21struts2文件的下传和上载struts2文件的下传和上载    public String getUsername() struts2文件的下传和上载{
22struts2文件的下传和上载        return username;
23struts2文件的下传和上载    }

24struts2文件的下传和上载
25struts2文件的下传和上载struts2文件的下传和上载    public void setUsername(String username) struts2文件的下传和上载{
26struts2文件的下传和上载        this.username = username;
27struts2文件的下传和上载    }

28struts2文件的下传和上载
29struts2文件的下传和上载struts2文件的下传和上载    public String getPassword() struts2文件的下传和上载{
30struts2文件的下传和上载        return password;
31struts2文件的下传和上载    }

32struts2文件的下传和上载
33struts2文件的下传和上载struts2文件的下传和上载    public void setPassword(String password) struts2文件的下传和上载{
34struts2文件的下传和上载        this.password = password;
35struts2文件的下传和上载    }

36struts2文件的下传和上载
37struts2文件的下传和上载struts2文件的下传和上载    public File getFile() struts2文件的下传和上载{
38struts2文件的下传和上载        return file;
39struts2文件的下传和上载    }

40struts2文件的下传和上载
41struts2文件的下传和上载struts2文件的下传和上载    public void setFile(File file) struts2文件的下传和上载{
42struts2文件的下传和上载        this.file = file;
43struts2文件的下传和上载    }

44struts2文件的下传和上载
45struts2文件的下传和上载struts2文件的下传和上载    public String getFileFileName() struts2文件的下传和上载{
46struts2文件的下传和上载        return fileFileName;
47struts2文件的下传和上载    }

48struts2文件的下传和上载
49struts2文件的下传和上载struts2文件的下传和上载    public void setFileFileName(String fileFileName) struts2文件的下传和上载{
50struts2文件的下传和上载        this.fileFileName = fileFileName;
51struts2文件的下传和上载    }

52struts2文件的下传和上载
53struts2文件的下传和上载struts2文件的下传和上载    public String getFileContentType() struts2文件的下传和上载{
54struts2文件的下传和上载        return fileContentType;
55struts2文件的下传和上载    }

56struts2文件的下传和上载
57struts2文件的下传和上载struts2文件的下传和上载    public void setFileContentType(String fileContentType) struts2文件的下传和上载{
58struts2文件的下传和上载        this.fileContentType = fileContentType;
59struts2文件的下传和上载    }

60struts2文件的下传和上载
61struts2文件的下传和上载    @SuppressWarnings("deprecation")
62struts2文件的下传和上载    @Override
63struts2文件的下传和上载struts2文件的下传和上载    public String execute() throws Exception struts2文件的下传和上载{
64struts2文件的下传和上载        InputStream is = new FileInputStream(file);
65struts2文件的下传和上载        String root = ServletActionContext.getRequest().getRealPath("/upload");
66struts2文件的下传和上载        File destFile = new File(root, this.getFileFileName());
67struts2文件的下传和上载        OutputStream os = new FileOutputStream(destFile);
68struts2文件的下传和上载        byte[] buffer = new byte[400];
69struts2文件的下传和上载
70struts2文件的下传和上载        int length = 0;
71struts2文件的下传和上载
72struts2文件的下传和上载struts2文件的下传和上载        while ((length = is.read(buffer)) > 0struts2文件的下传和上载{
73struts2文件的下传和上载            os.write(buffer, 0, length);
74struts2文件的下传和上载        }

75struts2文件的下传和上载        is.close();
76struts2文件的下传和上载        os.close();
77struts2文件的下传和上载        return SUCCESS;
78struts2文件的下传和上载    }

79struts2文件的下传和上载}

结果:
struts2文件的下传和上载
struts2文件的下传和上载 


2、多文件上传
修改action
struts2文件的下传和上载    private List<File> file;
struts2文件的下传和上载    
private List<String> fileFileName;
struts2文件的下传和上载    
private List<String> fileContentType;
struts2文件的下传和上载struts2文件的下传和上载    public String execute() throws Exception struts2文件的下传和上载{
struts2文件的下传和上载struts2文件的下传和上载        
for (int i = 0; i < file.size(); ++i) struts2文件的下传和上载{
struts2文件的下传和上载            InputStream is 
= new FileInputStream(file.get(i));
struts2文件的下传和上载            String root 
= ServletActionContext.getRequest().getRealPath(
struts2文件的下传和上载                    
"/upload");
struts2文件的下传和上载            File destFile 
= new File(root, this.getFileFileName().get(i));
struts2文件的下传和上载            OutputStream os 
= new FileOutputStream(destFile);
struts2文件的下传和上载            
byte[] buffer = new byte[400];
struts2文件的下传和上载
struts2文件的下传和上载            
int length = 0;
struts2文件的下传和上载
struts2文件的下传和上载struts2文件的下传和上载            
while ((length = is.read(buffer)) > 0struts2文件的下传和上载{
struts2文件的下传和上载                os.write(buffer, 
0, length);
struts2文件的下传和上载            }

struts2文件的下传和上载            is.close();
struts2文件的下传和上载            os.close();
struts2文件的下传和上载        }

struts2文件的下传和上载        
return SUCCESS;
struts2文件的下传和上载    }
修改upload3.jsp
struts2文件的下传和上载                <tr>
struts2文件的下传和上载                    
<td>
struts2文件的下传和上载                        file1
struts2文件的下传和上载                    
</td>
struts2文件的下传和上载                    
<td>
struts2文件的下传和上载                        
<s:file name="file"></s:file>
struts2文件的下传和上载                    
</td>
struts2文件的下传和上载                
</tr>
struts2文件的下传和上载                
<tr>
struts2文件的下传和上载                    
<td>
struts2文件的下传和上载                        file2
struts2文件的下传和上载                    
</td>
struts2文件的下传和上载                    
<td>
struts2文件的下传和上载                        
<s:file name="file"></s:file>
struts2文件的下传和上载                    
</td>
struts2文件的下传和上载                
</tr>
struts2文件的下传和上载                
<tr>
struts2文件的下传和上载                    
<td>
struts2文件的下传和上载                        file3
struts2文件的下传和上载                    
</td>
struts2文件的下传和上载                    
<td>
struts2文件的下传和上载                        
<s:file name="file"></s:file>
struts2文件的下传和上载                    
</td>
struts2文件的下传和上载                
</tr>
结果:
struts2文件的下传和上载
struts2文件的下传和上载


3、任意数量文件上传
在多文件上传的基础上修改upload3.jsp
struts2文件的下传和上载struts2文件的下传和上载        <script type="text/javascript">struts2文件的下传和上载
struts2文件的下传和上载        
function addMore()
struts2文件的下传和上载struts2文件的下传和上载        
struts2文件的下传和上载{
struts2文件的下传和上载            
var td = document.getElementById("more");
struts2文件的下传和上载            
var br = document.createElement("br");
struts2文件的下传和上载            
var input = document.createElement("input");
struts2文件的下传和上载            
var button = document.createElement("input");
struts2文件的下传和上载            input.type 
= "file";
struts2文件的下传和上载            input.name 
= "file";
struts2文件的下传和上载            button.type 
= "button";
struts2文件的下传和上载            button.value 
= "Remove";
struts2文件的下传和上载            button.onclick 
= function()
struts2文件的下传和上载struts2文件的下传和上载            
struts2文件的下传和上载{
struts2文件的下传和上载                td.removeChild(br);
struts2文件的下传和上载                td.removeChild(input);
struts2文件的下传和上载                td.removeChild(button);
struts2文件的下传和上载            }

struts2文件的下传和上载            td.appendChild(br);
struts2文件的下传和上载            td.appendChild(input);
struts2文件的下传和上载            td.appendChild(button);
struts2文件的下传和上载        }

struts2文件的下传和上载    
</script>
struts2文件的下传和上载                <tr>
struts2文件的下传和上载                    
<td>
struts2文件的下传和上载                        file1
struts2文件的下传和上载                    
</td>
struts2文件的下传和上载                    
<td id="more">
struts2文件的下传和上载                        
<s:file name="file"></s:file>
struts2文件的下传和上载                        
<input type="button" value="Add More.." onclick="addMore()">
struts2文件的下传和上载                    
</td>
struts2文件的下传和上载                
</tr>

结果:
struts2文件的下传和上载

(四)文件上传类型、大小的限制
使用struts的拦截器,struts2-core-2.1.6.jar/org.apache.struts2.interceptor.FileUploadInterceptor.class的源码中我们可以看到:
struts2文件的下传和上载public class FileUploadInterceptor extends AbstractInterceptor {
struts2文件的下传和上载
struts2文件的下传和上载    private static final long serialVersionUID = -4764627478894962478L;
struts2文件的下传和上载
struts2文件的下传和上载    protected static final Logger LOG = LoggerFactory.getLogger(FileUploadInterceptor.class);
struts2文件的下传和上载    private static final String DEFAULT_MESSAGE = "no.message.found";
struts2文件的下传和上载
struts2文件的下传和上载    protected boolean useActionMessageBundle;
struts2文件的下传和上载
struts2文件的下传和上载    protected Long maximumSize;
struts2文件的下传和上载    protected Set
<String> allowedTypesSet = Collections.emptySet();
struts2文件的下传和上载    protected Set
<String> allowedExtensionsSet = Collections.emptySet();
所以我们只需的struts.xml中配置它的属性allowedTypesSet即可。在action节点中修改拦截器(默认的拦截器中已经有fileUpload拦截器,我们必须提取出来进行参数设置,然后在加上默认的拦截器)。
struts2文件的下传和上载        <action name="upload" class="com.test.action.UploadAction">
struts2文件的下传和上载            
<result name="success">/upload/result3.jsp</result>
struts2文件的下传和上载            
<result name="input">/upload/upload3.jsp</result>
struts2文件的下传和上载            
<interceptor-ref name="fileUpload">
struts2文件的下传和上载                
<param name="maximumSize">409600</param>
struts2文件的下传和上载                
<param name="allowedTypes">
struts2文件的下传和上载                    application/vnd.ms-powerpoint
struts2文件的下传和上载                
</param>
struts2文件的下传和上载            
</interceptor-ref>
struts2文件的下传和上载            
<interceptor-ref name="defaultStack"></interceptor-ref>
struts2文件的下传和上载        
</action>
其中<param name="allowedTypes">application/vnd.ms-powerpoint</param>allowedTypes的值可在C:\Tomcat 6.0\conf的web.xml文件中查找。
struts2文件的下传和上载
报错信息:
struts2文件的下传和上载严重: Content-Type not allowed: file "intrl.txt" "upload__138d8aca_120b73e9cf4__8000_00000002.tmp" text/plain


(五)文件的下载
download.jsp
struts2文件的下传和上载        <s:a href="/MyStruts2/download.action">download</s:a>
DownloadAction.java
 1struts2文件的下传和上载package com.test.action;
 2struts2文件的下传和上载
 3struts2文件的下传和上载import java.io.InputStream;
 4struts2文件的下传和上载
 5struts2文件的下传和上载import org.apache.struts2.ServletActionContext;
 6struts2文件的下传和上载
 7struts2文件的下传和上载import com.opensymphony.xwork2.ActionSupport;
 8struts2文件的下传和上载
 9struts2文件的下传和上载struts2文件的下传和上载public class DownloadAction extends ActionSupport struts2文件的下传和上载{
10struts2文件的下传和上载struts2文件的下传和上载    public InputStream getDownloadFile() struts2文件的下传和上载{
11struts2文件的下传和上载        return ServletActionContext.getServletContext().getResourceAsStream(
12struts2文件的下传和上载                "/upload/intrl.ppt");
13struts2文件的下传和上载    }

14struts2文件的下传和上载
15struts2文件的下传和上载    @Override
16struts2文件的下传和上载struts2文件的下传和上载    public String execute() throws Exception struts2文件的下传和上载{
17struts2文件的下传和上载        return SUCCESS;
18struts2文件的下传和上载    }

19struts2文件的下传和上载}

20struts2文件的下传和上载
web.xml中action配置
struts2文件的下传和上载        <action name="download"
struts2文件的下传和上载            class
="com.test.action.DownloadAction">
struts2文件的下传和上载            
<result name="success" type="stream">
struts2文件的下传和上载                
<param name="contentType">
struts2文件的下传和上载                    application/vnd.ms-powerpoint
struts2文件的下传和上载                
</param>
struts2文件的下传和上载                
<param name="contentDisposition">
struts2文件的下传和上载                    filename="intrl.ppt"
struts2文件的下传和上载                
</param>
struts2文件的下传和上载                
<param name="inputName">downloadFile</param>
struts2文件的下传和上载            
</result>
struts2文件的下传和上载        
</action>
结果:
struts2文件的下传和上载
FROM:http://www.cnitblog.com/intrl/archive/2009/04/18/56447.html