Struts 2框架提供了内置支持处理文件上传使用基于HTML表单的文件上传。上传一个文件时,它通常会被存储在一个临时目录中,他们应该由Action类进行处理或移动到一个永久的目录,以确保数据不丢失。
1. upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="/struts-tags" prefix="s"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML > <html> <head> <title>uploadDemo</title> </head> <body> <s:form action="/myUpload.action" enctype="multipart/form-data"> <s:file name="myfile"></s:file> <s:submit value="submit"></s:submit> </s:form> </body> </html>
2 uploadResult.jsp
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML > <html> <head> </head> <body> Upload success!! </body> </html>
3 UploadAction.java
package com.xinping.action; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import com.opensymphony.xwork2.ActionSupport; public class UploadAction extends ActionSupport { private File myfile; // 对应真正上传的文件 private String myfileFileName; // 对应上传的文件名 public File getMyfile() { return myfile; } public void setMyfile(File myfile) { this.myfile = myfile; } public String getMyfileFileName() { return myfileFileName; } public void setMyfileFileName(String myfileFileName) { this.myfileFileName = myfileFileName; } @Override public String execute() throws Exception { InputStream is = new FileInputStream(myfile); OutputStream os = new FileOutputStream("E://temp3//" + myfileFileName); byte[] buffer = new byte[1024]; int length = 0; while (-1 != (length = is.read(buffer))) { os.write(buffer, 0, length); } os.close(); is.close(); return SUCCESS; } }
4 struts.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 所有匹配*.action的请求都由struts2处理 --> <constant name="struts.action.extension" value="action" /> <!-- 是否启用开发模式 --> <constant name="struts.devMode" value="true" /> <!-- struts配置文件改动后,是否重新加载 --> <constant name="struts.configuration.xml.reload" value="true" /> <!-- 设置浏览器是否缓存静态内容 --> <constant name="struts.serve.static.browserCache" value="false" /> <!-- 请求参数的编码方式 --> <constant name="struts.i18n.encoding" value="utf-8" /> <!-- 每次HTTP请求系统都重新加载资源文件,有助于开发 --> <constant name="struts.i18n.reload" value="true" /> <!-- 文件上传最大值 --> <constant name="struts.multipart.maxSize" value="104857600" /> <!-- 让struts2支持动态方法调用 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" /> <!-- Action名称中是否还是用斜线 --> <constant name="struts.enable.SlashesInActionNames" value="false" /> <!-- 允许标签中使用表达式语法 --> <constant name="struts.tag.altSyntax" value="true" /> <package name="struts" extends="struts-default"> <action name="myUpload" class="com.xinping.action.UploadAction"> <result name="success">/uploadResult.jsp</result> </action> </package> </struts>
参考资料:
http://www.yiibai.com/struts_2/struts_file_uploads.html