Struts2中实现文件上传(附源码)

Struts2使用开源项目Apache Jakarta Commons FileUpload和内建的FileUploadInterceptor拦截器实现文件上传.


实现原理 :

Struts 2是通过Commons FileUpload文件上传。Commons FileUpload通过将HTTP的数据保存到临时文件夹,然后Struts使用fileUpload拦截器将文件绑定到Action的实例中。从而我们就能够以本地文件方式的操作浏览器上传的文件。

具体实现

前段时间Apache发布了Struts 2.1GA,所以本文的实现是以该版本的Struts作为框架的。以下是例子所依赖类包的列表:
依赖类包的列表
其中commons-io-1.1.jar和commons-fileupload-1.1.3.jar可以在
http://jakarta.apache.org/
上下载.

★ 文件上传页面 index.jsp:

<% @ page language="java" pageEncoding="GB18030" %>
<% @taglib prefix="s" uri="/struts-tags"  %>
< html >
  
< head >
    
< title > Upload Page </ title >
  
</ head >
  
< body >
  
< s:form  action ="upload"  method ="post"  enctype ="multipart/form-data" >
      
< s:file  name ="doc"  label ="File" />
      
< s:submit  value ="upload" />
  
</ s:form >
  
</ body >
</ html >


先将表单的提交方式设为POST,然后将enctype设为multipart/form-data,这并没有什么特别之处.
★ 处理文件上传 FileUploadAction.java:

package  com;

import  java.io.File;
import  java.text.SimpleDateFormat;
import  java.util.Date;
import  java.util.Random;

import  org.apache.commons.io.FileUtils;
import  org.apache.struts2.ServletActionContext;

import  com.opensymphony.xwork2.ActionSupport;

public   class  FileUploadAction  extends  ActionSupport
{
    
private static final long serialVersionUID = 4658947796066228597L;
    
private File doc;
    
private String fileName;
    
private String contentType;

    
public void setDoc(File file)
    
{
        
this.doc = file;
    }


    
public void setDocFileName(String fileName)
    
{
        
this.fileName = fileName;
    }


    
public void setDocContentType(String contentType)
    
{
        
this.contentType = contentType;
    }


    @Override
    
public String execute() throws Exception
    
{
        System.out.println(
"fileName:" + fileName);
        System.out.println(
"contextType" + contentType);
        
// 得到当前web工程下的/files目录的在本机的绝对路径,如果没有这个文件夹则会创建
        String targetDirectory = ServletActionContext.getServletContext()
                .getRealPath(
"/files");
        
//重命名上传文件
        String targetFileName = generateFileName(fileName);
        
//在指定目录创建文件
        File target = new File(targetDirectory, targetFileName);
        
//把要上传的文件copy过去
        FileUtils.copyFile(doc, target);
        
return SUCCESS;
    }

    
//重命名上传文件(非必须)
    public String generateFileName(String fileName)
    
{
        String formatDate 
= new SimpleDateFormat("yyMMddHHmmss")
                .format(
new Date());
        
int random = new Random().nextInt(10000);
        
int position = fileName.lastIndexOf(".");
        String extension 
= fileName.substring(position);

        
return formatDate + random + extension;
    }


    
public String getFileName()
    
{
        
return fileName;
    }


}


在fileupload.jsp中,只有doc一个字段,而FileUploadAction.java中,却有三个字段,Struts2怎么通过页面的一个字段设置Action里的三个字段呢?没错,这就是FileUploadInterceptor的功劳了!你所要做的只是按照一定的样式命名这三个字段的set方法,而字段名可以任意命名。第一个File类型的字段的set方法还是以常规的方式命名,另两个String类型的字段的set方法必须分别以“File字段的set方法名+FileName”和“File字段的set方法名+ContentType”来命名。

★ 配置文件 struts.xml :
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"
>
< struts >
    
< package  name ="com"  extends ="struts-default" >
        
< action  name ="upload"  class ="com.FileUploadAction" >
            
< result  name ="success" > /success.jsp </ result >
        
</ action >
    
</ package >
</ struts >     
★ 配置文件 web.xml:
<? xml version="1.0" encoding="UTF-8" ?>    
< web-app  version ="2.4"      
    xmlns
="http://java.sun.com/xml/ns/j2ee"      
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"      
    xsi:schemaLocation
="http://java.sun.com/xml/ns/j2ee     
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
>    
        
    
< 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.FilterDispatcher </ 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 >    

OK!源码可以我的微软网盘下载. 163529.html

々上善若水々 2007-11-27 19:49 发表评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值