注:本文系做者在看了浪曦的风中叶老师的struts2视频的我的总结,但愿能帮助广大struts2的初学者。
本讲主要讲解struts2的文件上传机制的底层。
首先咱们仍是新建一个新的web project 取名为upload_test
而后在WebRoot中新建两个jsp页面 upload.jsp 和result.jsp
代码分别以下:
upload.jsp
html
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%> web
<% app
String path = request.getContextPath(); jsp
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; post
%> ui
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> spa
<html> .net
<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'
加上这两个内容后,再试一次 就成功了
struts2 核心拦截器2 (微微进阶)——stru ...
upload_test.rar (3.1 KB)
下载次数: 116
评论 共 9 条 请登陆后发表评论
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
这篇文章我也没有看出来与structs2有关系?
一塌糊涂~~~~
看不懂这些回复的朋友怎么理解的。
我觉着这个例子满合适的,很是简短,说明了原理,一看即明。
引用
请教这篇文章和struts2有什么关系?
这个是讲上传的原理,不是struts2的上传如何用。
引用
就两个JSP文件 还号称上传的底层 我都很差意思说了。。
难道写上一堆?初学者好理解吗?
引用
别告诉我你的后台是空的昂~~~最好能将java程序不写在jsp页面上~~
如今这样不是更简洁易懂吗?
就两个JSP文件 还号称上传的底层 我都很差意思说了。。
引用
本讲主要讲解struts2的文件上传机制的底层。
请教这篇文章和struts2有什么关系?
强烈建议:代码格式太乱了,另外是否能够把
<meta http-equiv="pragma" content="no-cache">
诸如此类的跟该程序无关的代码去掉,那样会更清晰些。
sharp_lover 写道
别告诉我你的后台是空的昂~~~最好能将java程序不写在jsp页面上~~
是的 呵呵
别告诉我你的后台是空的昂~~~最好能将java程序不写在jsp页面上~~