当前位置: 移动技术网 > IT编程>开发语言>Java > Struts2实现文件上传功能

Struts2实现文件上传功能

2019年07月19日  | 移动技术网IT编程  | 我要评论

servlet 3.0规范的httpservletrequest已经提供了方法来处理文件上传但这种上传需要在servlet中完成。而struts2则提供了更简单的封装。

struts2默认使用的是jakarta的common-fileupload的文件上传框架,因此使用struts2的文件上传功能,则需要添加两个jar包,即commons-io-2.2.jar和commons-fileupload-1.3.1.jar。 

struts2简单文件上传示例:

1.文件上传页面

  为了能上传文件,表单的method必须设置为post,并且enctype设置为multipart/form-data。一旦设置了enctype为multipart/form-data,此时浏览器将会采用二进制流的方式来处理表单数据。

<%@ taglib prefix="s" uri="/struts-tags" %>
<%--
 created by intellij idea.
 user: administrator
 date: 2018/1/16
 time: 14:06
 to change this template use file | settings | file templates.
--%>
<%@ page contenttype="text/html;charset=utf-8" language="java" %>
<html>
<head>
 <title>struts2 简单文件上传</title>
</head>
<body>
 <s:form action="file_upload" method="post" enctype="multipart/form-data">
 <s:file name="upload" label="选择文件"/>
 <s:submit value="上传"/>
 </s:form>
</body>
</html>

2.处理上传请求的action

/**
 * description:struts2简单文件上传
 * author: eleven
 * date: 2018/1/24 10:39
 */
public class fileaction extends actionsupport{

 //上传文件
 private file upload;
 //上传文件类型
 private string uploadcontenttype;
 //上传文件名
 private string uploadfilename;

 //文件上传允许的类型在struts.xml中使用param标签动态设置了
 private string allowtypes;

 public string page(){
 return "page";
 }

 public void upload() {

 //文件上传:
 //1.读取文件内容
 //2.将文件内容写到指定文件
 try{
  system.out.println("文件上传允许的类型="+allowtypes);
  string realpath = servletactioncontext.getservletcontext().getrealpath("/upload");
  system.out.println("项目的绝对路径="+realpath);
  //创建文件保存目录
  new file(realpath).mkdir();
  file file = new file(realpath+"/"+uploadfilename);
  //文件不存在则创建
  if(!file.exists()){
  file.createnewfile();
  }
  fileoutputstream out = new fileoutputstream(file);
  fileinputstream in = new fileinputstream(upload);
  byte[] buffer = new byte[1024];
  int len = 0;
  //边读边写 每次读取1kb 写1kb
  while((len = in.read(buffer))>0){
  out.write(buffer,0,len);
  }
  system.out.println("文件上传成功...");
 }catch(exception e){
  e.printstacktrace();
 }

 }

 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 getallowtypes() {
 return allowtypes;
 }

 public void setallowtypes(string allowtypes) {
 this.allowtypes = allowtypes;
 }
}

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

  类型为file的xxx成员变量封装了该文件域对应的文件内容。

  类型为string的xxxfilename成员变量封装了该文件域对应的文件的文件名。

  类型为string的xxxcontenttype成员变量封装了该文件域对应的文件的文件类型。

3.配置struts.xml

<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public
 "-//apache software foundation//dtd struts configuration 2.3//en"
 "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
 <constant name="struts.enable.dynamicmethodinvocation" value="false" />
 <constant name="struts.devmode" value="true" />

 <package name="default" namespace="/" extends="struts-default">

 <!--文件上传-->
 <action name="file_*" class="eleven.action.fileaction" method="{1}">
  <result name="page">/web-inf/jsp/fileupload.jsp</result>
  <!--动态设置action的属性,这里举例设置了允许文件上传的类型,但是action程序中并未做过多的处理-->
  <param name="allowtypes">image/png,image/gif,image/jpeg</param>
 </action>

 </package>

</struts>

拦截器实现文件过滤  

  struts2提供了一个文件上传的拦截器,fileupload,为了让该拦截器起作用,要在action中配置拦截器引用。

  配置fileupload拦截器时,可以为其指定两个参数:

    allowtypes:允许上传的文件类型,多个文件类型之间用英文逗号,隔开

    maximumsize:允许上传的文件大小,单位是字节。

  当文件过滤失败后,系统自动转入input逻辑视图,因此必须为该action配置名为input的逻辑视图。除此之外,还必须显示地为该action配置defaultstack的拦截器引用。

struts.xml配置文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<!doctype struts public
 "-//apache software foundation//dtd struts configuration 2.3//en"
 "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
 <constant name="struts.enable.dynamicmethodinvocation" value="false" />
 <constant name="struts.devmode" value="true" />

 <package name="default" namespace="/" extends="struts-default">

 <!--文件上传-->
 <action name="file_*" class="eleven.action.fileaction" method="{1}">
  <!--配置fileupload拦截器 且配置在defaultstack拦截器栈之前-->
  <interceptor-ref name="fileupload">
  <!--允许上传的文件类型-->
  <param name="allowedtypes">image/png,image/gif,image/jpeg</param>
  <!--允许上传文件大小-->
  <param name="maximumsize">2000</param>
  </interceptor-ref>
  <!--配置系统默认拦截器-->
  <interceptor-ref name="defaultstack"/>
  <!--配置input视图页面-->
  <result name="input">/web-inf/jsp/input.jsp</result>

  <result name="page">/web-inf/jsp/fileupload.jsp</result>
 </action>

 </package>

</struts>

  上面配置的文件上传的拦截器,要求文件上传的类型只能是图片文件,并且文件大小不能大于2000字节,如果上传文件太大,或者类型不符合,则将跳转到input逻辑视图。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网