xhEditor具备在编辑框中插入图片和音频,但是默认情况时关闭的,可以通过如下形式来实现在编辑框中插入图片并显示在编辑框中。下面实现利用struts上传图片,需要注意的是xhEditor上传图片时参数名为filedata。
1、JSP页面
2、action
1、JSP页面
<textarea class="xheditor {upBtnText:'浏览',upImgUrl:'uploadAction!uploadImg',upImgExt:'jpg,jpeg,gif,png'}"></textarea>
2、action
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;
import com.opensymphony.xwork2.ActionSupport;
@Component("uploadAction")
public class UploadAction extends ActionSupport {
/**
*
*/
private static final long serialVersionUID = 7087202718619368736L;
private File filedata;
private String filedataFileName;
private String filedataContentType;
public void uploadImg() {
ServletContext sc = ServletActionContext.getServletContext();System.out.println(sc.getContextPath());
if(filedata!=null){
String dataDir = sc.getRealPath("/images/upload");
String fileType = filedataFileName.substring(filedataFileName.lastIndexOf("."));
int na = (int)(Math.random() * 9999.0D);
String tempName = new Long(System.currentTimeMillis()).toString() + String.valueOf(na) + fileType;
File savedFile = new File(dataDir, tempName);
filedata.renameTo(savedFile);
tempName = sc.getContextPath() + "/images/upload/" + tempName;
try {
PrintWriter out = ServletActionContext.getResponse().getWriter();
out.print("{\"err\":\"\",\"msg\":\"" + tempName + "\"}");
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
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;
}
public String getFiledataContentType() {
return filedataContentType;
}
public void setFiledataContentType(String filedataContentType) {
this.filedataContentType = filedataContentType;
}
}