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>
以上发的是上传单个文件的例子,下一篇文章我会用例子说明若上传多个文件该如何解决?