文件上传功能:
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>
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>