struts2实现文件上传

来源:互联网 发布:python 内存管理 编辑:程序博客网 时间:2024/06/05 20:58

除了导入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>





原创粉丝点击