1.首先下载个KindEditor编辑器然后解压,放到web目录下,里面有很多文件夹和小例子,不知道啥用处我都没有删除它们,因为我项目里用的是jsp,所以我找到了它里面的一个jsp的例子,然后把它改了下,如下:
(1)demo.jsp
<html>
<head>
<base href="http://${header['host']}${pageContext.request.contextPath}/" />
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src=\'#\'" /jquery.js"></script>
<link rel="stylesheet" href="kindeditor/themes/default/default.css" />
<link rel="stylesheet" href="kindeditor/plugins/code/prettify.css" />
<script charset="utf-8" src=\'#\'" /kindeditor.js"></script>
<script charset="utf-8" src=\'#\'" /lang/zh_CN.js"></script>
<script charset="utf-8" src=\'#\'" /plugins/code/prettify.js"></script>
<script>
KindEditor.ready(function(K) {
var editor1 = K.create('textarea[name="artContentId"]', {
cssPath : 'kindeditor/plugins/code/prettify.css',
uploadJson : '/xheditor/upload',
allowFileManager : true,
/* afterCreate : function() {
var self = this;
K.ctrl(document, 13, function() {
self.sync();
document.forms['form1'].submit();
});
K.ctrl(self.edit.doc, 13, function() {
self.sync();
document.forms['form1'].submit();
});
} 没有用处*/
afterBlur:function(){this.sync();} //注明失去焦点后提交form,这个很重要,也是我苦恼好久的问题,为啥子,直接type=submit就可以提交,而加上了js验证了就提交不了内容的了的原因
});
/* prettyPrint(); 没有用处*/
/* K('#form1').bind('submit', function() {
editor.sync();
}); 没有用处*/
});
<form action="admin/news/addarticle/${dirname}" method="post" id="form1"> 文章内容 <textarea name="artContent" id="artContentId" cols="100" rows="8" style="width:700px;height:200px;visibility:hidden;">
</textarea>
<input type="button" ID="submitFormId" class="btnInput" value="提交" />
(2)接收图片servlet
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.util.Date;
import javax.imageio.ImageIO;
/**
* 多线程 图片上传功能
*
* @author User
*
*/
public class FileUploadThread extends Thread implements Serializable {
private static final long serialVersionUID = 7383841520335606068L;
private String fileRealPath; // context路径
private String fileNameLong;// 源文件名
private InputStream input;// 文件流
private String faceurl;// 相对文件夹路径
private byte[] b;
private String returnStr;
private int returnType;//1=返回原图,2=返回小图
public FileUploadThread(String fileRealPath, String fileNameLong,
String faceurl, InputStream input, byte[] b, int returnType) {
this.fileRealPath = fileRealPath;
this.fileNameLong = fileNameLong;
this.faceurl = faceurl;
this.input = input;
this.b = b;
this.returnType = returnType;
}
public String getFileRealPath() {
return fileRealPath;
}
public void setFileRealPath(String fileRealPath) {
this.fileRealPath = fileRealPath;
}
public String getReturnStr() {
return returnStr;
}
public void setReturnStr(String returnStr) {
this.returnStr = returnStr;
}
public String getFileNameLong() {
return fileNameLong;
}
public void setFileNameLong(String fileNameLong) {
this.fileNameLong = fileNameLong;
}
public InputStream getInput() {
return input;
}
public void setInput(InputStream input) {
this.input = input;
}
public byte[] getB() {
return b;
}
public void setB(byte[] b) {
this.b = b;
}
public synchronized void run() {
// 获取上传的文件名称
// String fileNameLong = uploadFile.getOriginalFilename();
// 获取文件扩展名
String extensionName = fileNameLong.substring(fileNameLong
.lastIndexOf(".") + 1);
if (!extensionName.equals("jpg")) {
if (!extensionName.equals("gif")) {
if (!extensionName.equals("png")) {
this.returnStr = "false";
}
}
}
String baseDir = "/uploadimage/";
String fileFolder = faceurl;
/* 文件存储的相对路径 */
String saveDirPath = baseDir + fileFolder + "/";
/* 文件存储在容器中的绝对路径 */
// String saveFilePath
String saveFilePath = fileRealPath + saveDirPath;
/* System.out.println(saveFilePath); */
/* 构建文件目录以及目录文件 */
File fileDir = new File(saveFilePath);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
/* 重命名文件 */
String filename = Digest.dateFormatString(new Date())
.replaceAll(" ", "").replaceAll(":", "");
try {
FileOutputStream out = new FileOutputStream(saveFilePath + filename
+ "." + extensionName);
out.write(b); // 写入文件
out.close();
BufferedImage image = ImageIO.read(input);
ImageIO.write(ResizeImage.resizeImage(image, 100, 100), "jpg",
new File(saveFilePath + filename + "_small."
+ extensionName));
} catch (Exception e) {
e.printStackTrace();
}
if(returnType == 1) {
returnStr = saveDirPath + filename+ "." + extensionName;
} else {
returnStr = saveDirPath + filename + "_small." + extensionName;
}
}
}