【SSH三大框架】Struts2基础第五篇:文件上传

来源:互联网 发布:电气图纸绘制软件 编辑:程序博客网 时间:2024/05/02 04:28

首先,我们建立一个web项目,然后导入:commons-fileupload、commons-io这两个jar包,当然还有其它的struts2需要的jar包

其次,我们还是先配置一下struts2的拦截器:web.xml

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="3.0"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
  7.   <display-name></display-name>   
  8.   <welcome-file-list>  
  9.     <welcome-file>index.jsp</welcome-file>  
  10.   </welcome-file-list>  
  11.     
  12.   <filter>  
  13.         <filter-name>struts2</filter-name>  
  14.         <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  15.     </filter>  
  16.     <filter-mapping>  
  17.         <filter-name>struts2</filter-name>  
  18.         <url-pattern>/*</url-pattern>  
  19.     </filter-mapping>  
  20. </web-app>  

然后,我们写一个前台界面,作为上传文件:file.jsp

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>  
  2. <%  
  3. String path = request.getContextPath();  
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";  
  5. %>  
  6.   
  7. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  8. <html>  
  9.   <head>  
  10.     <title>文件上传</title>  
  11.   </head>  
  12.     
  13.   <body>  
  14.     <form action="upload.action" enctype="multipart/form-data" method="post">  
  15.         文件:<input type="file" name="image">  
  16.         <input type="submit" value="上传">  
  17.     </form>  
  18.   </body>  
  19. </html>  
可以看到,我们在上边的form表单中定义的属性:

action:这是要提交到的action目录

enctype=“multipart/form-data":是设置表单的MIME编码。默认情况,编码格式是:application/x-www-form-urlencoded,不能够用于文件上传;当设置为:multipart/form-data,才能够完成的传递文件数据,进行下面的操作。


接下来,我们需要在struts.xml中配置一个action:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="GBK"?>  
  2. <!DOCTYPE struts PUBLIC  
  3.     "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"  
  4.     "http://struts.apache.org/dtds/struts-2.1.7.dtd">  
  5. <!-- 指定Struts 2配置文件的根元素 -->  
  6. <struts>  
  7.     <constant name="struts.action.extension" value="do,action"/>  
  8.     <constant name="struts.multipart.maxSize" value="10701096" />  
  9.     <package name="employee" extends="struts-default">  
  10.         <action name="upload" class="cn.fileupload.FileUploadAction">   
  11.             <result name="success">/message.jsp</result>  
  12.         </action>  
  13.     </package>  
  14. </struts>  
这里的<constant name="struts.multipart.maxSize" value="10701096" />是设置文件最大上传的容量大小,这里设置的是10M


我们还需要建立一个处理请求的逻辑类:

[html] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. package cn.fileupload;  
  2.   
  3. import java.io.File;  
  4.   
  5. import org.apache.commons.io.FileUtils;  
  6. import org.apache.struts2.ServletActionContext;  
  7.   
  8. import com.opensymphony.xwork2.ActionContext;  
  9. import com.opensymphony.xwork2.ActionSupport;  
  10.   
  11. public class FileUploadAction extends ActionSupport{  
  12.     private File image;  
  13.     //获得文件的文件名,格式为:nameFileName,这里格式中的name为jsp页面中的name属性  
  14.     private String imageFileName;  
  15.       
  16.     public String getImageFileName() {  
  17.         return imageFileName;  
  18.     }  
  19.     public void setImageFileName(String imageFileName) {  
  20.         this.imageFileName = imageFileName;  
  21.     }  
  22.     public File getImage() {  
  23.         return image;  
  24.     }  
  25.     public void setImage(File image) {  
  26.         this.image = image;  
  27.     }  
  28.     public String addUI(){  
  29.           
  30.         return "success";  
  31.     }  
  32.     public String execute() throws Exception{  
  33.         //获得要存放图片的绝对路径  
  34.         String realpath = ServletActionContext.getServletContext().getRealPath("/images");  
  35.         System.out.println(realpath);  
  36.         //在路径下创建上传的文件  
  37.         File savefile = new File(new File(realpath),imageFileName);  
  38.         if(image!=null){  
  39.             if(!savefile.getParentFile().exists()){  
  40.                 //如果路径不存在,则创建一个  
  41.                 savefile.getParentFile().mkdirs();  
  42.                 //把上传的文件保存在路径中  
  43.                 FileUtils.copyFile(image, savefile);  
  44.                 ActionContext.getContext().put("message", "上传成功");  
  45.             }  
  46.         }  
  47.           
  48.         return "success";  
  49.     }  
  50. }  

如果文件上传成功,我们就可以在项目根目录下的/image目录下找到上传的文件
0 0
原创粉丝点击