使用FileUpload上傳文件

Struts1中對使用fileUpload進行文件上傳進行了封裝,通過FormFile接口進行處理使得操作變得更加簡單,

首先看一下流程圖:

下面將對每一部分進行講解

輸入界面:upload.jsp

注意:表單method 使用postenctype使用multipart/form-data

      上傳控件使用<html:file/>

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>

<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

 

<html>

    <head>

       <title>JSP for UploadForm form</title>

    </head>

    <body>

       <html:form action="/upload" method="post" enctype="multipart/form-data">

           theFile : <html:file property="theFile"/><html:errors property="theFile"/><br/>

           <html:submit/><html:cancel/>

       </html:form>

    </body>

</html>

ActionForm:com.qiudawei115.struts.form.UploadForm

注意這裏使用的是FormFile

 

package com.qiudawei115.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: 06-11-2007

 *

 * XDoclet definition:

 * @struts.form name="uploadForm"

 */

public class UploadForm extends ActionForm {

       /*

        * Generated fields

        */

 

       /** theFile property */

       private FormFile theFile;

 

       /*

        * 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

       }

 

       /**

        * Returns the theFile.

        * @return CommonsMultipartRequestHandler$CommonsFormFile

        */

       public FormFile getTheFile() {

              return theFile;

       }

 

       /**

        * Set the theFile.

        * @param theFile The theFile to set

        */

       public void setTheFile(

                     FormFile theFile) {

              this.theFile = theFile;

       }

}

Action com.qiudawei115.struts.action.UploadAction:處理上傳文件

工作:獲取上傳文件封裝FormFile,獲取上牀文件信息並保存在session

package com.qiudawei115.struts.action;

 

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

 

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.qiudawei115.struts.form.UploadForm;

 

/**

 * MyEclipse Struts

 * Creation date: 06-11-2007

 *

 * XDoclet definition:

 * @struts.action path="/upload" name="uploadForm" input="/upload.jsp" scope="request" validate="true"

 */

public class UploadAction 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) {

              UploadForm uploadForm = (UploadForm) form;// TODO Auto-generated method stub

              /**

               * 獲取上傳文件的信息

               * 封裝成爲FormFile格式

               */

              FormFile theFile=uploadForm.getTheFile();

              String contentType=theFile.getContentType();

              String fileName=theFile.getFileName();

              int fileSize=theFile.getFileSize();

              byte[] fileData=null;

              try {

                     fileData=theFile.getFileData();

              } catch (FileNotFoundException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              } catch (IOException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

             

              System.out.println("contentType:"+contentType);

              System.out.println("fileName:"+fileName);

              System.out.println("fileSize:"+fileSize);

              FileOutputStream out=null;

              try {

                     out=new FileOutputStream(new File("c://"+fileName));                   

              } catch (FileNotFoundException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

              try {

                     out.write(fileData);

                     out.close();

              } catch (IOException e) {

                     // TODO Auto-generated catch block

                     e.printStackTrace();

              }

             

              HttpSession session=request.getSession();

      

              session.setAttribute("contentType", contentType);

              session.setAttribute("fileName", fileName);

              session.setAttribute("fileSize", Integer.valueOf(fileSize));

              session.setAttribute("location", "c://"+fileName);

             

              return mapping.findForward("display");

       }

}

MyJsp.jsp通過session顯示上傳文件的信息

<%@ page language="java" pageEncoding="ISO-8859-1"%>

 

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>

<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>

<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

 

 

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html:html lang="true">

  <head>

    <html:base />

   

    <title>MyJsp.jsp</title>

 

    <meta http-equiv="pragma" content="no-cache">

    <meta http-equiv="cache-control" content="no-cache">

    <meta http-equiv="expires" content="0">   

    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

    <meta http-equiv="description" content="This is my page">

    <!--

    <link rel="stylesheet" type="text/css" href="styles.css">

    -->

 

  </head>

 

  <body>

    This a struts page. <br>

    <%session=request.getSession(); %>

    contentType:<%=session.getAttribute("contentType") %><br>

    FileName:<%=session.getAttribute("fileName") %><br>

    FileSize:<%=session.getAttribute("fileSize") %><br>

    Location:<%=session.getAttribute("location") %>

  </body>

</html:html>

這個只是一個簡單的例子,可根據需要在進行改進,有需要源代碼的可和我聯繫.

[email protected]

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章