想飞就别怕摔

大爷的并TM骂人

struts文件上传(简单实例)

struts文件上传主要的文件有:
    2个jsp文件uploadIndex.jsp和uploadSuccess.jsp文件;FileUploadAction.java;FileUploadActionForm.java;struts-config.xml;web.xml;
    
1、uploadIndex.jsp 注意:form中method="post" ;enctype="multipart/form-data"
<%@ page language="java" pageEncoding="gb2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
<title>测试Strust文件上传</title>
  
</head>
  
<body>
      
<form action="fileupload.do" method="post" enctype="multipart/form-data" >
          
<table>
              标题:
<input type="text" name="title"/>
              
<input type="file" name="myfile"/>
              
<input type="submit" value="上传"/>
          
</table>
    
</form>    
  
</body>
</html>
2、uploadSuccess.jsp
 1 <%@ page language="java" pageEncoding="gb2312"%>
 2 <%@ page isELIgnored="false" %>
 3 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 4 <html>
 5   <head>
 6     <title>uploadSuccess.jsp</title>
 7   </head>
 8   <body>
 9     文件标题:${fileupload.title }<br>
10     文件名:${fileupload.myfile.fileName }<br>
11     文件大小:${fileupload.myfile.fileSize}<br>
12   </body>
13 </html>
14 
3、FileUploadActionForm.java
 1 package com.eplugger.struts.form;
 2 import org.apache.struts.action.ActionForm;
 3 import org.apache.struts.upload.FormFile;
 4 public class FileUploadActionForm extends ActionForm {
 5     private String title;
 6     private FormFile myfile;  //注意:上传文件的类型必须是FormFile;它是struts提供的;
 7     public String getTitle() {
 8         return title;
 9     }
10     public void setTitle(String title) {
11         this.title = title;
12     }
13     public FormFile getMyfile() {
14         return myfile;
15     }
16     public void setMyfile(FormFile myfile) {
17         this.myfile = myfile;
18     }
19 }
20 
4、FileUploadAction.java
package com.eplugger.struts.action;

import java.io.FileOutputStream;
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.eplugger.struts.form.FileUploadActionForm;

public class FileUploadAction extends Action {
    @Override
    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            
throws Exception {
        
//获得form
        FileUploadActionForm fpaf =(FileUploadActionForm)form;
        
//获得通过form传来的title值;
        String title = fpaf.getTitle();
        
//获得通过form传来的文件;注意类型必须是FormFile;
        FormFile myFile = fpaf.getMyfile();
        
if(myFile != null ){
            
//创建文件输出的路径
            FileOutputStream fos= new  FileOutputStream("e:\\"+myFile.getFileName());
            
//输出(一个bayt[]数组)
            fos.write(myFile.getFileData());
            
//把内存中的文件变成物理的
            fos.flush();
            
//关闭
            fos.close();
        }
        request.setAttribute(
"title","title");
        
return mapping.findForward("success");
    }
}
5、struts-config.xml
<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE struts-config PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 1.3//EN"
        "http://struts.apache.org/dtds/struts-config_1_3.dtd"
>
<struts-config>
    
<form-beans>
        
<form-bean name="fileupload" type="com.eplugger.struts.form.FileUploadActionForm"/>
    
</form-beans>
    
<action-mappings>
        
<action path="/fileupload"
                type
="com.eplugger.struts.action.FileUploadAction"
                name
="fileupload"
                scope
="request"
        
>
        
<forward name="success" path="/uploadSuccess.jsp"/>
        
</action>
    
</action-mappings>
    <controller maxFileSize="10M"/>

</struts-config>
6.web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns
="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation
="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
  
<welcome-file-list>
    
<welcome-file>index.jsp</welcome-file>
  
</welcome-file-list>
  
  
<servlet>
        
<servlet-name>action</servlet-name>
        
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
          
<!-- Default -->
        
<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>

    
<servlet-mapping>
        
<servlet-name>action</servlet-name>
        
<url-pattern>*.do</url-pattern>
    
</servlet-mapping>
</web-app>

最后说一下,关于文件的大小等参数都可以再struts-config.xml文件中的<controller  />中配置,可以配置的属性可以参考struts-core-1.3.10.jar中的org.apache.struts.resources.struts-config_1_3.dtd
主要内容如下:

<!-- The "controller" element describes the ControllerConfig bean
     [org.apache.struts.config.ControllerConfig] that encapsulates
     a module's runtime configuration. The following
     attributes are defined:

     bufferSize      The size of the input buffer used when processing
                     file uploads.
                     [4096]

     catalog         Name of the catalog to use when processing requests
                     for this module.
                     [struts]

     className       Fully qualified Java class name of the
                     ControllerConfig subclass for this controller object.
                     If specified, the object must be a subclass of the
                     default class.
                     ["org.apache.struts.config.ControllerConfig"]

     command         Name of the command to execute to process a request.
                     [servlet-standard]

     contentType     Default content type (and optional character encoding) to
                     be set on each response. May be overridden by the Action,
                     JSP, or other resource to which the request is forwarded.
                     ["text/html"]

     forwardPattern  Replacement pattern defining how the "path" attribute of a
                     <forward> element is mapped to a context-relative URL when
                     it starts with a slash (and when the contextRelative
                     property is false). This value may consist of any
                     combination of the following:
                     - "$M" - Replaced by the module prefix of this module
                     - "$P" - Replaced by the "path" attribute of the  selected
                     "forward" element
                     - "$$" - Causes a literal dollar sign to be rendered
                     - "$x" - (Where "x" is any character not defined above)
                     Silently swallowed, reserved for future use
                     If not specified, the default forwardPattern is "$M$P",
                     which is consistent with the previous behavior of
                     forwards.  Since Struts 1.1.  ["$M$P"]

     inputForward    Set to "true" if you want the "input" attribute of
                     <action> elements to be the name of a local or global
                     ActionForward, which will then be used to calculate the
                     ultimate URL. Set to "false" (the default) to treat the
                     "input" parameter of <action> elements as a
                     module-relative path to the resource
                     to be used as the input form. Since Struts 1.1.
                     [false]

     locale          Set to "true" if you want a Locale object stored in the
                     user's session if not already present.
                     [true]

     maxFileSize     The maximum size (in bytes) of a file to be accepted as a
                     file upload.  Can be expressed as a number followed by a
                     "K", "M", or "G", which are interpreted to mean kilobytes,
                     megabytes, or gigabytes, respectively.
                     ["250M"]

     memFileSize     The maximum size (in bytes) of a file whose contents will
                     be retained in memory after uploading. Files larger than
                     this threshold will be written to some alternative storage
                     medium, typically a hard disk. Can be expressed as a number
                     followed by a "K", "M", or "G", which are interpreted to
                     mean kilobytes, megabytes, or gigabytes, respectively.
                     ["256K"]

     multipartClass  The fully qualified Java class name of the multipart
                     request handler class to be used with this module.
                     ["org.apache.struts.upload.CommonsMultipartRequestHandler"]

     nocache         Set to "true" if you want the controller to add HTTP
                     headers for defeating caching to every response from
                     this module.  [false]

     pagePattern     Replacement pattern defining how the "page" attribute of
                     custom tags using it is mapped to a context-relative URL
                     of the corresponding resource.  This value may consist of
                     any combination of the following:
                     - "$M" - Replaced by the module prefix of this module
                     - "$P" - Replaced by the value of the "page" attribute
                     - "$$" - Causes a literal dollar sign to be rendered
                     - "$x" - (Where "x" is any character not defined above)
                              Silently swallowed, reserved for future use
                     If not specified, the default forwardPattern is
                     "$M$P", which is consistent with previous hard coded
                     behavior of URL evaluation for "page" attributes.
                     ["$M$P"]

     processorClass  The fully qualified Java class name of the
                     RequestProcessor subclass to be used with this module.
                     ["org.apache.struts.chain.ComposableRequestProcessor"]

     tempDir         Temporary working directory to use when processing
                     file uploads.
                     [{Directory provided by servlet container}]

-->

posted on 2009-06-10 21:57 生命的绽放 阅读(551) 评论(0)  编辑  收藏 所属分类: Struts1.x


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年6月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜