Struts中上傳檔案需要注意的地方

iteye_9300發表於2010-03-18

今天突然想研究下Struts的上傳功能,沒想到搞了兩個小時才搞出來,卡在一個地方,總是報錯說引數型別轉化異常,結果我改動了下頁面,卻又神出鬼沒的上傳成功了,趕緊記錄下

 首先是web.xml中的配置,我個人喜歡每次弄個字元過濾器,以免出錯

 

<filter>

<filter-name>filter</filter-name>

<filter-class>com.wujintao.dao.SetCharacterEncodingFilter</filter-class>

<init-param>

<param-name>encode</param-name>

<param-value>utf-8</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>filter</filter-name>

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

</filter-mapping>

<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>2</param-value>

    </init-param>

    <init-param>

      <param-name>detail</param-name>

      <param-value>2</param-value>

    </init-param>

    <load-on-startup>2</load-on-startup>

  </servlet>

  <!-- Standard Action Servlet Mapping -->

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

 

struts-config.xml中的配置,挺奇怪的最後兩行必須得配上

 

<struts-config>

 

<!-- ================================================ Form Bean Definitions -->

    <form-beans>

     <form-bean name="uploadForm" type="com.javacrazyer.web.formbean.UploadForm"/>

    </form-beans>

 

<!-- =========================================== Action Mapping Definitions -->

    <action-mappings>

<action path="/upload" 

name="uploadForm" 

type="com.javacrazyer.web.action.UploadAction">

<forward name="succ" path="/succ.jsp"/>

<forward name="fail" path="/failure.jsp"/>

</action>

    </action-mappings>

 

    <controller bufferSize="8192" maxFileSize="1M" />

 

    <message-resources parameter="msgs"/>

</struts-config>

 

其次就是業務邏輯的程式碼了,在這之前我必須說下,這個Struts的上傳用到的關鍵點就是自己自帶的FormFile類了,因此在FormBean裡面應該配上一個FormFile型別的屬性

 

private String description; //檔案的描述

private FormFile file;  //檔案具體內容對應的例項 

 

public String getDescription() {

return description;

}

public void setDescription(String description) {

this.description = description;

}

public FormFile getFile() {

return file;

}

public void setFile(FormFile file) {

this.file = file;

}

 

業務邏輯處理程式碼

 

//具體業務流程處理方法,由Struts框架回撥

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

 

String toURL = "succ";

//取資料

UploadForm uForm = (UploadForm)form;

 

String desc = uForm.getDescription();

System.out.println("description==" + desc);

 

FormFile ff = uForm.getFile();

 

if(ff == null){

toURL = "fail";

return mapping.findForward(toURL);

}

System.out.println("檔名:" + ff.getFileName());

System.out.println("內容型別:" + ff.getContentType());

System.out.println("檔案大小:" + ff.getFileSize() + "位元組");

 

BufferedInputStream bis = new BufferedInputStream(ff.getInputStream());

 

//目標路徑

String destPath = this.getServlet().getServletContext().getRealPath("/files");

System.out.println(destPath+"===============================");

BufferedOutputStream bos = null;

 

try{

bos = new BufferedOutputStream(new FileOutputStream(destPath + "/" + ff.getFileName()));

 

//從原始檔中取資料,寫到目標檔案中

byte [] buff = new byte[8192];

 

for(int len = -1; (len = bis.read(buff)) != -1;){

bos.write(buff, 0, len);

}

 

bos.flush();

 

}catch(IOException ie){

ie.printStackTrace();

toURL = "fail";

}finally{

if(bis != null){

try{

bis.close();

}catch(IOException ie){

ie.printStackTrace();

}

}

if(bos != null){

try{

bos.close();

}catch(IOException ie){

ie.printStackTrace();

}

}

}

 

return mapping.findForward(toURL);

 

}

 

接下來我要說的,大家都猜到了肯定是JSP頁面的上傳表單了,這裡就是卡住我的罪魁禍首

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

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

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

<html>

  <head>

    <title>My JSP 'index.jsp' starting page</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>

 <html:form action="upload.do" enctype="multipart/form-data"> 

<html:text property="description"></html:text>

<html:file property="file"/> 

<html:submit/> 

</html:form> 

  </body>

</html>

 

為什麼說它是罪魁禍首呢,因為一直都認為<input type="file" />是最佳的上傳表單,誰知道我找了大半天,竟然出現在這裡的它必須是STRUTS標籤的表單元素,鬱悶!!!深刻的教訓啊。

相關文章