网络编程

struts2 文件上传与下载 初始文件上传的底层技术——struts2第七讲

发布制作:admin  发布日期:2011/6/8
注:本文系作者在看了浪曦的风中叶老师的struts2视频的个人总结,希望能帮助广大struts2的初学者。

本讲主要讲解struts2的文件上传机制的底层。


首先我们还是新建一个新的web project 取名为upload_test

然后在WebRoot中新建两个jsp页面 upload.jsp 和result.jsp

代码分别如下:
upload.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
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 'index.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>
<form action='result.jsp' name='upload'>
							username : <input name='name' type='text'><br>
							file : <input name='file' type='file' >
							<br>
							<input type='submit'  value='submit' name='submit'>
					</form>


  </body>
</html>



然后是result.jsp页面:
<%@ page language="java" import="java.util.*" pageEncoding="gbk"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ page import='java.io.*' %>
<!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>
			

                                   <%
                                   				InputStream is  = request.getInputStream();          //         新建一个inputstream对象  注意应该在这个页面中导入java.io.*包
                                                 
                                                 BufferedReader bu = new BufferedReader( new InputStreamReader(is));                  // 将输入的内容转换成字符流
                                                 
                                                 String buffer = null;
                                                 
                                                 while( (buffer = bu.readLine()) != null )                        //如果还有内容 怎继续输出
                                                 {
                                                	 out.print(buffer+"<br>");
                                                 }
                                   
                                   %>
  </body>
</html>


这样的基本课可以上传了
点击文件后点击提交 那么跳转到了result.jsp页面 可是没有任何信息输出 这是怎么回事呢
这里要注意的是 在文件上上传的表单中必须要包含两个内容
method='post' 还有 enctype='multipart/form-data'
加上这两个内容后,再试一次 就成功了