jsp中文件上传

    技术2022-05-12  12

    FileUpload.html

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head>  <title>FileUpload.html</title>

      <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">  <meta http-equiv="description" content="this is my page">  <meta http-equiv="content-type" content="text/html; charset=UTF-8">

      <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

     </head>

     <body>  <form action="/test6/DoFileUpload.jsp" method="POST"   ENCTYPE="multipart/form-data">   <table border="1">    <tr>     <td>      <input type="file" name="flieUpload">     </td>    </tr>    <tr>     <td>      <input type="submit" name="submit" value="上传">     </td>    </tr>   </table>  </form> </body></html>

     

    以下是上传的方法:

    <%@ page language="java" import="java.util.*,com.jspsmart.upload.*" pageEncoding="gb2312"%><% 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 'DoFileUpload.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>  <%   int i=0;   SmartUpload su = new SmartUpload();   su.initialize(pageContext);//上传初始化   su.upload();//上传    File file = su.getFiles().getFile(i);   if (!file.isMissing()) {    int fileSize = su.getSize();    if(fileSize > 3000) {     out.print("<script language='javascript'>/n");     out.print("alert('上传的文件超过3KB,不能上传!');/n");     //out.print("window.document.location.href='ring_add.jsp';/n");     out.print("</script>/n");    } else {          int count = su.save("F:/tomcat5.028/webapps/fileupload");//上传的文件保存到fileupload目录下面     out.println(count + "个文件已经上传.");     out.println("<table>");     out.println("<tr><td>文件名</td><td>大小</td><td>类型</td></tr>");     for (; i<su.getFiles().getCount(); i++) {      out.print("<tr><td>" + file.getFileName() + "</td><td>" + file.getSize() + "</td><td>" + file.getFileExt() + "</td></tr>");     }     out.println("</table>");    }   }  %> </body></html>


    最新回复(0)