struts2文件上传

一:Struts2上传说明


      Struts2的本身没有集成文件上传框架,但它提供了对第三方插件的封装,已经让开发人员很方便的调用各种框架。
所以需要加入第三方文件上传框架,常用的有Common-FileUpload和COS。其中Common-FileUpload更为方便,也是Struts2的默认框架。Common-FileUpload可以在http://jakarta.apache.org/commons/io/上下载,下载后把lib/common-fileupload-1.2.jar放入web应用程序的WEB-INF/lib文件夹下即可。myEclipse8.x中的Struts2中的jar包已经集成了该插件。

 

 

二:配置文件


web.xml文件:


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
 xmlns="http://java.sun.com/xml/ns/javaee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  <display-name>Struts 2 Fileupload</display-name>

   <filter>
        <filter-name>struts-cleanup</filter-name>
        <filter-class>
            org.apache.struts2.dispatcher.ActionContextCleanUp
        </filter-class>
   </filter>
  <filter>
   <filter-name>struts2</filter-name>
   <filter-class>
    org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
   </filter-class>
  </filter>
  <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
  <filter-mapping>
   <filter-name>struts2</filter-name>
   <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

 

struts.xml文件不用修改。

 

 

三.视图层


页面上主要需要把form标签的enctype属性指定为multipart/form-data。
下面代码以jsp为例:


<s:form name="addEnterpriseForm" action="saveEnterprise" method="post" enctype="multipart/form-data">
    <s:file name="logo" label="商家LOGO" />
    <s:submit value="提交"/>
</s:form>

 

四.Java代码


import java.io.File;

private File logo; //商家LOGO
private String logoFileName; //商家LOGO图片名(不包含文件路径)

String extentionFileName = logoFileName.substring(logoFileName.lastIndexOf(".")); //获取文件的扩展名

String newFileName = new Date().getTime() + extentionFileName; //为上传的图片重新命名,新名字为当前的日期时间,扩展名不变

 

//为文件指定存储路径,文件存储在WEB应用程序的指定的文件夹下
File newFile = new File(ServletActionContext.getServletContext().getRealPath(“文件夹名”) + "/" + newFileName);

 

copy(uploadFile, newFile); //文件上传

 

/**
  * 文件上传
  * @param src 源文件
  * @param dst 重命名后的文件
  */
 private void copy(File src, File dst) {
        try {
           InputStream in = null ; //用来从 CDR编组流中读取 IDL类型的Java API
           OutputStream out = null ; //用来将IDL类型写入CDR编组流的Java API
            try {               
               in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
               out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
               byte [] buffer = new byte [BUFFER_SIZE];
               //读取转换
               while (in.read(buffer) > 0 ) {
                  out.write(buffer);
               }
            } finally {
                if( null != in) {
                    in.close();
                }
                if( null != out) {
                    out.close();
                }
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
}

 

 

五.大文件上传
 Struts2默认仅支持上传2M以下的文件,我们可以通过修改struts.properties文件中变量来改变文件上传的大小上限。
 在struts.properties文件中加入如下语句即可:
 struts.multipart.maxSize=1000000000
 struts.properties文件为Struts2的全局变量配置文件,在myEclipse中放到与struts.xml同一目录即可。
 Web应用中可以放到WebRoot\WEB-INF\classes文件夹下即可,不同特别配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值