使用ueditor编辑器大大方便了我们的开发,下面我说一下在使用ueditor上传图片的java方法实现:
public void uploadImage() {
List<UploadFile> fileList = getFiles( PathKit.getWebRootPath()+ RESOURCE_DIR);
String imgpath =PathKit.getWebRootPath() + RESOURCE_DIR+"/";
String title = "";
String fname = "";
String newFileName = "";
String state = "SUCCESS";
if (fileList != null && fileList.size() > 0) {
UploadFile file = (UploadFile) fileList.get(0);
UploadFile f = (UploadFile) file;
// 获取文件名
fname = f.getFileName();
// 获取的是.jpg
String suwf = fname.substring(fname.lastIndexOf("."),
fname.length());
// 随机生成的
newFileName = fileUtil.getNewFileName(fname);
f.getFile().renameTo(new File(imgpath + newFileName));
try {
File file1 = new File(imgpath + newFileName);
Iterator<ImageReader> readers = ImageIO
.getImageReadersByFormatName(suwf.substring(1,
suwf.length()));
ImageReader reader = (ImageReader) readers.next();
ImageInputStream iis = ImageIO.createImageInputStream(file1);
reader.setInput(iis, true);
iis.close();
} catch (Exception e) {
e.printStackTrace();
}
// 实际上是原图片名
title = getPara("pictitle");
if (title == null || title.trim().length() == 0) {
title = fname;
}
} else {
state = "";
}
title = title.replace("&", "&").replace("'", "&qpos;")
.replace("\"", """).replace("<", "<")
.replace(">", ">");
renderText("{'original':'" + fname + "','url':'http://"
+ getRequest().getServerName() + ":"
+ getRequest().getServerPort() + getRequest().getContextPath()
+ RESOURCE_DIR+"/" + newFileName + "','title':'" + title
+ "','state':'" + state + "'}");
}