layui富文本编辑器添加图片回显问题详解

“src”: “图片路径”,

“title”:  “图片名称”    //可选

}

}

所以后台返回需要返回这几个参数

//上传

@RequestMapping(value=“/upload”)

@ResponseBody

public Object upload(MultipartFile file) {

String filename = file.getOriginalFilename();

String uuid = UUID.randomUUID().toString();

//上传的方法在Service层写的,这里直接调用的

boolean boole = pService.savefile(file, uuid);

if (boole) {

Map<String,Object> map = new HashMap<String,Object>();

Map<String,Object> map2 = new HashMap<String,Object>();

map.put(“code”, 0); //0表示上传成功

map.put(“msg”,“上传成功”); //提示消息

//src返回图片上传成功后的下载路径,这里直接给绝对路径

map2.put(“src”, “http://localhost/layUITextarea/download?uuid=”+uuid);

map2.put(“title”, filename);

map.put(“data”, map2);

return map;

} else {

return new AjaxResult(true, file.getOriginalFilename());

}

}

//下载方法

@RequestMapping(value = “/download”)

@ResponseBody

private void download(String uuid, HttpServletRequest request, HttpServletResponse response) {

fileService.download(uuid, request, response);

}

Service层上传下载部分代码。

@Service

public class FileService extends CommonService<FileImg, Integer> {

@Autowired

private FileRepository fileRepository;

// 图片存放位置

private final static String IMAGEPATH=“e:\layui\image”;

//保存图片

@Transactional

public boolean savefile(MultipartFile file, String uuid){

try{

File path = path(file.getContentType());

String filename = file.getOriginalFilename();

FileImg fileEntity = new FileImg();

SimpleDateFormat sdf=new SimpleDateFormat(“yyyy-MM-dd HH:mm:ss”);

Date date=new Date();

fileEntity.setFileName(filename);

fileEntity.setUuid(uuid);

String storeaddress = path.getAbsolutePath();

fileEntity.setStoreaddress(storeaddress);

File saveFile = new File(path,uuid);

try {

fileRepository.save(fileEntity);

file.transferTo(saveFile);

return true;

} catch (IllegalStateException | IOException e) {

e.printStackTrace();

return false;

}

}catch (Exception e){

System.out.println(“图片保存异常”);

return false;

}

}

//图片地址是否存在

private File path(String filename) {

File pat=new File(“e:\layui”);

File path = new File(FileService.IMAGEPATH);

if(!pat.isDirectory()) {

pat.mkdir();

}

if(!path.isDirectory()) {

path.mkdir();

}

return path;

}

/**

  • 下载

  • @param uuid

  • @param request

  • @param response

*/

public void download(String uuid, HttpServletRequest request, HttpServletResponse response) {

FileImg fileentity = fileRepository.findByUuid(uuid);

String filename = fileentity.getFileName();

filename = getStr(request, filename);

File file = new File(fileentity.getStoreaddress(), uuid);

if(file.exists()) {

FileInputStream fis;

try {

fis = new FileInputStream(file);

response.setContentType(“application/x-msdownload”);

response.addHeader(“Content-Disposition”, “attachment; filename=” + filename );

ServletOutputStream out = response.getOutputStream();

byte[] buf=new byte[2048];

int n=0;

while((n=fis.read(buf))!=-1){

out.write(buf, 0, n);

}

fis.close();

out.flush();

out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

private String getStr(HttpServletRequest request, String fileName) {

String downloadFileName = null;

String agent = request.getHeader(“USER-AGENT”);

try {

if(agent != null && agent.toLowerCase().indexOf(“firefox”) > 0){

//downloadFileName = “=?UTF-8?B?” + (new String(Base64Utils.encode(fileName.getBytes(“UTF-8”)))) + “?=”;

//设置字符集

downloadFileName = “=?UTF-8?B?” + Base64Utils.encodeToString(fileName.getBytes(“UTF-8”)) + “?=”;

}else{

downloadFileName = java.net.URLEncoder.encode(fileName, “UTF-8”);

}

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

}

return downloadFileName;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值