Struts2單個檔案和多個檔案上傳
阿新 • • 發佈:2018-11-30
<一>簡述:
Struts2的檔案上傳其實也是通過攔截器來實現的,只是該攔截器定義為預設攔截器了,所以不用自己去手工配置,<interceptor name=”fileUpload” class=”org.apache.struts2.interceptor.FileUploadInterceptor”/>
<二>指定使用者上傳檔案的大小,有兩種方式:
1)預設是在default.properties 檔案的 struts.multipart.maxSize=2097152 鍵值指定為2097152 也就是2M,通過計算 2097152/(1024*1024) = 2 M
那我們可以改變其預設值,只要在src目錄下,新建一個 struts.properties 檔案,指定上傳大小 如下:
一次上傳只可以上傳10M,不管一次上傳多少個檔案,按總和計算
2)在struts.xml檔案中指定,如圖:
其實name就對應struts.properties的鍵,value對應 值
注意:如果即在struts.properties設定檔案上傳大小,又在struts.xml 設定檔案上傳大小,則struts.properties的優先順序高於struts.xml,一般在一處指定上傳大小即可,推薦 struts.properties
<三>Struts2之單檔案上傳
1.fileupload.jsp
[html] view plain copy print ?
- <%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
- <%
- String path
- String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
- %>
- <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>
- <html>
- <head>
- <base href=“<%=basePath%>”>
- <title>My JSP ‘fileupload.jsp’ starting page</title>
- <meta http-equiv=“pragma” content=“no-cache”>
- <meta http-equiv=“cache-control” content=“no-cache”>
- <meta http-equiv=“expires” content=“0”>
- <meta http-equiv=“keywords” content=“keyword1,keyword2,keyword3”>
- <meta http-equiv=“description” content=“This is my page”>
- <!–
- <link rel=”stylesheet” type=”text/css” href=”styles.css”>
- –>
- </head>
- <body>
- <!– enctype 預設是 application/x-www-form-urlencoded –>
- <form action=“FileUpload2” enctype=“multipart/form-data” method=“post” >
- 使用者名稱:<input type=“text” name=“usename”> <br/>
- 上傳檔案:<input type=“file” name=“file1”><br/>
- <input type=“submit” value=“提交”/>
- </form>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'fileupload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- enctype 預設是 application/x-www-form-urlencoded -->
<form action="FileUpload2" enctype="multipart/form-data" method="post" >
使用者名稱:<input type="text" name="usename"> <br/>
上傳檔案:<input type="file" name="file1"><br/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
2.具體處理上傳的 FileUpload.java
[java] view plain copy print ?
- package com.struts2.fileupload;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * 單個檔案上傳
- * @author Administrator
- * 上傳檔案其實是上傳了兩份,
- *
- * 首先將上傳的檔案儲存到 default.properties 檔案中 struts.multipart.saveDir鍵指定的目錄中
- * 預設是空的
- * 儲存在 Tomcat 6.0\work\Catalina\localhost\struts2目錄下以.tmp字尾名的檔案
- *
- * 如果要在 struts.multipart.saveDir 指定目錄, 則可以在 src資料夾下 建一個 struts.properties,
- * 覆蓋 default.properties 的某些鍵值
- *
- * 還有一份是 存放在自己設定的目錄下
- */
- public class FileUpload extends ActionSupport {
- private String usename ;
- private File file1 ; //具體上傳檔案的 引用 , 指向臨時目錄中的臨時檔案
- private String file1FileName ; // 上傳檔案的名字 ,FileName 固定的寫法
- private String file1ContentType ; //上傳檔案的型別, ContentType 固定的寫法
- public String getUsename() {
- return usename;
- }
- public void setUsename(String usename) {
- this.usename = usename;
- }
- public File getFile1() {
- return file1;
- }
- public void setFile1(File file1) {
- this.file1 = file1;
- }
- public String getFile1FileName() {
- return file1FileName;
- }
- public void setFile1FileName(String file1FileName) {
- this.file1FileName = file1FileName;
- }
- public String getFile1ContentType() {
- return file1ContentType;
- }
- public void setFile1ContentType(String file1ContentType) {
- this.file1ContentType = file1ContentType;
- }
- @Override
- public String execute() throws Exception {
- //獲取檔案儲存路徑
- String path = ServletActionContext.getRequest().getRealPath(”/upload”);
- //輸出流
- OutputStream os = new FileOutputStream(new File(path,file1FileName));
- //輸入流
- InputStream is = new FileInputStream(file1);
- byte[] buf = new byte[1024];
- int length = 0 ;
- while(-1 != (length = is.read(buf) ) )
- {
- os.write(buf, 0, length) ;
- }
- is.close();
- os.close();
- return SUCCESS;
- }
- }
package com.struts2.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 單個檔案上傳
* @author Administrator
* 上傳檔案其實是上傳了兩份,
*
* 首先將上傳的檔案儲存到 default.properties 檔案中 struts.multipart.saveDir鍵指定的目錄中
* 預設是空的
* 儲存在 Tomcat 6.0\work\Catalina\localhost\struts2目錄下以.tmp字尾名的檔案
*
* 如果要在 struts.multipart.saveDir 指定目錄, 則可以在 src資料夾下 建一個 struts.properties,
* 覆蓋 default.properties 的某些鍵值
*
* 還有一份是 存放在自己設定的目錄下
*/
public class FileUpload extends ActionSupport {
private String usename ;
private File file1 ; //具體上傳檔案的 引用 , 指向臨時目錄中的臨時檔案
private String file1FileName ; // 上傳檔案的名字 ,FileName 固定的寫法
private String file1ContentType ; //上傳檔案的型別, ContentType 固定的寫法
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public File getFile1() {
return file1;
}
public void setFile1(File file1) {
this.file1 = file1;
}
public String getFile1FileName() {
return file1FileName;
}
public void setFile1FileName(String file1FileName) {
this.file1FileName = file1FileName;
}
public String getFile1ContentType() {
return file1ContentType;
}
public void setFile1ContentType(String file1ContentType) {
this.file1ContentType = file1ContentType;
}
@Override
public String execute() throws Exception {
//獲取檔案儲存路徑
String path = ServletActionContext.getRequest().getRealPath("/upload");
//輸出流
OutputStream os = new FileOutputStream(new File(path,file1FileName));
//輸入流
InputStream is = new FileInputStream(file1);
byte[] buf = new byte[1024];
int length = 0 ;
while(-1 != (length = is.read(buf) ) )
{
os.write(buf, 0, length) ;
}
is.close();
os.close();
return SUCCESS;
}
}
3.最終顯示結果的頁面,filedemo.jsp
[java] view plain copy print ?
- <%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
- <%@ taglib prefix=”s” uri=“/struts-tags” %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+”://”+request.getServerName()+“:”+request.getServerPort()+path+“/”;
- %>
- <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>
- <html>
- <head>
- <base href=”<%=basePath%>”>
- <title>My JSP ’filedemo.jsp’ starting page</title>
- <meta http-equiv=”pragma” content=“no-cache”>
- <meta http-equiv=”cache-control” content=“no-cache”>
- <meta http-equiv=”expires” content=“0”>
- <meta http-equiv=”keywords” content=“keyword1,keyword2,keyword3”>
- <meta http-equiv=”description” content=“This is my page”>
- <!–
- <link rel=”stylesheet” type=“text/css” href=“styles.css”>
- –>
- </head>
- <body>
- 上傳成功: <br/>
- usename: <s:property value=”usename” /><br/>
- file: <s:property value=”file1FileName”/><br/>
- contentType: <s:property value=”file1ContentType”/>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'filedemo.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
上傳成功: <br/>
usename: <s:property value="usename" /><br/>
file: <s:property value="file1FileName"/><br/>
contentType: <s:property value="file1ContentType"/>
</body>
</html>
<四>Struts2之多檔案上傳
1.fileupload.jsp
[html] view plain copy print ?
- <%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
- %>
- <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>
- <html>
- <head>
- <base href=“<%=basePath%>”>
- <title>My JSP ‘fileupload.jsp’ starting page</title>
- <meta http-equiv=“pragma” content=“no-cache”>
- <meta http-equiv=“cache-control” content=“no-cache”>
- <meta http-equiv=“expires” content=“0”>
- <meta http-equiv=“keywords” content=“keyword1,keyword2,keyword3”>
- <meta http-equiv=“description” content=“This is my page”>
- <!–
- <link rel=”stylesheet” type=”text/css” href=”styles.css”>
- –>
- </head>
- <body>
- <!– enctype 預設是 application/x-www-form-urlencoded –>
- <form action=“FileUpload2” enctype=“multipart/form-data” method=“post” >
- 使用者名稱:<input type=“text” name=“usename”> <br/>
- 上傳檔案:<input type=“file” name=“file1”><br/>
- 上傳檔案: <input type=“file” name=“file1”><br/> <!– 兩個名字相同 都是file1 –>
- <input type=“submit” value=“提交”/>
- </form>
- </body>
- </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'fileupload.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<!-- enctype 預設是 application/x-www-form-urlencoded -->
<form action="FileUpload2" enctype="multipart/form-data" method="post" >
使用者名稱:<input type="text" name="usename"> <br/>
上傳檔案:<input type="file" name="file1"><br/>
上傳檔案: <input type="file" name="file1"><br/> <!-- 兩個名字相同 都是file1 -->
<input type="submit" value="提交"/>
</form>
</body>
</html>
兩個上傳檔案的name屬性值要是一樣的,後臺方便處理
2.具體處理上傳檔案的FileUpload2.Java
多檔案上傳用集合的方式
[java] view plain copy print ?
- package com.struts2.fileupload;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.InputStream;
- import java.io.OutputStream;
- import java.util.List;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- /**
- * 多檔案上傳,用集合的方式
- * @author Administrator
- *
- */
- public class FileUpload2 extends ActionSupport {
- private String usename ;
- private List<File> file1 ;
- private List<String> file1FileName ;
- private List<String> file1ContentType ;
- public String getUsename() {
- return usename;
- }
- public void setUsename(String usename) {
- this.usename = usename;
- }
- public List<File> getFile1() {
- return file1;
- }
- public void setFile1(List<File> file1) {
- this.file1 = file1;
- }
- public List<String> getFile1FileName() {
- return file1FileName;
- }
- public void setFile1FileName(List<String> file1FileName) {
- this.file1FileName = file1FileName;
- }
- public List<String> getFile1ContentType() {
- return file1ContentType;
- }
- public void setFile1ContentType(List<String> file1ContentType) {
- this.file1ContentType = file1ContentType;
- }
- @Override
- public String execute() throws Exception {
- //獲取檔案儲存路徑
- String path = ServletActionContext.getRequest().getRealPath(”/upload”);
- for(int i = 0 ; i < file1.size() ; i++ )
- {
- OutputStream os = new FileOutputStream(new File(path,file1FileName.get(i)));
- InputStream is = new FileInputStream(file1.get(i));
- byte[] buf = new byte[1024];
- int length = 0 ;
- while(-1 != (length = is.read(buf) ) )
- {
- os.write(buf, 0, length) ;
- }
- is.close();
- os.close();
- }
- return SUCCESS;
- }
- }
package com.struts2.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
/**
* 多檔案上傳,用集合的方式
* @author Administrator
*
*/
public class FileUpload2 extends ActionSupport {
private String usename ;
private List<File> file1 ;
private List<String> file1FileName ;
private List<String> file1ContentType ;
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public List<File> getFile1() {
return file1;
}
public void setFile1(List<File> file1) {
this.file1 = file1;
}
public List<String> getFile1FileName() {
return file1FileName;
}
public void setFile1FileName(List<String> file1FileName) {
this.file1FileName = file1FileName;
}
public List<String> getFile1ContentType() {
return file1ContentType;
}
public void setFile1ContentType(List<String> file1ContentType) {
this.file1ContentType = file1ContentType;
}
@Override
public String execute() throws Exception {
//獲取檔案儲存路徑
String path = ServletActionContext.getRequest().getRealPath("/upload");
for(int i = 0 ; i < file1.size() ; i++ )
{
OutputStream os = new FileOutputStream(new File(path,file1FileName.get(i)));
InputStream is = new FileInputStream(file1.get(i));
byte[] buf = new byte[1024];
int length = 0 ;
while(-1 != (length = is.read(buf) ) )
{
os.write(buf, 0, length) ;
}
is.close();
os.close();
}
return SUCCESS;
}
}
3.用於顯示的介面filedemo.jsp
[html] view plain copy print ?
- <%@ page language=“java” import=“java.util.*” pageEncoding=“UTF-8”%>
- <%@ taglib prefix=“s” uri=“/struts-tags” %>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+”://”+request.getServerName()+”:”+request.getServerPort()+path+”/”;
- %>
- <!DOCTYPE HTML PUBLIC ”-//W3C//DTD HTML 4.01 Transitional//EN”>
- <html>
- <head>
- <base href=“<%=basePath%>”>
- <title>My JSP ‘filedemo2.jsp’ starting page</title>
- <meta http-equiv=“pragma” content=“no-cache”>
- <meta ht