原创作者: radovi   阅读:5262次   评论:9条   更新时间:2011-05-26    
注:本文系作者在看了浪曦的风中叶老师的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'
加上这两个内容后,再试一次 就成功了
评论 共 9 条 请登录后发表评论
9 楼 1351539356 2011-04-03 23:49
http://www.jianruishiyouyaowu.com

http://www.hshxjx.com

http://www.jrsy100.com

http://www.6699ok.com

http://www.50suncity.net

http://www.wabjw.com

http://wenwen.soso.com/z/q274894589.htm
8 楼 虚弱的java 2011-02-19 14:53
这篇文章我也没有看出来与structs2有关系?
7 楼 cokin_tkj 2010-07-30 15:30
一塌糊涂~~~~
6 楼 ynstudio 2010-02-08 19:47
看不懂这些回复的朋友怎么理解的。

我觉着这个例子满合适的,非常简短,说明了原理,一看即明。


引用
请教这篇文章和struts2有什么关系?

这个是讲上传的原理,不是struts2的上传如何用。

引用
就两个JSP文件 还号称上传的底层 我都不好意思说了。。

难道写上一堆?初学者好理解吗?

引用
别告诉我你的后台是空的昂~~~最好能将java程序不写在jsp页面上~~

现在这样不是更简洁易懂吗?


5 楼 xici_magic 2009-12-16 15:10
就两个JSP文件 还号称上传的底层 我都不好意思说了。。
4 楼 yangqianyu222 2009-06-12 11:06
引用
本讲主要讲解struts2的文件上传机制的底层。


请教这篇文章和struts2有什么关系?
3 楼 yangqianyu222 2009-06-12 10:57
强烈建议:代码格式太乱了,另外是否可以把
<meta http-equiv="pragma" content="no-cache"> 

诸如此类的跟该程序无关的代码去掉,那样会更清晰些。
2 楼 radovi 2009-03-13 12:57
sharp_lover 写道

别告诉我你的后台是空的昂~~~最好能将java程序不写在jsp页面上~~

是的 呵呵
1 楼 sharp_lover 2009-03-13 09:44
别告诉我你的后台是空的昂~~~最好能将java程序不写在jsp页面上~~

发表评论

您还没有登录,请您登录后再发表评论

文章信息

  • radovi在2009-03-12创建
  • radovi在2011-05-26更新
  • 标签: 文件上传, struts2
Global site tag (gtag.js) - Google Analytics