使用struts上传文件

    新建工程 upload<o:p></o:p>

<o:p> </o:p>添加 Struts 框架,使用 Struts1.2 版本<o:p></o:p>

<o:p></o:p>

  新建 一个 jsp + action 这里将使用动态的 ActionForm<o:p></o:p>

<o:p> </o:p> 在新建表单对像时使用一个文件名和一个文件对像进行提交<o:p></o:p>

<o:p>  </o:p>修改动态 From 的类型为 org.apache.struts.upload.FormFile<o:p></o:p>

<o:p> </o:p>  

xml 代码

 

  1. <form-bean name="upfileForm" type="org.apache.struts.action.DynaActionForm">  
  2.   
  3.     <form-property name="filename" type="java.lang.String" />  
  4.   
  5.     <form-property name="filedata" type="java.lang.String" />  
  6.   
  7.   form-bean>  

    改为  <o:p></o:p>

xml 代码

 

  1. <form-bean name="upfileForm" type="org.apache.struts.action.DynaActionForm">  
  2.   
  3.       <form-property name="filename" type="java.lang.String" />  
  4.   
  5.       <form-property name="filedata" type="org.apache.struts.upload.FormFile" />  
  6.   
  7.     form-bean>  
  8.   

 

    <o:p> </o:p>

    修改 upfile.jsp 文件,在中加入 enctype="multipart/form-data" ,这样才可以提交二进制类型的文件<o:p></o:p>

<o:p> </o:p>

 修改文件第一行代码<o:p></o:p>

  

<o:p>
xml 代码
  1. <%@ page language="java"%>  

    改为

       

   

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

      

 

    修改 upfileAction.java 文件,修改后的内容如下:<o:p></o:p>

 <o:p></o:p>

<o:p>
java 代码
  1. package com.test.struts.action;   
  2.   
  3.     
  4.   
  5. import java.io.FileNotFoundException;   
  6.   
  7. import java.io.FileOutputStream;   
  8.   
  9. import java.io.IOException;   
  10.   
  11. import java.io.InputStream;   
  12.   
  13. import java.io.OutputStream;   
  14.   
  15.     
  16.   
  17. import javax.servlet.http.HttpServletRequest;   
  18.   
  19. import javax.servlet.http.HttpServletResponse;   
  20.   
  21.     
  22.   
  23. import org.apache.struts.action.Action;   
  24.   
  25. import org.apache.struts.action.ActionForm;   
  26.   
  27. import org.apache.struts.action.ActionForward;   
  28.   
  29. import org.apache.struts.action.ActionMapping;   
  30.   
  31. import org.apache.struts.action.DynaActionForm;   
  32.   
  33. import org.apache.struts.upload.FormFile;   
  34.   
  35.     
  36.   
  37. /**   
  38.  
  39.  * MyEclipse Struts  
  40.  
  41.  * Creation date: 07-05-2006  
  42.  
  43.  *   
  44.  
  45.  * XDoclet definition:  
  46.  
  47.  * @struts.action path="/upfile" name="upfileForm" input="/upfile.jsp" scope="request" validate="true"  
  48.  
  49.  */  
  50.   
  51. public class UpfileAction extends Action {   
  52.   
  53.     
  54.   
  55.     // --------------------------------------------------------- Instance Variables   
  56.   
  57.     
  58.   
  59.     // --------------------------------------------------------- Methods   
  60.   
  61.     
  62.   
  63.     /**   
  64.  
  65.      * Method execute  
  66.  
  67.      * @param mapping  
  68.  
  69.      * @param form  
  70.  
  71.      * @param request  
  72.  
  73.      * @param response  
  74.  
  75.      * @return ActionForward  
  76.  
  77.      */  
  78.   
  79.     public ActionForward execute(   
  80.   
  81.        ActionMapping mapping,   
  82.   
  83.        ActionForm form,   
  84.   
  85.        HttpServletRequest request,   
  86.   
  87.        HttpServletResponse response) {   
  88.   
  89.        DynaActionForm upfileForm = (DynaActionForm) form;   
  90.   
  91.        // 声明并获取对像   
  92.   
  93.                    String filename = upfileForm.getString("filename");   
  94.   
  95.         // 输出文件名   
  96.   
  97.                    System.out.println(filename);   
  98.   
  99.         FormFile filedata = (FormFile) upfileForm.get("filedata");   
  100.   
  101.         // 取当前系统路径   
  102.   
  103.                    String filePath = request.getRealPath("/");    
  104.   
  105.         try {   
  106.   
  107.             // 转换文件为数据流   
  108.   
  109.                             InputStream stream = filedata.getInputStream();   
  110.   
  111.             // 建立输出流   
  112.   
  113.                             OutputStream bos = new FileOutputStream(filePath + "/" +   
  114.   
  115.                     filedata.getFileName());   
  116.   
  117.             // 将文件写入网站根目录下   
  118.   
  119.                             int bytesRead = 0;   
  120.   
  121.             byte[] buffer = new byte[8192];   
  122.   
  123.             while ( (bytesRead = stream.read(buffer, 08192)) != -1) {   
  124.   
  125.             bos.write(buffer, 0, bytesRead);   
  126.   
  127.             }   
  128.   
  129.             bos.close();   
  130.   
  131.             stream.close();   
  132.   
  133.         } catch (FileNotFoundException e) {   
  134.   
  135.             // TODO Auto-generated catch block   
  136.   
  137.             e.printStackTrace();   
  138.   
  139.         } catch (IOException e) {   
  140.   
  141.             // TODO Auto-generated catch block   
  142.   
  143.             e.printStackTrace();   
  144.   
  145.         }   
  146.   
  147.         // 返回到提交页面   
  148.   
  149.                    return mapping.getInputForward();   
  150.   
  151.     }   
  152.   
  153.     
  154.   
  155. }   
  156.   

 

<o:p></o:p>

 将SetCharacterEncodingFilter.java复制到项目中,在 web.xml 文件中加入以下配置内容,过滤器的编码设置为 UTF-8<o:p></o:p>

 

     <o:p></o:p>

xml 代码

 

  1. <filter>  
  2.   
  3.      <filter-name>Set Character Encodingfilter-name>  
  4.   
  5.      <filter-class>com.test.SetCharacterEncodingFilterfilter-class>  
  6.   
  7.      <init-param>  
  8.   
  9.          <param-name>encodingparam-name>  
  10.   
  11.          <param-value>UTF-8param-value>  
  12.   
  13.      init-param>  
  14.   
  15.   filter>  
  16.   
  17.   <filter-mapping>  
  18.   
  19.      <filter-name>Set Character Encodingfilter-name>  
  20.   
  21.      <url-pattern>/*url-pattern>  
  22.   
  23.   filter-mapping>  
  24.   
  25.   <filter-mapping>  
  26.   
  27.      <filter-name>Set Character Encodingfilter-name>  
  28.   
  29.      <servlet-name>actionservlet-name>  
  30.   
  31.   filter-mapping>  

 

    配置 Tomcat 的 server.xml 文件,文件在 Tomcat_Home/conf 中<o:p></o:p>

<o:p> </o:p>

    在端口配置的前面加入 URIEncoding="UTF-8" 如果使用了和IIS集成的话需要在 8009 的端口前也加入此配置内容。<o:p> </o:p>

    现在启动服务器,测试。<o:p></o:p>

 

</o:p></o:p>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值