strus2文件上传

来源:互联网 发布:创玻璃优化软件 编辑:程序博客网 时间:2024/06/08 04:08

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>




 

原创粉丝点击