java使用CKEditor实现图片上传功能

网友投稿 252 2023-04-27


java使用CKEditor实现图片上传功能

java如何使用CKEditor实现图片上传功能,具体内容如下

1.根据实际需要下载指定的CKEditor

2.删除文件ckeditor/plugins/image/dialogs/image.js预览框中文本内容,并修改hidden属性值为显示上传选项卡

删除image.js中包含在双引号中的上述文本

将image.js中的hidden属性值改为0

3.修改ckeditor/config.js文件,配置“上传到服务器”按钮调用的controller接口

4.“上传到服务器”按钮调用的controller级别的接口

@Controller

@RequestMapping("publicutil")

public class PublicUtilController {

@RequestMapping(value = "uploadImage")

private void uploadImage(HttpServletRequest request, HttpServletResponse response, HttpSession session,@RequestParam MultipartFile[] upload) {

response.setCharacterEncoding("UTF-8");

PrintWriter out=null;

try {

out = response.getWriter();

} catch (IOException e1) {

logger.error("response.getWriter()异常="+e1);

e1.printStackTrace();

}

String callback = request.getParameter("CKEditorFuncNum");

// 获得response,request

Map m = new HashMap();

if (!ServletFileUpload.isMultipartContent(request)) {

m.put("error", 1);

m.put("message", "请选择文件!");

//return m;

logger.info("请选择文件!");

}

String originalFileName=null;//上传的图片文件名

String fileExtensionName=null;//上传图片的文件扩展名

for (MultipartFile file : upload) {

if (file.getSize()> 10*1024* 1024) {

out.println("");

}

originalFileName=file.getOriginalFilename();

logger.info("上传的图片文件名="+originalFileName);

fileExtensionName= originalFileName.substring(

originalFileName.lastIndexOf(".") ,originalFileName.length()).toLowerCase();

logger.info("图片文件扩展名="+fileExtensionName);

String[] imageExtensionNameArray= WebsiteConstant.IMAGE_EXTENSION_NAME_ARRAY;

String allImageExtensionName="";

boolean isContain=false;//默认不包含上传图片文件扩展名

for(int i=0;i

if(fileExtensionName.equals(imageExtensionNameArray[i])){

isContain=true;

}

if(i==0){

allImageExtensionName+=imageExtensionNameArray[i];

}else{

allImageExtensionName+=" , "+imageExtensionNameArray[i];

}

}

String newFileName=java.util.UUID.randomUUID().toString()+fileExtensionName;

String uploadPath =WebsiteConstant.PIC_APP_FILE_SYSTEM_CKEDITOR_LOCATION;

if(isContain){//包含

File pathFile = new File(uploadPath);

if (!pathFile.exists()) { // 如果路径不存在,创建

pathFile.mkdirs();

}

try {

FileUtils.copyInputStreamToFile(file.getInputStream(), new File(uploadPath ,newFileName));

// InputStream is=file.getInputStream();

// File toFile = new File(uploadPath, newFileName);

// OutputStream os = new FileOutputStream(toFile);

// byte[] buffer = new byte[1024];

// int length = 0;

// while ((length = is.read(buffer)) > 0) {

// os.write(buffer, 0, length);

// }

// is.close();

// os.close();

} catch (IOException e) {

logger.error("FileUtils.copyInputStreamToFile uploadPath="+uploadPath+" newFileerutZBName ="+newFileName+" exception="+e);

}

String imageUrl=WebsiteConstant.PIC_APP_SERVER_URL+"images/ckeditor/"+newFileName;http://

// 返回"图像信息"选项卡并显示图片 ,在对应的文本框中显示图片资源url

out.println("");

}else{

out.println("");

}

}

}

}

public class WebsiteConstant {

public static String[] IMAGE_EerutZBXTENSION_NAME_ARRAY={".jpg",".jpeg",".png",".gif",".bmp"};

public static String PIC_APP_SERVER_URL="http://localhost:8090/Picture/";

public static String PIC_APP_FILE_SYSTEM_CKEDITOR_LOCATION="/Users/abc/Documents/tomcat/webapps/Picture/images/ckeditor/";

public static final int SUCCESS = 1; // 操作成功

5.若是在Maven项目中使用的CKEditor,需要在pom.xml中添加如下代码:

com.ckeditor

ckeditor-java-core

3.5.3

6.最终效果图

if(fileExtensionName.equals(imageExtensionNameArray[i])){

isContain=true;

}

if(i==0){

allImageExtensionName+=imageExtensionNameArray[i];

}else{

allImageExtensionName+=" , "+imageExtensionNameArray[i];

}

}

String newFileName=java.util.UUID.randomUUID().toString()+fileExtensionName;

String uploadPath =WebsiteConstant.PIC_APP_FILE_SYSTEM_CKEDITOR_LOCATION;

if(isContain){//包含

File pathFile = new File(uploadPath);

if (!pathFile.exists()) { // 如果路径不存在,创建

pathFile.mkdirs();

}

try {

FileUtils.copyInputStreamToFile(file.getInputStream(), new File(uploadPath ,newFileName));

// InputStream is=file.getInputStream();

// File toFile = new File(uploadPath, newFileName);

// OutputStream os = new FileOutputStream(toFile);

// byte[] buffer = new byte[1024];

// int length = 0;

// while ((length = is.read(buffer)) > 0) {

// os.write(buffer, 0, length);

// }

// is.close();

// os.close();

} catch (IOException e) {

logger.error("FileUtils.copyInputStreamToFile uploadPath="+uploadPath+" newFileerutZBName ="+newFileName+" exception="+e);

}

String imageUrl=WebsiteConstant.PIC_APP_SERVER_URL+"images/ckeditor/"+newFileName;http://

// 返回"图像信息"选项卡并显示图片 ,在对应的文本框中显示图片资源url

out.println("");

}else{

out.println("");

}

}

}

}

public class WebsiteConstant {

public static String[] IMAGE_EerutZBXTENSION_NAME_ARRAY={".jpg",".jpeg",".png",".gif",".bmp"};

public static String PIC_APP_SERVER_URL="http://localhost:8090/Picture/";

public static String PIC_APP_FILE_SYSTEM_CKEDITOR_LOCATION="/Users/abc/Documents/tomcat/webapps/Picture/images/ckeditor/";

public static final int SUCCESS = 1; // 操作成功

5.若是在Maven项目中使用的CKEditor,需要在pom.xml中添加如下代码:

com.ckeditor

ckeditor-java-core

3.5.3

6.最终效果图


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java List 用法详解及实例分析
下一篇:Vue学习笔记进阶篇之单元素过度
相关文章

 发表评论

暂时没有评论,来抢沙发吧~