struts1文件上传

Struts1.2实现单文件上传

jsp:
<% page language="java" pageEncoding="GBK" %>
<% taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>  
<% taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
 
< html >  
    
< head >
        
< title > JSP for UpfileForm form </ title >
    
</ head >
    
< body >
        
< html:form  action ="/upfile"  enctype ="multipart/form-data" >
            file 
< html:file  property ="file"   />< html:errors  property ="file" />< br />
            
< html:submit  value ="确定" />
        
</ html:form >
    
</ body >
</ html >

注意表单类型必须为:enctype="multipart/form-data".
Struts-Config.xml:
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd" >

< struts-config >
  
< data-sources  />
  
< form-beans  >
    
< form-bean  name ="upfileForm"  type ="com.yourcompany.struts.form.UpfileForm"   />

  
</ form-beans >

  
< global-exceptions  />
  
< global-forwards  />
  
< action-mappings  >
    
< action
      
attribute ="upfileForm"
      input
="/upfile.jsp"
      name
="upfileForm"
      path
="/upfile"
      scope
="request"
      type
="com.yourcompany.struts.action.UpfileAction"   />

  
</ action-mappings >

  
< message-resources  parameter ="com.yourcompany.struts.ApplicationResources"   />
</ struts-config >


web.xml:
<? xml version="1.0" encoding="UTF-8" ?>
< web-app  xmlns ="http://java.sun.com/xml/ns/j2ee"  xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"  version ="2.4"  xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
  
< servlet >
    
< servlet-name > action </ servlet-name >
    
< servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
    
< init-param >
      
< param-name > config </ param-name >
      
< param-value > /WEB-INF/struts-config.xml </ param-value >
    
</ init-param >
    
< init-param >
      
< param-name > debug </ param-name >
      
< param-value > 3 </ param-value >
    
</ init-param >
    
< init-param >
      
< param-name > detail </ param-name >
      
< param-value > 3 </ param-value >
    
</ init-param >
    
< load-on-startup > 0 </ load-on-startup >
  
</ servlet >
  
< servlet-mapping >
    
< servlet-name > action </ servlet-name >
    
< url-pattern > *.do </ url-pattern >
  
</ servlet-mapping >
  
< welcome-file-list >
    
< welcome-file > index.jsp </ welcome-file >
  
</ welcome-file-list >
</ web-app >

UpfileForm.java:

package  com.yourcompany.struts.form;

import  javax.servlet.http.HttpServletRequest;
import  org.apache.struts.action.ActionErrors;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionMapping;
import  org.apache.struts.upload.FormFile;


public   class  UpfileForm  extends  ActionForm  {
    


    

    
private FormFile file;


    


    

    
public ActionErrors validate(ActionMapping mapping,
            HttpServletRequest request) 
{
        
// TODO Auto-generated method stub
        return null;
    }


    

    
public void reset(ActionMapping mapping, HttpServletRequest request) {
        
// TODO Auto-generated method stub
    }

    
public FormFile getFile() {
        
return file;
    }


    
public void setFile(FormFile file) {
        
this.file = file;
    }


    
}

UpfileAction.java:

package  com.yourcompany.struts.action;

import  java.io.FileNotFoundException;
import  java.io.FileOutputStream;
import  java.io.IOException;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;
import  org.apache.struts.action.Action;
import  org.apache.struts.action.ActionForm;
import  org.apache.struts.action.ActionForward;
import  org.apache.struts.action.ActionMapping;
import  org.apache.struts.upload.FormFile;

import  com.yourcompany.struts.form.UpfileForm;


public   class  UpfileAction  extends  Action  {
    


    

    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
        UpfileForm upfileForm 
= (UpfileForm) form;
        FormFile file 
= upfileForm.getFile();
        FileOutputStream fileOutput;
        
        
try {
            fileOutput 
= new FileOutputStream("d://" + file.getFileName());
            fileOutput.write(file.getFileData());
            fileOutput.flush();
            fileOutput.close();
        }
 catch (FileNotFoundException e) {
            e.printStackTrace();
        }
 catch (IOException e) {
            e.printStackTrace();
        }

        
        

        
return null;
    }

}
当然对于Form我们也可以用动态的:
LazyValidatorForm uploadForm  =  (LazyValidatorForm) form;
然后FormFile可以直接 通过form得到:
FormFile formFile  =  uploadForm.get( " file " );

相应的在web.xml中的配置:
< form - bean name = " uploadForm "  type = " org.apache.struts.validator.LazyValidatorForm "   />

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值