JAVA WEB Struts2实验

     

班级   软件16-1   学号 3162052051116  姓名 张识虔   同组实验者              

实验名称       Struts2             日期 2018 11 14

一、实验目的

理解MVC设计模式的基本概念和Java Web开发的两种模式Model1和Model2,以及Struts开发工作流程和基本应用。

二、实验要求

  1. 掌握Struts2.0的开发环境的建立。
  2. 理解Struts的框架原理。
  3. 掌握Struts的工作流程。
  4. 掌握Struts2.0的基本应用。

三、实验内容

(一) Struts2的文件上传

1. 文件上传页面,其中包含两个表单域:文件标题和文件浏览域

    • 程序功能:上传页面,包含两个表单域。
    • 程序源码:

upload.html

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=GBK" />

<title>简单的文件上传</title>

</head>

<body>

<form action="upload.action" method="post" enctype="multipart/form-data">

         文件标题:<input type="text" name="title" /><br>

         选择文件:<input type="file" name="upload" /><br>

   <input value="上传" type="submit" />

</form>

</body>

</html>

2.处理上传请求的Action类

     程序源码:

UploadAction.java

package 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 UploadAction extends ActionSupport {

      

       private String title;

       private File upload;

       private String uploadContentType;

       private String uploadFileName;

       private String savePath;

       public String getTitle() {

              return (this.title);

       }

       public void setTitle(String title) {

              this.title = title;

       }

       public File getUpload() {

              return (this.upload);

       }

       public void setUpload(File upload) {

              this.upload = upload;

       }

       public String getUploadContentType() {

              return (this.uploadContentType);

       }

       public void setUploadContentType(String uploadContentType) {

              this.uploadContentType = uploadContentType;

       }

       public String getUploadFileName() {

              return (this.uploadFileName);

       }

       public void setUploadFileName(String uploadFileName) {

              this.uploadFileName = uploadFileName;

       }

       public String getSavePath()throws Exception {

              return ServletActionContext.getRequest().getRealPath(savePath);

             

       }

       public void setSavePath(String savePath) {

              this.savePath = savePath;

       }

      

       @Override

       public String execute()throws Exception{

              System.out.println("开始上传单个文件-------------");

              System.out.println(getSavePath());

              System.out.println("============"+getUploadFileName());

              System.out.println("============"+getUploadContentType());

              System.out.println("============"+getUpload());

              //以服务器的文件保存地址和原文件名建立上传文件传输流

              FileOutputStream fos = new FileOutputStream(getSavePath() +"\\" +

              getUploadFileName());

              FileInputStream fis = new FileInputStream(getUpload());

              byte[] buffer = new byte[1024];

              int len = 0;

              while((len = fis.read(buffer))>0){

                     fos.write(buffer,0,len);

              }

              return SUCCESS;

       }

      

}

 

添加struts.xml 步骤

 

首先点击 文件包

 

创建成功之后 在 src  出现  

配置文件上传的Action

程序源码:

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.custom.i18n.resources" value="globalMessages"/>

<constant name="struts.i18n.encoding" value="GBK"/>

 

<package name="lee" extends="struts-default">

 

        <action name="upload" class="lee.UploadAction">

            <param name="savePath">/upload</param>

               <result>/succ.jsp</result>    

        </action>      

</package>

</struts> 

. 部署并浏览该页面。

 

选择的文件一定要放到 自己的文件中  这是我的例子  如果不放入此文件 就无法上传

实验截图

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值