您的位置:首页 > 编程语言 > Java开发

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' 
加上这两个内容后,再试一次 就成功了
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: