strus2文件上传

1. 使用Struts2上传文件到服务器中,文件上传成功后在eclipse的工作区间是看不见上传的文件的,可以在Tomcat服务器指定路径中查看上传的文件信息。

2. WebRoot目录下必须创建一个用于保存上传文件的目录,否则报错“找不到指定目录”。

 

上传页面:upload.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>   
    <base href="<%=basePath%>">   
       
    <title>Struts2文件上传</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>   
   <center>   
    <h1>Struts 2完成上传</h1>   
      <form action="upload.action" method="post" enctype="multipart/form-data">   
        <table>   
            <tr>   
                <td>用户名:</td>   
                <td><input type="text" name="username" ></td>   
            </tr>   
            <tr>   
                <td>上传文件:</td>   
                <td><input type="file" name="myFile"></td>   
            </tr>   
            <tr>   
                <td><input type="submit" value="上传"></td>   
                <td><input type="reset"></td>   
            </tr>   
        </table>   
      </form>   
  </center>   
  </body>   
</html>  


strus2配置文件: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>   
  
    <package name="struts2" extends="struts-default">   
        <action name="upload" class="net.hncu.struts2.action.UploadAction">   
            <result name="success">/result.jsp</result>   
            <result name="input">/upload.jsp</result>   
        </action>   
    </package>   
</struts>  



上传成功显示页面:result.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 'result.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>
   上传成功!!<br>
   用户名:<s:property value="username"/><br>
   文件名:<s:property value="myFileFileName"/>
  </body>
</html>




 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值