struts2实现文件上传

除了导入struts2必要的jar包外,还要导入两个commons-fileupload-1.2.2.jar,另一个是commons-io-2.0.1.jar

单文件上传

前台页面

<form action="uploadfile"	enctype="multipart/form-data" method="post">
    		<input type="file" name="myfile"/>
    		<input type="submit" value="提交"/>
</form>
其中需要注意的是enctype属性必须为multipart/form-data,它的意思,是设置表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了multipart/form-data,才能完整的传递文件数据,进行下面的操作. 

创建action

public class MyFileUpload extends ActionSupport{
	private String myfileFileName;
	private File myfile;
	private String myfileContentType;
	
	public String upload() throws IOException{
		//得到上传文件保存的路径
		String savePath=ServletActionContext.getServletContext().getRealPath("/upload");
//上传后保存的文件
		File outFile=new File(savePath+"/"+myfileFileName);
//上传文件操作
		FileUtils.copyFile(myfile, outFile);
		return "success";
	}
	//省略get/set方法
}
说明:上面的FileUtils.copyFile(face, outfile);  方法,使用Common-io.jar文件中的一个工具类。如果不使用该方法,可以自己实现文件的读写:

		FileOutputStream fos = new FileOutputStream("D:/struts/"+myfileFileName);
		FileInputStream fis = new FileInputStream(myfile);
		byte[] b = new byte[1024];
		int len = 0;
		while((len = fis.read(b))!=-1){
			fos.write(b,0,len);
		}
		fos.close();
		fis.close();
配置struts.xml文件

<action name="*file" class="base.MyFileUpload" method="{1}">
				<result>/success.jsp</result>
</action>
注意:文件上传一定要完成:a、文件类型的过滤;b、文件大小的过滤。在Struts中,可以用struts自带的拦截器来完成。
整个表单的总文件的大小,不能超过struts.properties里限定的大小,如:
struts.multipart.maxSize=10971520
过滤文件的大小,类型如:
<interceptor-ref name="fileUpload">
			<param name="maximumSize">4194304</param>
<!-- 配置允许上传文件的类型,如果有多个类型用","隔开 -->  
            <param name="fileUpload.allowedTypes">application/vnd.ms-excel,text/plain</param>  
            <!--配置允许上传文件的扩展名,如果有多个用","隔开  -->  
            <param name="fileUpload.allowedExtensions">txt,excel,ppt</param> 
 
		</interceptor-ref>

多文件上传

public class MyFileUpload extends ActionSupport{
	private String[] myfileFileName;
	private File[] myfile;
	private String[] myfileContentType;
	public String upload() throws IOException{
		String savePath=ServletActionContext.getServletContext().getRealPath("/upload");
		//确保有上传文件
		if(myfile!=null&&myfile.length>0){
			for(int i=0;i<myfile.length;i++){
				File outFile=new File(savePath+"/"+myfileFileName[i]);
				if(myfile[i]!=null&&myfile[i].length()>0){
					FileUtils.copyFile(myfile[i], outFile);
				}else{
					System.out.println("上传有误");
				}
			}
		}
		return "success";
	}
	//省略getset
}
默认的错误信息
在struts2-core-2.x.x.x.jar\org\apache\struts2\struts-messages.properties文件中定义:
struts.messages.error.uploading=Error uploading: {0}
struts.messages.error.file.too.large=File too large: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=Content-Type not allowed: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=File extension not allowed: {0} "{1}" "{2}" {3}
{0}:<input type=“file” name=“uploadImage”>中name属性的值
{1}:上传文件的名称
{2}:上传文件保存到临时目录的名称
{3}:上传文件的类型(对struts.messages.error.file.too.large是上传文件的大小)
我们可以在Action的统计目录下创建一个fileuploadmessage.properties文件, 文件名没有要求, 但必须是properties文件, 在其中输入:
struts.messages.error.uploading=上传错误: {0}
struts.messages.error.file.too.large=文件太大: {0} "{1}" "{2}" {3}
struts.messages.error.content.type.not.allowed=不支持的文件类型: {0} "{1}" "{2}" {3}
struts.messages.error.file.extension.not.allowed=不支持的文件扩展名: {0} "{1}" "{2}" {3}
<constant name="struts.custom.i18n.resources" value="cn.xing.upload.fileuploadmessage">
</constant>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值