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

来源:互联网 发布:知乎专栏可以随意开吗 编辑:程序博客网 时间:2024/05/02 00:48

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

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

<?xml version="1.0" encoding="UTF-8"?><web-app version="3.0" 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_3_0.xsd">  <display-name></display-name>  <welcome-file-list>    <welcome-file>index.jsp</welcome-file>  </welcome-file-list>    <filter><filter-name>struts2</filter-name><filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class></filter><filter-mapping><filter-name>struts2</filter-name><url-pattern>/*</url-pattern></filter-mapping></web-app>

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

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head><title>文件上传</title>  </head>    <body>    <form action="upload.action" enctype="multipart/form-data" method="post">    文件:<input type="file" name="image">    <input type="submit" value="上传">    </form>  </body></html>
可以看到,我们在上边的form表单中定义的属性:

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

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


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

<?xml version="1.0" encoding="GBK"?><!DOCTYPE struts PUBLIC"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN""http://struts.apache.org/dtds/struts-2.1.7.dtd"><!-- 指定Struts 2配置文件的根元素 --><struts><constant name="struts.action.extension" value="do,action"/><constant name="struts.multipart.maxSize" value="10701096" /><package name="employee" extends="struts-default"><action name="upload" class="cn.fileupload.FileUploadAction"><result name="success">/message.jsp</result></action></package></struts>
这里的<constant name="struts.multipart.maxSize" value="10701096" />是设置文件最大上传的容量大小,这里设置的是10M


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

package cn.fileupload;import java.io.File;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport{private File image;//获得文件的文件名,格式为:nameFileName,这里格式中的name为jsp页面中的name属性private String imageFileName;public String getImageFileName() {return imageFileName;}public void setImageFileName(String imageFileName) {this.imageFileName = imageFileName;}public File getImage() {return image;}public void setImage(File image) {this.image = image;}public String addUI(){return "success";}public String execute() throws Exception{//获得要存放图片的绝对路径String realpath = ServletActionContext.getServletContext().getRealPath("/images");System.out.println(realpath);//在路径下创建上传的文件File savefile = new File(new File(realpath),imageFileName);if(image!=null){if(!savefile.getParentFile().exists()){//如果路径不存在,则创建一个savefile.getParentFile().mkdirs();//把上传的文件保存在路径中FileUtils.copyFile(image, savefile);ActionContext.getContext().put("message", "上传成功");}}return "success";}}

如果文件上传成功,我们就可以在项目根目录下的/image目录下找到上传的文件




0 0