Use xheditor struts2 to upload images

Source: Internet
Author: User

For the xheditor environment, see At this time, the image search function does not have an upload button.

If you want to see the upload button, set the following parameters in xheditor:

Html5upload: false// This attribute must be false

Upimgurl: "imgupload. Action"// Upload server interface

Onupload: insertupload// Server return information, in JSON format

I. Front-end code

<SCRIPT type = "text/JavaScript"> $ (function () {$ ('# content '). xheditor ({tools: 'full', // You can also customize tools. Full html5upload: false, // This attribute must be false; otherwise, the image cannot be uploaded. upimgurl: "imgupload. action ", onupload: uploadimg}); function uploadimg (data ){//... callback Function }}) </SCRIPT>
<body><div align="center"><textarea rows="20" cols="110" id="content"></textarea></div></body>
Ii. struts2 upload code

Public class imguploadaction extends actionsupport {private string err = ""; private string MSG; // return information private file filedata; // Upload File private string filedatafilename; // file name Public String imgupload () {// get response, request object actioncontext AC = actioncontext. getcontext (); httpservletresponse response = (httpservletresponse) AC. get (servletactioncontext. http_response); httpservletrequest request = (httpservlet Request) AC. get (servletactioncontext. http_request); response. setcontenttype ("text/html; charset = GBK"); printwriter out = NULL; try {out = response. getwriter () ;}catch (ioexception E1) {e1.printstacktrace () ;}string saverealfilepath = servletactioncontext. getservletcontext (). getrealpath ("/upload"); file filedir = new file (saverealfilepath); If (! Filedir. exists () {// create filedir if it does not exist. mkdirs ();} file SaveFile; SaveFile = new file (saverealfilepath + "/" + filedatafilename); try {fileutils. copyfile (filedata, SaveFile);} catch (ioexception e) {err = "error" + E. getmessage (); E. printstacktrace ();} string filename = request. getcontextpath () + "/upload/" + filedatafilename; MSG = "{\" Err \ ": \" "+ err +" \ ", \" MSG \": \ "" + filename + "\"} "; out. print (MSG); // return MSG information return NULL;} Public String geterr () {return err;} public void seterr (string ERR) {This. err = err;} Public String getmsg () {return MSG;} public void setmsg (string MSG) {This. MSG = MSG;} public file getfiledata () {return filedata;} public void setfiledata (File filedata) {This. filedata = filedata;} Public String getfiledatafilename () {return filedatafilename;} public void setfiledatafilename (string filedatafilename) {This. filedatafilename = filedatafilename ;}}
Iii. struts2 configuration file

<struts><package name="build" extends="struts-default"><action name="ImgUpload" method="imgUpload"class="com.itmyhome.ImgUploadAction"></action></package></struts>

Download project demo source code: to be displayed

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: 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.