小菜毛毛技術(shù)分享

          與大家共同成長(zhǎng)

            BlogJava :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
            164 Posts :: 141 Stories :: 94 Comments :: 0 Trackbacks

          上傳文件選取頁(yè)面:upload.html

          <html>
          <head>
          <title>文件上傳</title>
          <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
          </head>
          <body>
          <b>文件上傳</b></font></p>

          <form name="UploadForm" enctype="multipart/form-data" method="post" action="upload.jsp">
          <input type="file" name="File1" size="20" maxlength="20"> <br>
          <input type="submit"value="上傳">
          </form>

          </body>
          </html>

          上傳處理:upload.jsp

          <%@ page contentType="text/html; charset=GBK" %>
          <%@ page import="java.io.*"%>
          <%@ page import="java.util.*"%>
          <%@ page import="javax.servlet.*"%>
          <%@ page import="javax.servlet.http.*"%>
          <html>
          <head>
          <title> upFile </title>
          </head>
          <body bgcolor="#ffffff">
          <center>
          <%
          //定義上載文件的最大字節(jié)
          int MAX_SIZE = 102400 * 102400;
          // 創(chuàng)建根路徑的保存變量
          String rootPath;

          //聲明文件讀入類
          DataInputStream in = null;
          FileOutputStream fileOut = null;
          //取得客戶端的網(wǎng)絡(luò)地址
          String remoteAddr = request.getRemoteAddr();
          out.print("request.getRemoteAddr()"+remoteAddr+"<br>");
          //獲得服務(wù)器的名字
          String serverName = request.getServerName();
          out.print("request.getServerName()="+serverName+"<br>");
          //取得jsp文件相對(duì)與根地址的地址
          out.print("request.getServletPath()="+request.getServletPath()+"<br>");
          //取得互聯(lián)網(wǎng)程序的絕對(duì)地址
          String realPath = request.getRealPath(serverName);
          out.println("request.getRealPath(serverName)="+realPath+"<br>");
          out.println("OK<br>");
          realPath = realPath.substring(0,realPath.lastIndexOf("\\"));
          //創(chuàng)建文件的保存目錄
          rootPath = realPath + "\\upload\\";
          //out.println(rootPath);
          //取得客戶端上傳的數(shù)據(jù)類型
          String contentType = request.getContentType();
          //out.println("<p>客戶端上傳的數(shù)據(jù)類型 = " + contentType + "</p>");
          try{
          if(contentType.indexOf("multipart/form-data") >= 0){
          //讀入上傳的數(shù)據(jù)
          in = new DataInputStream(request.getInputStream());
          int formDataLength = request.getContentLength();
          if(formDataLength > MAX_SIZE){
          out.println("<P>上傳的文件字節(jié)數(shù)不可以超過(guò)" + MAX_SIZE + "</p>");
          return;
          }
          //保存上傳文件的數(shù)據(jù)
          byte dataBytes[] = new byte[formDataLength];
          int byteRead = 0;
          int totalBytesRead = 0;
          //上傳的數(shù)據(jù)保存在byte數(shù)組
          while(totalBytesRead < formDataLength){
          byteRead = in.read(dataBytes,totalBytesRead,formDataLength);
          totalBytesRead += byteRead;
          }
          //根據(jù)byte數(shù)組創(chuàng)建字符串
          String file = new String(dataBytes);
          //out.println(file);
          //取得上傳的數(shù)據(jù)的文件名
          String saveFile = file.substring(file.indexOf("filename=\"") + 10);
          saveFile = saveFile.substring(0,saveFile.indexOf("\n"));
          saveFile = saveFile.substring(saveFile.lastIndexOf("\\") + 1,saveFile.indexOf("\""));
          int lastIndex = contentType.lastIndexOf("=");
          //取得數(shù)據(jù)的分隔字符串
          String boundary = contentType.substring(lastIndex + 1,contentType.length());
          //創(chuàng)建保存路徑的文件名
          String fileName = rootPath + saveFile;
          //out.print(fileName);
          int pos;
          pos = file.indexOf("filename=\"");
          pos = file.indexOf("\n",pos) + 1;
          pos = file.indexOf("\n",pos) + 1;
          pos = file.indexOf("\n",pos) + 1;
          int boundaryLocation = file.indexOf(boundary,pos) - 4;
          //out.println(boundaryLocation);
          //取得文件數(shù)據(jù)的開始的位置
          int startPos = ((file.substring(0,pos)).getBytes()).length;
          //out.println(startPos);
          //取得文件數(shù)據(jù)的結(jié)束的位置
          int endPos = ((file.substring(0,boundaryLocation)).getBytes()).length;
          //out.println(endPos);
          //檢查上載文件是否存在
          File checkFile = new File(fileName);
          if(checkFile.exists()){
          out.println("<p>" + saveFile + "文件已經(jīng)存在.</p>");
          return;
          }
          //檢查上載文件的目錄是否存在
          File fileDir = new File(rootPath);
          if(!fileDir.exists()){
          fileDir.mkdirs();
          }
          //創(chuàng)建文件的寫出類
          fileOut = new FileOutputStream(fileName);
          //保存文件的數(shù)據(jù)
          fileOut.write(dataBytes,startPos,(endPos - startPos));
          fileOut.close();
          out.println("<P>" + saveFile + "文件成功上載.</p>");
          }else{
          String content = request.getContentType();
          out.println("<p>上傳的數(shù)據(jù)類型不是是multipart/form-data</p>");
          }
          }catch(Exception ex){
          throw new ServletException(ex.getMessage());
          }
          %>
          </center>
          </body>
          </html>

          posted on 2010-01-19 21:27 小菜毛毛 閱讀(2500) 評(píng)論(2)  編輯  收藏 所屬分類: J2EE相關(guān)技術(shù)與框架

          Feedback

          # re: 純jsp的單個(gè)文件上載代碼 2014-02-27 10:51 葫蘆娃
          很不錯(cuò)。謝  回復(fù)  更多評(píng)論
            

          # re: 純jsp的單個(gè)文件上載代碼 2014-02-27 10:52 葫蘆娃
          <hr>
          <br/>
          <s>  回復(fù)  更多評(píng)論
            

          主站蜘蛛池模板: 南平市| 彰武县| 章丘市| 青川县| 那曲县| 凤凰县| 五原县| 韶山市| 佛坪县| 尚志市| 响水县| 宁乡县| 永胜县| 工布江达县| 夹江县| 五峰| 南投市| 屏东市| 清涧县| 韶山市| 建德市| 黎城县| 临潭县| 项城市| 大宁县| 赫章县| 黑河市| 江安县| 娱乐| 普宁市| 米泉市| 郸城县| 巫山县| 乌审旗| 舞阳县| 云龙县| 云霄县| 遂川县| 临沂市| 孝昌县| 聊城市|