1. 程式人生 > >struts2——多文件上傳

struts2——多文件上傳

div mapping doctype charset cut com web.xml user tle

技術分享圖片
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <%@taglib uri="/struts-tags" prefix="s"%>
 4 <!DOCTYPE html>
 5 <html>
 6     <head>
 7         <meta charset="UTF-8">
 8         <title>struts2的一個例子</title
> 9 </head> 10 <body> 11 <s:form action="files.action" method="post" enctype="multipart/form-data"> 12 <s:textarea name="username" label="用戶名"/> 13 <s:file name="files" label="請選擇上傳文件"/> 14 <s:file name="files" label
="請選擇上傳文件"/> 15 <s:submit value="提交"/> 16 </s:form> 17 18 </body> 19 </html>
index.jsp代碼 技術分享圖片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 3 <filter> 4 <filter-name>struts2</filter-name> 5 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 6 </filter> 7 <filter-mapping> 8 <filter-name>struts2</filter-name> 9 <url-pattern>/*</url-pattern> 10 </filter-mapping> 11 <display-name></display-name> 12 <welcome-file-list> 13 <welcome-file>index.jsp</welcome-file> 14 </welcome-file-list> 15 </web-app>
web.xml代碼 技術分享圖片
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE struts PUBLIC
 3     "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
 4     "http://struts.apache.org/dtds/struts-2.3.dtd">
 5 <struts>
 6     <constant name="struts.devMode" value="true"/>
 7     <package name="hello" extends="struts-default" namespace="/">
 8         <action name="files" class="com.xiaostudy.web.UpFiles" method="upFiles">
 9             <result name="success">/ok.jsp</result>
10             <result name="input">/err.jsp</result>
11         </action>
12     </package>
13 </struts>
struts.xml代碼 技術分享圖片
 1 package com.xiaostudy.web;
 2 
 3 import java.io.File;
 4 
 5 import org.apache.struts2.ServletActionContext;
 6 
 7 import com.opensymphony.xwork2.ActionSupport;
 8 
 9 public class UpFiles extends ActionSupport {
10     
11     public String username;
12     public File[] files;
13     public String filesFileName[];
14     public String filesContentType[];
15     
16     public String upFiles() {
17         
18         String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/files");
19         File file = new File(path);
20         if(!file.exists()) {
21             file.mkdirs();
22             System.out.println("創建了文件夾》》》》》》");
23         }
24         for(int i = 0; i < files.length && files != null; i++) {
25             File file2 = new File(file, filesFileName[i]);
26             System.out.println(files[i]);
27             files[i].renameTo(file2);
28             System.out.println(file);
29             System.out.println(file2);
30         }
31         
32         
33         
34         return SUCCESS;
35     }
36 
37     public String getUsername() {
38         return username;
39     }
40 
41     public void setUsername(String username) {
42         this.username = username;
43     }
44 
45     public File[] getFiles() {
46         return files;
47     }
48 
49     public void setFiles(File[] files) {
50         this.files = files;
51     }
52 
53     public String[] getFilesFileName() {
54         return filesFileName;
55     }
56 
57     public void setFilesFileName(String[] filesFileName) {
58         this.filesFileName = filesFileName;
59     }
60 
61     public String[] getFilesContentType() {
62         return filesContentType;
63     }
64 
65     public void setFilesContentType(String[] filesContentType) {
66         this.filesContentType = filesContentType;
67     }
68 
69 }
action動作類UpFiles 技術分享圖片
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5     <head>
 6         <meta charset="UTF-8">
 7         <title>struts2的一個例子</title>
 8     </head>
 9     <body>
10     okokokok
11     </body>
12 </html>
ok.jsp代碼 技術分享圖片
 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="UTF-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 上傳失敗!!!
11 </body>
12 </html>
err.jsp代碼

struts2——多文件上傳