struts中文件的上传和下载
2014-06-16 17:15
411 查看
首先我们还是新建一个新的web project 取名为upload_test
然后在WebRoot中新建两个jsp页面 upload.jsp 和result.jsp
代码分别如下:
upload.jsp
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页面:
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'
加上这两个内容后,再试一次 就成功了
然后在WebRoot中新建两个jsp页面 upload.jsp 和result.jsp
代码分别如下:
upload.jsp
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页面:
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.1笔记(九):上传和下载文件
- struts 实现文件上传与下载
- struts 文件上传及文件下载
- 文件上传下载——通过struts的FormFile上传单个excel文件
- 文件上传与下载————>struts
- struts 实现文件上传与下载
- struts 实现文件上传与下载
- Struts2.x上传和下载文件
- 文件上传下载——通过struts的FormFile上传单个excel文件
- struts1 文件上传下载
- Struts2.0 文件上传与下载全解析
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- 传智播客--struts2.x表单重复提交,文件上传下载,转换器
- struts1文件上传和下载
- struts 实现文件上传与下载
- Struts2.0实现的文件上传(单附件和多附件)以及附件下载功能
- struts 实现文件上传与下载
- struts2.0上传和下载文件
- Struts实现文件上传与下载
- struts 文件上传 下载