File Upload function:
1. Start by initializing a form on the index.jsp interface.
<body> <form enctype="Multipart/form-data"action="<%=path%>/1.jsp"Method="Post">Name:<input type="text"Name="username"/>Select File:<input type="file"Name="myfile"/> <input type="Submit"Value="Submit"/> </form></body>
Initialize the form
2. Create a 1.jsp in the root directory of the web-root to achieve file upload function
<% @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"%><%//determine the type of request requestedRequest.setcharacterencoding ("Utf-8"); Boolean flag=servletfileupload.ismultipartcontent (Request); if(flag) {Diskfileitemfactory Factory=Newdiskfileitemfactory (); //find a parser to parse each item in the requestServletfileupload upload=NewServletfileupload (Factory);//Creation of parsersList<fileitem> list=upload.parserequest (Request);//parsing the requested data using the parserIterator<fileitem> myitor= list.iterator ();//features of automatic iterations while(Myitor.hasnext ()) {Fileitem Item=Myitor.next (); if(item!=NULL) { //determine the type of data encapsulated by the Fileitem object, a file form, or a normal form field if(Item.isformfield ())//Normal Forms{String name= Item.getfieldname ();//gets the Name property of the form if(Name.equals ("username")) { out. Print (Item.getstring ("Utf-8")); } } Else{String name=item.getname ();//Get file name out. Print (name); String Path="/web-inf/upload/";//Relative path nameString path2= This. Getservletcontext (). Getrealpath (path);//to obtain an absolute pathname by relative path name out. Print (path2); File File=NewFile (name); File Uploadpath=NewFile (Path2,file.getname ()); Item.write (Uploadpath);//write files to this path out. Print ("Upload Successful"); } } } } %>
Create a file
Rich Text file:
1. Create a moneytext.jsp page and copy the downloaded CKEditor folder to the Web-inf folder
<script type="Text/javascript"Src="Ckeditor/ckeditor.js"></script> <body> <form action="/fileinfo/success.jsp"Method="Post"> <textareaclass="CKEditor"Name="txtconent"></textarea> <input type="Submit"Value="Submit"/> </form></body>
Create a folder
2. Create a sucess page to show the data that moneytext.jsp Rich text passes over
<%request.setcharacterencoding ("Utf-8"); %> <script type="Text/javascript"Src="Ckeditor/ckeditor.js"></script> <body> <textareaclass="CKEditor"Name="conent"><%=request.getparameter ("txtconent")%></textarea> </body>
Passing Data
File upload and Rich Text page