1、实现struts2的文件上传,首先把表单form的enctype属性设置为:multipart/form-data
<form action="FileUpload" method="post" enctype="multipart/form-data"> 文件标题:<input name="title" type="text" /><br> 文件:<input name="upload" type="file"/><br> <input type="submit" value="上传" /> </form>
action类为:
public class FileUploadAction { private String title; private File upload; private String uploadFileName; private String uploadContentType; private String savePath; (getter/setter) public String execute() throws Exception { System.out.println("path:" + getSavePath()); System.out.println("filename:" + getUploadFileName()); String path = ServletActionContext.getServletContext().getRealPath(getSavePath());// 相对路径 try { FileOutputStream fos = new FileOutputStream(path + "\\" + getUploadFileName()); byte[] buffer = new byte[1024]; int len; FileInputStream fis = new FileInputStream(getUpload()); while ((len = fis.read(buffer)) > 0) { fos.write(buffer, 0, len); } } catch (Exception e) { e.printStackTrace(); } return "success"; } }这里要注意的是,如果struts配置文件中savePath配置的是相对路径的话,则需要通过
String path = ServletActionContext.getServletContext().getRealPath(getSavePath());来获取相对路径,如果是绝对路径的话,可直接使用
FileOutputStream fos = new FileOutputStream(<span style="font-family: Arial, Helvetica, sans-serif;">getSavePath()</span> + "\\" + getUploadFileName());
3、添加上传成功后转向的页面
上传成功! <br> 文件标题: <s:property value="title" /> <br /> 文件为: <img src="<s:property value="'upload/'+uploadFileName"/>" /> <br />
4、最后是配置action
<action name="FileUpload" class="com.demo.action.FileUploadAction"> <!-- 动态设置action属性值 --> <param name="savePath">/upload</param> <result name="success">/demo/jsp/uploadSuccess.jsp</result> </action>这里配置的是相对路径,需要在项目WebRoot下建立upload文件夹
5、最后在web.xml里配置一个Filter,ActionContextCleanUp,作用是方便struts2和SiteMesh整合,其实本来和文件上传没有什么关系,但发现有时候会出现一些未知的异常,但加了这个Filter后就一切正常了
<filter> <filter-name>struts-cleanup</filter-name> <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> </filter> <filter-mapping> <filter-name>struts-cleanup</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
但启动是后台出现了错误
***************************************************************************
* WARNING!!! *
* *
* >>> ActionContextCleanUp <<< is deprecated! Please use the new filters! *
* *
* This can be a source of unpredictable problems! *
* *
* Please refer to the docs for more details! *
* http://struts.apache.org/2.x/docs/webxml.html *
* *
*************************************************************************
由于在struts升级时这个配置有漏洞,所以用以下配置代替
<filter> <filter-name>struts-prepare</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter</filter-class> </filter> <filter-mapping> <filter-name>struts-prepare</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>struts-execute</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts-execute</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
<!-- 设置应用使用的解码集 --> <constant name="struts.i18n.encoding" value="UTF-8"/>即可,完美解决
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/kevinxxw/article/details/46988337