struts2中上傳文件其實很容易,只需要做好如下配置:
1.在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar兩個包。(如果當前項目在之前配置好了struts就不需要重複添加了)
2.在form表單中enctype設置爲:"multipart/form-data",如下:<form action="dofile/fileac" enctype="multipart/form-data" method="post">
<%@ 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 'index.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>
<form action="dofile/fileac" enctype="multipart/form-data" method="post">
<input type="file" name="newfile">
<input type="submit">
</form>
</body>
</html>
3.建立FileAction類 聲明屬性(我設計的該類在com.action包內)
需要說明的是你定義的這些屬性的名字,newfile必須和表單中的name的值一樣。其他屬性的名字前綴必須也是表單中name的值後面加上FileName獲得它的文件名稱。如果名字不一致會報錯。
package com.action;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
public class FileAction {
private File newfile;
private String newfileFileName;
public File getNewfile() {
return newfile;
}
public void setNewfile(File newfile) {
this.newfile = newfile;
}
public String getNewfileFileName() {
return newfileFileName;
}
public void setNewfileFileName(String newfileFileName) {
this.newfileFileName = newfileFileName;
}
public String execute() {
//有時你jsp頁面的編碼格式爲utf-8,並且在request也設置了編碼格式,可是保存之後的文件名還是亂碼
//那麼就採用下面這個方法,國際編碼格式,一定可以包子utf-8格式
// try {
//
// newfFileName= new String(newfFileName.getBytes("iso8859-1"),"utf-8");
//
// } catch (UnsupportedEncodingException e1) {
// // TODO Auto-generated catch block
// e1.printStackTrace();
// }
//獲取當前項目存儲文件的文件夾名稱,我這裏定義的名字是Images
String path = ServletActionContext.getServletContext().getRealPath(
"/Images");
File f = new File(path + "/" + newfileFileName);
try {
FileUtils.copyFile(newfile, f);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "success";
}
}
4.自然,表單提交數據後要經過中間層,就要讀取struts.xml(struts的配置之前文章提到過)
其中<constant name="srtuts.multipart.maxSize" value="104857600"></constant>是配置上傳文件大小的設置,可以根據具體需求概述數值
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
<constant name="srtuts.multipart.maxSize" value="104857600"></constant>
<package name="file" extends="struts-default" namespace="/dofile">
<action name="fileac" class="com.action.FileAction">
<result name="success">/welcome.jsp</result>
</action>
</package>
</struts>
以上發的是上傳單個文件的例子,下一篇文章我會用例子說明若上傳多個文件該如何解決?