Struts2实现文件上传

写了一个用Struts实现文件上传的功能,拿出来晒晒。

首先导入如下的包到WEB-INF/lib目录下:

commons-fileupload-1.2.1.jar

commons-io-1.4.jar

commons-logging-1.0.4.jar

freemarker-2.3.8.jar

ognl-2.6.11.jar

struts2-core-2.0.11.jar

xwork-2.0.4.jar

 

下面是各个文件的编写和配置:

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3.     
  4.     <filter>
  5.         <filter-name>struts-clean</filter-name>
  6.         <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
  7.     </filter>
  8.     <filter>
  9.         <filter-name>struts</filter-name>
  10.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  11.     </filter>
  12.     <filter-mapping>
  13.         <filter-name>struts</filter-name>
  14.         <url-pattern>/*</url-pattern>
  15.     </filter-mapping>
  16.     <welcome-file-list>
  17.         <welcome-file>fileupload.jsp</welcome-file>
  18.     </welcome-file-list>
  19. </web-app>

struts2提供了ActionContextCleanUp类,在struts2的架构中,标准的过滤器一般以ActionContextCleanUp开始,后面跟着其他需要的过滤器,最后,由FilterDispatcher来处理请求,FilterDispatcher通常是将请求传递给ActionMapper,ActionContextCleanUp的一个重要作用是通知FilterDispatcher在正确的时间清除ActionContext中的请求数据,所以正确的排序如下:
     (1)ActionContextCleanUp过滤器
     (2)其他过滤器
     (3)FilterDispatcher过滤器

 

FileUpLoadAction.java

  1. package com.bin.strust2.dome01;
  2. import java.io.BufferedInputStream;
  3. import java.io.BufferedOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.FileOutputStream;
  7. import java.io.InputStream;
  8. import java.io.OutputStream;
  9. import java.util.Date;
  10. import com.opensymphony.xwork2.ActionSupport;
  11. import org.apache.struts2.ServletActionContext;
  12. public class FileUpLoadAction extends ActionSupport
  13. {
  14.    // private static final long serialVersionUID = 572146812454l;
  15.     private static final int BUFFER_SIZE = 16 * 1024;
  16.     private File myFile;
  17.     private String contentType;
  18.     private String fileName;
  19.     private String imageFileName;
  20.     private String caption;
  21.     public void setMyFileContentType(String contentType)
  22.     {
  23.         this.contentType = contentType;
  24.     }
  25.     public void setMyFileFileName(String fileName)
  26.     {
  27.         this.fileName = fileName;
  28.     }
  29.     public void setMyFile(File myFile)
  30.     {
  31.         this.myFile = myFile;
  32.     }
  33.     public String getImageFileName()
  34.     {
  35.         return imageFileName;
  36.     }
  37.     public String getCaption()
  38.     {
  39.         return caption;
  40.     }
  41.     public void setCaption(String caption)
  42.     {
  43.         this.caption = caption;
  44.     }
  45.     private static void copy(File src, File dst)
  46.     {
  47.         try
  48.         {
  49.             InputStream in = null;
  50.             OutputStream out = null;
  51.             try
  52.             {
  53.                 in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
  54.                 out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
  55.                 byte[] buffer = new byte[BUFFER_SIZE];
  56.                 while (in.read(buffer) > 0)
  57.                 {
  58.                     out.write(buffer);
  59.                 }
  60.             } finally
  61.             {
  62.                 if (null != in)
  63.                 {
  64.                     in.close();
  65.                 }
  66.                 if (null != out)
  67.                 {
  68.                     out.close();
  69.                 }
  70.             }
  71.         } catch (Exception e)
  72.         {
  73.             e.printStackTrace();
  74.         }
  75.     }
  76.     private static String getExtention(String fileName)
  77.     {
  78.         int pos = fileName.lastIndexOf(".");
  79.         return fileName.substring(pos);
  80.     }
  81.     @Override
  82.     public String execute()
  83.     {
  84.         imageFileName = new Date().getTime() + getExtention(fileName);
  85.         File imageFile = new File(ServletActionContext.getServletContext().getRealPath("/UploadImages") + "/" + imageFileName);
  86.         copy(myFile, imageFile);
  87.         return SUCCESS;
  88.     }
  89. }

用上传时间来命名上传的文件名。

 

fileupload.jsp

  1. <%@page contentType="text/html" pageEncoding="gbk"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3.    "http://www.w3.org/TR/html4/loose.dtd">
  4.    <%@taglib prefix="s" uri="/struts-tags" %>
  5. <html>
  6.     <head>
  7.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8.         <title>JSP Page</title>
  9.     </head>
  10.     <body>
  11.         <s:form action="fileupload" method="post" enctype="multipart/form-data">
  12.             <s:file name="myFile" label="上传文件"></s:file>
  13.             <s:textfield name="caption" label="显示名字"></s:textfield>
  14.             <s:submit value="上传"></s:submit>
  15.        </s:form>
  16.     </body>
  17. </html>

显示上传文件的页面,虽然可以上传的不只是图片一种,但如果上传的是图片我们就把他显示出来。

showfile.jsp

  1. <%@page contentType="text/html" pageEncoding="gbk"%>
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  3. "http://www.w3.org/TR/html4/loose.dtd">
  4. <%@taglib  prefix="s" uri="/struts-tags" %>
  5. <html>
  6.     <head>
  7.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  8.         <title>JSP Page</title>
  9.     </head>
  10.     <body>
  11.         <div style ="padding: 3px; border: solid 1px #cccccc; text-align: center" >
  12.             <img src ='UploadImages/<s:property value ="imageFileName" /> ' />
  13.             <br />
  14.             <s:property value ="caption" />
  15.         </div >
  16.     </body>
  17. </html>

最后还要配置struts.xml

  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE struts PUBLIC
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
  4.     "http://struts.apache.org/dtds/struts-2.0.dtd">
  5. <struts>
  6.    <package name="fileupload" extends="struts-default">
  7.         <action name="fileupload" class="com.bin.strust2.dome01.FileUpLoadAction">
  8.             <interceptor-ref name="fileUploadStack"></interceptor-ref>
  9.             <result name="success">/showfile.jsp</result>
  10.         </action>
  11.     </package>
  12. </struts>

到这里就可以实现文件的上传了,(*^__^*) 嘻嘻……。

 

但细心的朋友可以发现,在服务器打印出的信息中有这样一句话:

2008-11-30 14:20:45 org.apache.struts2.dispatcher.Dispatcher getSaveDir
信息: Unable to find 'struts.multipart.saveDir' property setting. Defaulting to java.servlet.context.tempdir

这个是因为我们没有给程序指定缓存的地址,我们在src目录下加入文件struts.properties,内容如下

  1. struts.multipart.saveDir = /tmp

但现在又有一个问题,就是我们给上传文件设置显示的名字如果是中文的话会出现乱码,如果不是用Struts2框架,我们是要自己写一个转换编码的方法或类的,但Strust2为我们提供了乱码解决方案,我们只要调用他就好了,做法如下:

在struts.xml中的struts节点下加入<constant name="struts.i18n.encoding" value="GBK"></constant>,即可解决乱码问题。


 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值