0
点赞
收藏
分享

微信扫一扫

Java_JavaEE_轻量_Struts2文件上传闲谈;


背景:上传文件,form表单method为post,enctype为multipart/form-data,这样浏览器才会把用户选择文件的二进制数据发送给服务器(采用二进制流方式处理数据);Struts2的文件上传还没有来得及使用Servlet3.0API,因此Struts2的文件上传还需要依赖于Common-FileUpload、COS等文件上传组件;

说明:Struts2并未提供自己的请求解析器,Struts2不会自己去处理multipart/form-data请求,它需要调用其他上传框架来解析二进制请求数据;再者就是添加文件上传组件JAR包(commons-io-1.3.2.jar、commons-fileupload-1.2.1.jar);

1、上传页面

<span style="font-size:14px;"><s:form action="uploadPro" enctype="multipart/form-data">
	<s:textfield name="title" label="文件标题"/><br />
	<s:file name="upload" label="选择文件"/><br />
	<s:submit value="上传"/>
</s:form></span>



2、Action实现

<span style="font-size:14px;">package org.crazyit.app.action;

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

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

/**
 * UploadAction_class
 * Description: 采用fileUpload拦截器过滤文件;
 * Date: 2014-11-28 16:52
 * @author cyb_23
 * @version  1.0
 */ 
public class UploadAction extends ActionSupport
{
	//  文件标题
	private String title;
	// 文件域
	private File upload;
	// 文件类型
	private String uploadContentType;
	// 文件名
	private String uploadFileName;
	// 保存路径
	private String savePath;
	public void setSavePath(String value)
	{
		this.savePath = value;
	}
	private String getSavePath() throws Exception 
	{
		return ServletActionContext.getServletContext().getRealPath(savePath);
	}
	public void setTitle(String title) 
	{
		this.title = title; 
	}
	public String getTitle()
	{
		return (this.title); 
	} 
	public void setUpload(File upload) 
	{
		this.upload = upload; 
	}
	public File getUpload() 
	{
		return (this.upload); 
	} 
	public void setUploadContentType(String uploadContentType) 
	{
		this.uploadContentType = uploadContentType; 
	}
	public String getUploadContentType()
	{
		return (this.uploadContentType); 
	} 
	public void setUploadFileName(String uploadFileName) 
	{
		this.uploadFileName = uploadFileName; 
	}
	public String getUploadFileName() 
	{
		return (this.uploadFileName); 
	}
	
	@Override
	public String execute() throws Exception
	{	// 文件输入流
		FileInputStream fis = new FileInputStream(getUpload());
		// 文件输出流
		FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
		// 边读边写
		byte[] buffer = new byte[1024];
		int len = 0;
		while ((len = fis.read(buffer)) > 0)<span style="white-space:pre">	</span>//从此输入流中将最多 buffer.length 个字节的数据读入一个 byte 数组中;
		{
			fos.write(buffer , 0 , len);<span style="white-space:pre">	//将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此文件输出流;</span>
		}
		fos.close();
		return SUCCESS;
	}
}</span>



3、Action配置

<span style="font-size:14px;"><struts>
	<constant name="struts.custom.i18n.resources" value="mess"/>
	<constant name="struts.i18n.encoding" value="GBK"/>

	<package name="lee" extends="struts-default">
		<action name="uploadPro" class="org.crazyit.app.action.UploadAction">
			<!-- 配置fileUpload的拦截器 -->
			<interceptor-ref name="fileUpload">
				<param name="allowedTypes">image/png,image/gif,image/jpeg</param>
				<param name="maximumSize">20000</param> 
			</interceptor-ref> 
			<!-- 配置系统默认的拦截器 -->
			<interceptor-ref name="defaultStack"/>
			<!-- 动态设置Action的属性值 -->
			<param name="savePath">/uploadFiles</param>

			<result>/WEB-INF/content/succ.jsp</result>
			<result name="input">/WEB-INF/content/upload.jsp</result>
		</action>
		<action name="*">
			<result>/WEB-INF/content/{1}.jsp</result>	
		</action>
	</package>
</struts></span>



4、资源文件_错误提示

#文件类型不允许_提示信息
struts.messages.error.content.type.not.allowed=您上传的文件类型只能是图片文件!请重新选择!
#上传文件太大_提示信息
struts.messages.error.file.too.large=您要上传的文件太大,请重新选择!

5、成功页面_显示图片

<span style="font-size:14px;"><body>
	上传成功!<br/>
	文件标题:<s:property value="title"/><br/>
	文件为:<img src="<s:property value="'uploadFiles/' + uploadFileName"/>"/><br/>
</body></span>



特别说明:如果能帮助到您,请您留下点滴痕迹,让我知道我的存在是有意义的;如果不能帮助到您,请接受我的歉意;(示例下载)





举报

相关推荐

0 条评论