您的位置:首页 > 产品设计 > UI/UE

百度Ueditor改变图片上传路径

2017-03-06 16:32 941 查看
首先,重写这个东西,然后再实例化编辑器



然后

写上传下载的代码:

package com.wzz.easy.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.wzz.easy.util.IDUtil;

@Controller
public class ImageController {

@RequestMapping("image/upload")
@ResponseBody
public String get(@RequestParam("upfile") CommonsMultipartFile upfile, HttpServletRequest req, HttpServletResponse response) {

/*
* {"state": "SUCCESS",
* "original": "QQ\u622a\u56fe20170303145647.png",
* "size": "121571",
* "title": "1488529026610076909.png",
* "type": ".png",
* "url": "/ueditor/jsp/upload/image/20170303/1488529026610076909.png"}
*/
// 返回结果信息(UEditor需要)
//Map<String, String> map = new HashMap<String, String>();
try {
System.out.println("上传...");

req.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
//response.setContentType("text/json");

String fileName = upfile.getFileItem().getName();
System.out.println("fileName:"+fileName);
String newName = IDUtil.getStringID()+fileName.substring(upfile.getOriginalFilename().lastIndexOf("."));
System.out.println("newName:"+newName);

InputStream is = upfile.getInputStream();
byte[] b = new byte[is.available()];
is.read(b);
FileOutputStream fos = new FileOutputStream(new File("E://uploadImage/" + newName));
fos.write(b);
fos.flush();
fos.close();

System.out.println(upfile.getFileItem().getName());
System.out.println(is);
String name = req.getParameter("name");
System.out.println(name);

JSONObject map = new JSONObject();
// 是否上传成功
map.put("state", "SUCCESS");
// 现在文件名称
map.put("title", newName);
// 文件原名称
map.put("original", fileName);
// 文件类型 .+后缀名
map.put("type", fileName.substring(upfile.getOriginalFilename().lastIndexOf(".")));
// 文件路径
map.put("url", "image/down?name=" + newName);
// 文件大小(字节数)
map.put("size", upfile.getSize() + "");

JSONObject o = new JSONObject();

return map.toString();

} catch (IOException e) {
e.printStackTrace();
}
return "";
}

@RequestMapping("image/down")
public void downLoad(String name, HttpServletRequest request, HttpServletResponse response) {

System.out.println("下载...");
System.out.println(name);

response.setHeader("Content-Disposition", "attachment; filename=" + name);
response.setContentType("application/octet-stream; charset=utf-8");
try {
ServletOutputStream os = response.getOutputStream();

String path = "E://uploadImage/" + name;

FileInputStream fis = new FileInputStream(new File(path));
byte[] b = new byte[20];
while (fis.read(b) != -1) {
os.write(b);
}
os.flush();
fis.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  百度 编辑器 图片
相关文章推荐