<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    我的漫漫程序之旅

    專注于JavaWeb開(kāi)發(fā)
    隨筆 - 39, 文章 - 310, 評(píng)論 - 411, 引用 - 0
    數(shù)據(jù)加載中……

    Struts1.2實(shí)現(xiàn)單文件上傳

    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:
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     
    */

    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;

    /** 
     * MyEclipse Struts
     * Creation date: 09-28-2007
     * 
     * XDoclet definition:
     * @struts.form name="upfileForm"
     
    */

    public class UpfileForm extends ActionForm {
        
    /*
         * Generated fields
         
    */


        
    /** file property */
        
    private FormFile file;


        
    /*
         * Generated Methods
         
    */


        
    /** 
         * Method validate
         * 
    @param mapping
         * 
    @param request
         * 
    @return ActionErrors
         
    */

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


        
    /** 
         * Method reset
         * 
    @param mapping
         * 
    @param request
         
    */

        
    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:
    /*
     * Generated by MyEclipse Struts
     * Template path: templates/java/JavaClass.vtl
     
    */

    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;

    /** 
     * MyEclipse Struts
     * Creation date: 09-28-2007
     * 
     * XDoclet definition:
     * @struts.action path="/upfile" name="upfileForm" input="/upfile.jsp" scope="request" validate="true"
     
    */

    public class UpfileAction extends Action {
        
    /*
         * Generated Methods
         
    */


        
    /** 
         * Method execute
         * 
    @param mapping
         * 
    @param form
         * 
    @param request
         * 
    @param response
         * 
    @return ActionForward
         
    */

        
    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;
        }

    }
    當(dāng)然對(duì)于Form我們也可以用動(dòng)態(tài)的:
    LazyValidatorForm uploadForm = (LazyValidatorForm) form;
    然后FormFile可以直接 通過(guò)form得到:
    FormFile formFile = uploadForm.get("file");

    相應(yīng)的在web.xml中的配置:
    <form-bean name="uploadForm" type="org.apache.struts.validator.LazyValidatorForm" />

    源碼下載


    posted on 2008-01-31 18:11 々上善若水々 閱讀(12080) 評(píng)論(3)  編輯  收藏 所屬分類: Struts1.x

    評(píng)論

    # re: Struts1.2實(shí)現(xiàn)單文件上傳  回復(fù)  更多評(píng)論   

    方法太牛比了。mail:yaoyao19851023@163.com
    2008-11-19 13:07 | yaoyao

    # re: Struts1.2實(shí)現(xiàn)單文件上傳  回復(fù)  更多評(píng)論   

    不靠譜,用接口里的方法如何得到文件流?
    FormFile file = upfileForm.getFile();
    FileOutputStream fileOutput;

    try {
    fileOutput = new FileOutputStream("d://" + file.getFileName());
    fileOutput.write(file.getFileData());
    2011-05-03 10:16 | Stan

    # re: Struts1.2實(shí)現(xiàn)單文件上傳[未登錄](méi)  回復(fù)  更多評(píng)論   

    文件過(guò)大怎么么處理呀?謝啦
    2012-05-12 19:24 | 陳晨
    主站蜘蛛池模板: 亚洲av成人综合网| 真人无码作爱免费视频| 免费国产污网站在线观看不要卡| 在线免费观看伊人三级电影| 成视频年人黄网站免费视频| 亚洲欧洲精品成人久久奇米网 | 亚洲视频在线观看免费| 亚洲熟女www一区二区三区| 久久久精品视频免费观看 | 亚洲男人第一av网站| 亚洲国产AV一区二区三区四区| 中文无码成人免费视频在线观看| 无码一区二区三区AV免费| 久久精品国产精品亚洲下载| 亚洲午夜一区二区电影院| 久青草视频97国内免费影视| 成人免费在线观看网站| 亚洲AV成人精品网站在线播放| 久久久亚洲精华液精华液精华液| 99爱在线精品视频免费观看9| 内射无码专区久久亚洲| 亚洲另类古典武侠| 国产午夜无码片免费| 国产麻豆免费观看91| 亚洲第一成年人网站| 精品一区二区三区免费观看 | 色偷偷亚洲女人天堂观看欧| 国色精品va在线观看免费视频| 美女被免费视频网站a国产| 亚洲国产精品不卡在线电影| 一个人看的www免费高清| 两个人的视频高清在线观看免费 | 无码日韩人妻AV一区免费l| 无码日韩人妻av一区免费| 亚洲AV本道一区二区三区四区| 免费人妻精品一区二区三区| 成年女人男人免费视频播放| 亚洲精品永久www忘忧草| 成人网站免费看黄A站视频| 亚洲AV无码专区日韩| 亚洲欧美aⅴ在线资源|