ckeditor java 上传_ckeditor 4.x 图片上传(java)

该博客介绍了一个Spring Boot控制器实现CKEditor图片上传的功能。通过接收MultipartFile参数,将图片保存到服务器,并返回JSON格式的成功或错误信息。同时,代码中包含了图片格式检查,确保上传的是合法图片格式(jpg/jpeg/png/gif)。
摘要由CSDN通过智能技术生成

packagewebapp.controller;importcom.alibaba.fastjson.JSONObject;importcom.baomidou.mybatisplus.core.toolkit.StringUtils;importorg.apache.commons.io.FileUtils;importorg.springframework.context.annotation.Scope;importorg.springframework.web.bind.annotation.RequestMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.bind.annotation.RestController;importorg.springframework.web.multipart.MultipartFile;importjavax.servlet.http.HttpServletResponse;importjava.io.File;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.HashMap;importjava.util.Map;importjava.util.UUID;

@Scope("prototype")

@RestControllerpublic classFileController {//图片在服务器所在路径

private static String WEB_BASE_PATH = "/var/xxx";//图片前缀域名(需搭建文件服务器)

private static String WEB_PIC_HOST = "http://pic.xxx.com/";//ckeditor图片上传

@RequestMapping("uploadCkEditor")

@ResponseBodypublic void uploadWangEdit(@RequestParam("upload") MultipartFile file, HttpServletResponse response) {

FileResponse fileResponse= newFileResponse();try{

PrintWriter out=response.getWriter();if(file.isEmpty()) {

String error= fileResponse.error(0, "文件为空");

out.println(error);

}

String filePath=WEB_BASE_PATH;

File dir= newFile(filePath);if(!dir.exists()) {

dir.mkdir();

}

String orgName=file.getOriginalFilename();

String[] split= orgName.split("\\.");

String suffix= split[1];//检查图片格式

boolean imgTypeIsRight =checkImgType(suffix);if (!imgTypeIsRight){

String error= fileResponse.error(0, "无效状态异常");

out.println(error);

}

String fileName= UUID.randomUUID().toString() +"."+suffix;

String path= filePath +fileName;

File tempFile= null;try{

tempFile= newFile(path);

FileUtils.copyInputStreamToFile(file.getInputStream(), tempFile);

}catch(IllegalStateException e) {

String error= fileResponse.error(0, "无效状态异常");

out.println(error);return;

}catch(IOException e) {

String error= fileResponse.error(0, "IO异常");

out.println(error);return;

}

String success= fileResponse.success(1, fileName, WEB_PIC_HOST+fileName, "");

out.print(success);

}catch(IOException ex){return;

}

}//ckEditor图片上传响应体

public class FileResponse extends HashMap{

Map msgMap = new HashMap<>();public String error(intcode, String msg){

FileResponse result= newFileResponse();

msgMap.put("message",msg);

result.put("uploaded",code);

result.put("error",msgMap);returnJSONObject.toJSON(result).toString();

}public String success(intcode, String fileName,String url,String msg){

FileResponse result= newFileResponse();if(!StringUtils.isEmpty(msg)){

msgMap.put("message",msg);

result.put("error",msgMap);

}

result.put("uploaded",code);

result.put("fileName",fileName);

result.put("url",url);returnJSONObject.toJSON(result).toString();

}

}//检查图片格式

private booleancheckImgType(String suffix){if (!"jpg".equals(suffix)&&!"jpeg".equals(suffix)&&!"png".equals(suffix)&&!"gif".equals(suffix)){return false;

}else{return true;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值