【Struts2框架】第八节上传-利用struts2实现上传功能

Struts2的文件上传

将表单的method属性设置为post,将enctype设置为multipart/form-data。
将enctype设置为multippart/form-data,浏览器将采用二进制流方式处理表单数据。
Struts2的文件上传默认使用jakarta的commons-fileupload文件上传框架,
依赖commons-fileupload-1.2.1.jar、commons-io-1.3.2.jar

①.# struts.multipart.parser=cos
②.# struts.multipart.parser=pell
③.struts.multipart.parser=jakarta

实现文件上传的Action
需求:输入标题,浏览文件,上传
若表单中文件域的name属性为xxx,则Action需要三个属性来封装该文件域信息
①.类型为File的xxx属性封装了该文件域对应的文件内容
②.类型为String的xxxFileName属性封装了该文件域对应的文件名
③.类型为String的xxxContentType属性封装了该文件域对应的文件类型

编写处理上传请求的Action类
①.private String title;
②.private File upload;
③.private String uploadContentType;
④.private String uploadFileName;
⑤.private String savePath;

Action属性的值可以通过Struts2配置文件注入,即在配置文件中为属性指定值
①.ServletActionConext.getServletContext().getRealPath(savePath);
execute: FileUtils.copy(uploadImage,new File(folder,uploadImageFileName));

配置文件上传的Action
为Action指定<param name="savaPath">/images</param>
<img src="<s:property value="'images/'+ uploadFileName"/>"/>

用java.util.UUID工具类生产唯一的文件名


上传实例:

struts.xml:

<?xml version="1.0" encoding="GBK" ?>
<!--指定struts2配置文件的DTD信息-->
<!DOCTYPE struts PUBLIC 
"-//apache Software Foundation//DTD Struts Configuation 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<!-- struts 是struts2配置文件的根元素-->

<struts>
<constant name="struts.devMode" value="true"></constant>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<!--允许静态方法的执行-->
<constant name="struts.ognl.allowStaticMethodAccess" value="true"></constant>

<package name="test" namespace="/" extends="struts-default">  
 	<action name="uploadPro" class="cn.edu.hpu.action.UploadAction">
 		<!-- 动态设置action的属性值 -->
 		<param name="savePath">/upload</param>
 		<!-- 配置Struts 2默认的视图页面 -->
 		<result>/content/succ.jsp</result>
 	</action>
 	<action name="*">
 		<result>/content/{1}.jsp</result>
 	</action>
</package> 

</struts>

UploadAction.java:
package cn.edu.hpu.action;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

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

public class UploadAction extends ActionSupport {
	//封装文件标题请求的参数
	private String title;
	//封装上传文件域的属性
	private File upload;
	//封装上传文件类型的属性
	private String uploadContenttype;
	//封装上传文件名的属性
	private String uploadFileName;
	//直接在struts.xml中配置的属性
	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() throws Exception{
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	
	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);
		}
		return SUCCESS;
	}

}

upload.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>upload</title>
  </head>
  
  <body>
     <s:form action="uploadPro" enctype="multipart/form-data">
     	<s:textfield name="title" label="文件标题"/><br/>
     	<s:file name="upload" label="选择文件"/><br/>
     	<s:submit value="上传"></s:submit>
     </s:form>
  </body>
</html>

succ.jsp:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    <title>upload success</title>
  </head>
  
  <body>
     	上传成功!<br/>
     	文件标题:<s:property value=" + title"/><br/>
     	文件为:<img src="<s:property value="'upload/'+uploadFileName"/>"/><br/>
  </body>
</html>

上述代码实现了一个基本的上传功能


注意:

在struts.xml中配置action,然后注入初始值param,,里面有允许上传的类型。

在action类中,用get、set方法得到允许上传的类型(一般是个字符串数组),之后与上传文件的类型比较,如果有一样的,放行,如果没有,说明上传类型非法,将错误信息放在FieldError里面,类似:addFieldError("upload","上传的类型不正确!");。在Jsp文件里以<s:fielderror/>显示错误提示


转载请注明出处:http://blog.csdn.net/acmman/article/details/47251619

你可能感兴趣的:(upload,struts2.0,Struts上传)