檔案上傳功能+富文本頁面!!!!!!!!!!!!!!!!

來源:互聯網
上載者:User

標籤:

一:檔案上傳功能

1:首先在index.jsp的介面上初始化一個表單。

<body>        <form  enctype="multipart/form-data" action="<%=path%>/1.jsp" method="post">            姓名:<input type="text" name="username"/>  選擇檔案:<input type="file" name="myfile"/>      <input type="submit" value="提交"/>        </form></body>

:enctype=多部分的表單資料,並且如果form表單的屬性中多了enctype="multipart/form-data",是不能使用request.getParameter(name屬性的)

2:在WEB-ROOT的根目錄下建立一個1.jsp,實現檔案上傳功能!

<%@page import="java.io.File"%><%@page import="org.apache.commons.fileupload.FileItem"%><%@page import="org.apache.commons.fileupload.disk.DiskFileItemFactory"%><%@page import="org.apache.commons.fileupload.servlet.ServletFileUpload"%><%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%   //判定request請求的類型     request.setCharacterEncoding("utf-8");    boolean flag= ServletFileUpload.isMultipartContent(request);     if(flag)     {         DiskFileItemFactory factory=new DiskFileItemFactory();        //找到一個解析器,解析請求中的各個項目         ServletFileUpload upload=new ServletFileUpload(factory);//解析器的建立         List<FileItem> list=upload.parseRequest(request);//使用解析器解析請求的資料         Iterator<FileItem> myitor= list.iterator();//自動迭代的功能         while(myitor.hasNext())         {                FileItem item=myitor.next();                if(item!=null)                {                  //判斷FileItem對象封裝的資料類型,檔案表單或普通表單欄位                  if(item.isFormField())//普通表單                  {                   String name= item.getFieldName();//擷取表單的name屬性                   if(name.equals("username"))                   {                      out.print(item.getString("utf-8"));                   }                  }                  else                  {                    String name=item.getName();//獲得檔案名稱                    out.print(name);                    String path="/WEB-INF/upload/";//相對路徑名                    String path2=this.getServletContext().getRealPath(path);//通過相對路徑名來獲得絕對路徑名                    out.print(path2);                    File file=new File(name);                    File uploadpath=new File(path2,file.getName());                    item.write(uploadpath);//向該路徑寫入檔案                    out.print("上傳成功");                  }                }         }     } %>

二:富文字檔

1:建立一個moneyText.jsp頁面,將下載的ckeditor檔案夾copy到WEB-INF檔案夾下

 <script type="text/javascript" src="ckeditor/ckeditor.js"></script>  <body>  <form action="/fileInfo/success.jsp" method="post">    <textarea class="ckeditor" name="txtConent"></textarea>    <input type="submit" value="提交"/>      </form></body>

2:建立一個sucess頁面用來展示從moneyText.jsp富文本傳遞過來的資料

 <%         request.setCharacterEncoding("utf-8");   %>  <script type="text/javascript" src="ckeditor/ckeditor.js"></script>  <body>    <textarea class="ckeditor" name="Conent"><%=request.getParameter("txtConent")%></textarea>  </body>

 

檔案上傳功能+富文本頁面!!!!!!!!!!!!!!!!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.