速動畫教程第二十二集 使用Struts上傳文件

?

制作環境:

??? Eclipse3.1.1? 、 MyEclipse4.1? 、 Tomcat5.5.x

?

步驟:

??? 新建工程 upload

?

??? 添加 Struts 框架,使用 Struts1.2 版本

?

??? 新建 一個 jsp + action 這里將使用動態的 ActionForm

?

??? 在新建表單對像時使用一個文件名和一個文件對像進行提交

?

??? 修改動態 From 的類型為 org.apache.struts.upload.FormFile

?

??? < form-bean name = "upfileForm" type = "org.apache.struts.action.DynaActionForm" >

????? < form-property name = "filename" type = "java.lang.String" />

????? < form-property name = "filedata" type = "java.lang.String" />

??? </ form-bean >

?

??? 改為

?

??? < form-bean name = "upfileForm" type = "org.apache.struts.action.DynaActionForm" >

????? < form-property name = "filename" type = "java.lang.String" />

????? < form-property name = "filedata" type = " org.apache.struts.upload.FormFile " />

??? </ form-bean >

?

??? 修改 upfile.jsp 文件,在<Form> 中加入 enctype = "multipart/form-data" ,這樣才可以提交二進制類型的文件

?

??? 修改文件第一行代碼

?????? <%@ page language = "java" %>

?????? 改為

??? <%@ page contentType = "text/html;charset=UTF-8" language = "java" %>

?????? 這里將使用 UTF-8 的編碼格式

?

??? 修改 upfileAction.java 文件,修改后的內容如下:

?

package com.test.struts.action;

?

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

?

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.action.DynaActionForm;

import org.apache.struts.upload.FormFile;

?

/**

?* MyEclipse Struts

?* Creation date: 07-05-2006

?*

?* XDoclet definition:

?* @struts.action path="/upfile" name="upfileForm" input="/upfile.jsp" scope="request" validate="true"

?*/

public class UpfileAction extends Action {

?

??? // --------------------------------------------------------- Instance Variables

?

??? // --------------------------------------------------------- Methods

?

??? /**

??? ?* Method execute

??? ?* @param mapping

??? ?* @param form

??? ?* @param request

??? ?* @param response

??? ?* @return ActionForward

??? ?*/

??? public ActionForward execute(

?????? ActionMapping mapping,

?????? ActionForm form,

?????? HttpServletRequest request,

?????? HttpServletResponse response) {

?????? DynaActionForm upfileForm = (DynaActionForm) form;

?????? // 聲明并獲取對像

?????????????????? String filename = upfileForm.getString("filename");

??????? // 輸出文件名

?????????????????? System.out.println(filename);

??????? FormFile filedata = (FormFile) upfileForm.get("filedata");

??????? // 取當前系統路徑

?????????????????? String filePath = request.getRealPath("/");

??????? try {

??????????? // 轉換文件為數據流

??????????????????????????? InputStream stream = filedata.getInputStream();

??????????? // 建立輸出流

??????????????????????????? OutputStream bos = new FileOutputStream(filePath + "/" +

??????????????????? filedata.getFileName());

??????????? // 將文件寫入網站根目錄下

??????????????????????????? int bytesRead = 0;

??? ??????? byte[] buffer = new byte[8192];

??? ??????? while ( (bytesRead = stream.read(buffer, 0, 8192)) != -1) {

??? ??????? bos.write(buffer, 0, bytesRead);

??? ??????? }

??? ??????? bos.close();

??? ??????? stream.close();

??????? } catch (FileNotFoundException e) {

??????????? // TODO Auto-generated catch block

??????????? e.printStackTrace();

??????? } catch (IOException e) {

??????????? // TODO Auto-generated catch block

??????????? e.printStackTrace();

??????? }

??????? // 返回到提交頁面

?????????????????? return mapping.getInputForward();

??? }

?

}

?

??? 現在可以進行測試了

?

??? 這時將會發現,提交的文件及文件名稱都是亂碼!下面將解決亂碼

?

??? 增加一個過濾器,過濾器的代碼請查看包中的具體文件

?

??? web.xml 文件中加入以下配置內容,過濾器的編碼設置為 UTF-8

?????? < filter >

?????? < filter-name > Set Character Encoding </ filter-name >

?????? < filter-class > com.test.SetCharacterEncodingFilter </ filter-class >

?????? < init-param >

?????????? < param-name > encoding </ param-name >

?????????? < param-value > UTF-8 </ param-value >

?????? </ init-param >

??? </ filter >

??? < filter-mapping >

?????? < filter-name > Set Character Encoding </ filter-name >

?????? < url-pattern > /* </ url-pattern >

??? </ filter-mapping >

??? < filter-mapping >

?????? < filter-name > Set Character Encoding </ filter-name >

?????? < servlet-name > action </ servlet-name >

??? </ filter-mapping >

?

??? 配置 Tomcat 的 server.xml 文件,文件在 Tomcat_Home/conf 中

?

??? 在端口配置的前面加入 URIEncoding="UTF-8" 如果使用了和IIS集成的話需要在 8009 的端口前也加入此配置內容,關于詳細的和IIS集成方法和亂碼解決請查看第二十一集的錄像

?

??? 現在重新啟動服務,測試

?

??? 一切正常!

?

??? 本集教程到些結束!!!