struts2上传图片

在WEB-INF下新建一个content目录,建立一个upload.jsp

 1 <%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
 2 <%@taglib prefix="s" uri="/struts-tags"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 4     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 5 <html xmlns="http://www.w3.org/1999/xhtml">
 6 <head>
 7     <title>简单的文件上传</title>
 8 </head>
 9 <body>
10 <s:form action="uploadPro" method="post"
11     enctype="multipart/form-data">
12     <s:textfield name="title" label="文件标题"/><br />
13     <s:file name="upload" label="选择文件"/><br />
14     <s:submit value="上传"/>
15 </s:form>
16 </body>
17 </html>

然后UploadAction.java如下

package action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

import java.io.File;
import java.io.*;

public class UploadAction extends ActionSupport{
	private String title;
	private File upload;
	private String uploadContentType;
	private String uploadFileName;
	private String savePath;
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public File getUpload() {
		return upload;
	}
	public void setUpload(File upload) {
		this.upload = upload;
	}
	public String getUploadContentType() {
		return uploadContentType;
	}
	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}
	public String getUploadFileName() {
		return uploadFileName;
	}
	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}
	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	@Override
	public String execute() throws Exception{
		FileOutputStream fos = new FileOutputStream(getSavePath()+"//"+getUploadFileName());
		FileInputStream fis = new FileInputStream(getUpload());
		byte[] buffer = new byte[1024];
		int len = 0;
		while((len = fis.read(buffer)) > 0){
			fos.write(buffer, 0 , len);
		}
		fis.close();
		fos.close();
		return SUCCESS;
	}
}

 在struts2中,如果表单中包含一个name属性为xxx的文件域,则对应action需要使用三个属性来封装该文件域的信息。

1.类型为File的xxx属性封装了该文件域对应的文件内容。

2.类型为String的xxxFileName属性封装了该文件域对应的文件的文件名。

3.类型为String的xxxContentType属性封装了该文件域对应的文件的文件类型。

上面例子struts2直接将文件域包含的上传文件名和文件类型的信息封装到uploadFileName和uploadContentType属性中。

下面是sruts.xml配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
	"http://struts.apache.org/dtds/struts-2.1.7.dtd">	
<struts>
	<constant name="struts.custom.i18n.resources" value="mess"/>
	<!-- 设置该应用使用的解码集 -->
	<constant name="struts.i18n.encoding" value="GBK"/>
	<package name="lee" extends="struts-default">
		<!-- 配置处理文件上传的Action -->
		<action name="uploadPro" class="action.UploadAction">
			<!-- 动态设置Action的属性值 -->
			<param name="savePath">/upload</param>
			<!-- 配置Struts 2默认的视图页面 -->
			<result>/WEB-INF/content/succ.jsp</result>	
		</action>
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>	
		</action>
	</package>
</struts>

 然后是web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
	xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <filter>
		<filter-name>struts2</filter-name>
		<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<!-- 让Struts 2的核心Filter拦截所有请求 -->
	<filter-mapping>
		<filter-name>struts2</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
</web-app>

succ.jsp:

<%@ page contentType="text/html; charset=UTF-8" language="java" errorPage="" %>
<%@taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>上传成功</title>
</head>
<body>
	上传成功!<br/>
	文件标题:<s:property value=" + title"/><br/>
	文件为:<img src="<s:property value="'upload/' 
		+ uploadFileName"/>"/><br/>
</body>
</html>

 如图

 

posted on 2014-10-21 17:10  颓废的悠然  阅读(167)  评论(0编辑  收藏  举报

导航