• struts——文件上传


    上传文件在一个系统当中是一个很常用的功能,也是一个比较重要的功能。今天我们就一起来学习一下Struts2如何上传文件。

    今天讲的上传文件的方式有三种:

    1,以字节为单位传输文件;

    2,Struts2封装的一种方式;

    3,以字符的方式传输文件。


    其实这三种方式都差不 多,都是将文件先从客户端一临时文件的形式,传输到服务器的临时文件夹下,然后在将该临时文件复制到我们要上传的目录。另外,有一个需要注意,就是上传过 程中产生的这些临时文件,Struts2不会自动清理,所以我们需要手动清理临时文件,这一个下面的代码中有提到。


    用Action来完成我们上传的核心功能:

    1. package com.action;  
    2.   
    3. import java.io.BufferedReader;  
    4. import java.io.BufferedWriter;  
    5. import java.io.File;  
    6. import java.io.FileInputStream;  
    7. import java.io.FileOutputStream;  
    8. import java.io.InputStreamReader;  
    9. import java.io.OutputStreamWriter;  
    10. import java.util.Map;  
    11.   
    12. import org.apache.struts2.ServletActionContext;  
    13.   
    14. import com.opensymphony.xwork2.ActionContext;  
    15. import com.opensymphony.xwork2.ActionSupport;  
    16.   
    17.   
    18. public class UploadAction extends ActionSupport {  
    19.   
    20.     private File upload;  
    21.     private String uploadFileName;    
    22.     private String uploadContentType;  
    23.       
    24.     private long maximumSize;  
    25.     private String allowedTypes;  
    26.       
    27.   
    28.     public File getUpload() {  
    29.         return upload;  
    30.     }  
    31.     public void setUpload(File upload) {  
    32.         this.upload = upload;  
    33.     }  
    34.     public String getUploadFileName() {  
    35.         return uploadFileName;  
    36.     }  
    37.     public void setUploadFileName(String uploadFileName) {  
    38.         this.uploadFileName = uploadFileName;  
    39.     }  
    40.   
    41.     public String getUploadContentType() {  
    42.         return uploadContentType;  
    43.     }  
    44.     public void setUploadContentType(String uploadContentType) {  
    45.         this.uploadContentType = uploadContentType;  
    46.     }  
    47.     public long getMaximumSize() {  
    48.         return maximumSize;  
    49.     }  
    50.     public void setMaximumSize(long maximumSize) {  
    51.         this.maximumSize = maximumSize;  
    52.     }  
    53.     public String getAllowedTypes() {  
    54.         return allowedTypes;  
    55.     }  
    56.     public void setAllowedTypes(String allowedTypes) {  
    57.         this.allowedTypes = allowedTypes;  
    58.     }  
    59.     @Override  
    60.     public String execute() throws Exception {  
    61.           
    62.         File uploadFile = new File(ServletActionContext.getServletContext().getRealPath("upload"));  
    63.         if(!uploadFile.exists()) {  
    64.             uploadFile.mkdir();  
    65.         }  
    66.           
    67.         //验证文件大小及格式  
    68.         if (maximumSize < upload.length()) {  
    69.             return "error";  
    70.         }  
    71.           
    72.         boolean flag =false;  
    73.         String[]  allowedTypesStr = allowedTypes.split(",");  
    74.         for (int i = 0; i < allowedTypesStr.length; i++) {  
    75.             if (uploadContentType.equals(allowedTypesStr[i])) {  
    76.                 flag = true;  
    77.             }  
    78.         }  
    79.         if (flag == false) {  
    80.             Map request = (Map) ActionContext.getContext().get("request");  
    81.             request.put("errorMassage", "文件类型不合法!");  
    82.               
    83.             System.out.println(request.toString());  
    84.             return "error";  
    85.         }  
    86.           
    87.         //第一种上传方式  
    88. //      FileInputStream input = new FileInputStream(upload);  
    89. //      FileOutputStream out = new FileOutputStream(uploadFile +"//"+ uploadFileName);  
    90. //      try {  
    91. //          byte[] b =new byte[1024];  
    92. //          int i = 0;  
    93. //          while ((i=input.read(b))>0) {  
    94. //              out.write(b, 0, i);  
    95. //          }  
    96. //      } catch (Exception e) {  
    97. //          e.printStackTrace();  
    98. //      } finally {  
    99. //          //关闭输入、输出流  
    100. //          input.close();  
    101. //          out.close();  
    102. //          //删除临时文件  
    103. //          upload.delete();  
    104. //      }  
    105.           
    106.         //第二种上传方式  
    107. //      FileUtils.copyFile(upload, new File(uploadFile+"\"+uploadFileName));  
    108. //      //删除临时文件  
    109. //      upload.delete();  
    110.           
    111.         //第三种上传方式  
    112.         BufferedReader bReader = new BufferedReader(new InputStreamReader(new FileInputStream(upload)));  
    113.         BufferedWriter bWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(uploadFile+"\"+ uploadFileName)));  
    114.         try {  
    115.             char [] c =new char[1024];  
    116.             int i = 0;  
    117.             while ((i = bReader.read(c)) > 0) {  
    118.                 bWriter.write(c, 0, i);  
    119.             }  
    120.         } catch (Exception e) {  
    121.             e.printStackTrace();  
    122.         } finally {  
    123.             bReader.close();  
    124.             bWriter.close();              
    125.             //删除临时文件  
    126.             upload.delete();  
    127.         }  
    128.           
    129.           
    130.         return "success";  
    131.     }     
    132.       
    133. }  


    在Struts.xml文件中配置我们的Action:

    1. <?xml version="1.0" encoding="UTF-8" ?>    
    2. <!DOCTYPE struts PUBLIC    
    3.     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    
    4.     "http://struts.apache.org/dtds/struts-2.0.dtd">    
    5.     
    6. <struts>    
    7.     <constant name="struts.action.extension" value=","></constant>    
    8.     <!-- 上传文件最大限制(如果为多文件上传,则为多个文件的总大小) -->    
    9.     <constant name="struts.multipart.maxSize" value="40000000"></constant>    
    10.     <!-- 存放上传文件的临时目录 -->    
    11.     <constant name="struts.multipart.saveDir" value="D:\temp"></constant>      
    12.         
    13.         
    14.     <package name="upload" namespace="/file" extends="struts-default">    
    15.         <action name="upLoad" class="com.action.UploadAction">    
    16.             <result name="success">/success.jsp</result>    
    17.             <result name="error" >/error.jsp</result>    
    18.             <result name = "input" type ="redirect">/index.jsp</result>    
    19.             <param name="maximumSize ">1000000</param>    
    20.             <param name="allowedTypes">application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,text/plain</param>    
    21.     
    22.             <!--用struts拦截器限制上传文件大小及类型    
    23.             <interceptor-ref name="fileUpload">    
    24.                 单个文件的大小    
    25.                 <param name="maximumSize ">1000000</param>    
    26.                 <param name="allowedTypes">application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,text/plain</param>    
    27.             </interceptor-ref>    
    28.             <interceptor-ref name="defaultStack"></interceptor-ref>    
    29.              -->    
    30.         </action>    
    31.             
    32.     </package>    
    33.     
    34. </struts>    



    文件上传的页面:

    1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
    2.     pageEncoding="UTF-8"%>  
    3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
    4. <html>  
    5. <head>  
    6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
    7. <title>上传文件</title>  
    8. </head>  
    9. <body>  
    10.     <!-- enctype="multipart/form-data"不对字符编码。在使用包含文件上传控件的表单时,必须使用该值。 -->  
    11.     <form action="file/upLoad" method="post" enctype="multipart/form-data">  
    12.         <input name="upload" type="file">  
    13.         <input name="btnUpload" type="submit" value="上传">  
    14.     </form>  
    15. </body>  
    16. </html>  


    上传文件之前,通常要判断一下文件的大小及类型,上面有两种方式来验证,一种是在Action里验证,一种是通过Struts2的拦截器验证。两者选其一,选择一个把另一个注释掉即可。


    另外,还有一个需要注意:上传文件页面中form的enctype属性值,一定要设置成"multipart/form-data",否则就会出错。

  • 相关阅读:
    VxWorks 6.9 内核编程指导之读书笔记 -- POSIX
    VxWorks 6.9 内核编程指导之读书笔记 -- ISRs和Watchdog Timer
    VxWorks 6.9 内核编程指导之读书笔记 -- Singnals
    VxWorks 6.9 内核编程指导之读书笔记 -- 多任务
    VxWorks 6.9 内核编程指导之读书笔记 -- 多任务(二)
    VxWorks 6.9 内核编程指导之读书笔记 -- C++开发
    VxWorks 6.9 内核编程指导之读书笔记 -- VxWorks Kernel application (二)
    VxWorks 6.9 内核编程指导之读书笔记 -- VxWorks kernel application (一)
    发布ASP(非.Net)网站
    解决方法:An error occurred on the server when processing the URL. Please contact the system administrator
  • 原文地址:https://www.cnblogs.com/zhangtan/p/5738415.html
Copyright © 2020-2023  润新知