struts2

来源:互联网 发布:卫星遥感数据招标公告 编辑:程序博客网 时间:2024/06/05 08:33

开发环境 JDK1.8 eclipse struts2-2.3.31
1.创建web项目
2.导入struts2核心jar包
3.更改web.xml文件(只要配置好struts2的Filter就好了)
4.创建src/struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"    "http://struts.apache.org/dtds/struts-2.0.dtd"><struts>    <constant name="struts.devMode" value="true" /><!-- struts2 调试模式 -->    <package name="/" namespace="/" extends="struts-default">     <action name="fileUpload" class="com.ifan.action.FileUploadAction">              <result name="success">/success.jsp</result>        </action>    </package></struts>

5.创建src/com.ifan.action.FileUpload.java

package com.ifan.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;public class FileUploadAction extends ActionSupport {    //被上传的文件    //Struts2会对该文件进行操作,当使用fileUpload.getName() 得到的是一个临时的文件    private File fileUpload;    //文件的形式     变量的命名方式是   fileContentType    private String fileUploadContentType;    //文件的名字     变量的命名方式是   fileFilename    private String fileUploadFileName;    public String getFileUploadContentType() {        return fileUploadContentType;    }    public void setFileUploadContentType(String fileUploadContentType) {        this.fileUploadContentType = fileUploadContentType;    }    public String getFileUploadFileName() {        return fileUploadFileName;    }    public void setFileUploadFileName(String fileUploadFileName) {        this.fileUploadFileName = fileUploadFileName;    }    public File getFileUpload() {        return fileUpload;    }    public void setFileUpload(File fileUpload) {        this.fileUpload = fileUpload;    }    public String execute() throws Exception {        //创建文件的存储位置        String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/upload");          System.out.println("path = "+path);        //File.separator   =  与系统有关的默认名称分隔符,为了方便,它被表示为一个字符串。此字符串只包含一个字符,即 separatorChar。        //创建文件的存储路径        String filename = path+File.separator+fileUploadFileName;         //输出文件的类型        System.out.println("fileUploadContentType =  " + fileUploadContentType);        //输出文件的名字        System.out.println("fileUploadFileName =  " + fileUploadFileName);        //输出文件的存储路径        System.out.println("filename = "+filename);        //创建输入输出流,进行文件的读写操作        FileInputStream in = new FileInputStream(fileUpload);          FileOutputStream out = new FileOutputStream(filename);          byte[]b = new byte[1024];          int len = 0;          while((len=in.read(b))>0){              out.write(b,0,len);          }          out.close();         in.close();        return SUCCESS;    }}

6.创建WebContent/index.jsp 作为上传文件的页面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ taglib prefix="s" uri="/struts-tags" %><%    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><base href="<%=basePath%>"><title>My JSP 'hello.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--    <link rel="stylesheet" type="text/css" href="styles.css">    --></head><body><!-- Struts2的文件上传标签 --><s:form action="fileUpload" namespace="/" method="POST" enctype="multipart/form-data"><!-- 该name需要和后台的File类型的名字对应起来,否则将得不到该文件 size 上传文件的大小 --><s:file name="fileUpload" label="Select a File to upload" size="40" /><s:submit value="submit" name="submit" /></s:form></body></html>

7.创建WebContent/success.jsp页面作为上传成功的页面,创建WebContent/error.jsp页面作为文件上传失败的页面

0 0
原创粉丝点击