File upload and Rich Text page

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.