百度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();
}
}
}
然后
写上传下载的代码:
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();
}
}
}
相关文章推荐
- 百度UEditor图片文件改变默认保存到项目根路径,自定义上传路径或远程服务器
- 关于百度world 编辑器改变上传图片的保存路径图片不显示的问题
- 百度编辑器(UEditor)调用上传图片、上传文件等模块,并修改图片、文件的保存路径
- [置顶] 百度ueditor编辑器将图片上传到非项目根路径(保存在磁盘上)或独立服务器解决方案
- 百度ueditor富文本--图片保存路径的配置以及上传到远程服务器
- 百度控件ueditor上传图片到指定路径(即图片存储在服务器外面,非工程目录下)
- 百度Ueditor 图片上传路径配置
- 百度UEditor图片上传或文件上传路径自定义
- 织梦使用百度Ueditor编辑器上传图片自动生成绝对路径实现方法(测试可用)
- MVC3.0下使用百度富文本编辑器ueditor1.3.6上传图片
- 百度ueditor组件上传图片后如何设置img里的alt属性
- dedecms整合百度编辑器(Ueditor)之上传图片加水印的办法
- ASP.NET配置Ueditor编辑器上传图片路径
- 百度Ueditor多图片上传控件
- 百度富文本编辑器UEditor1.3上传图片附件等
- 百度ueditor 1.4.3 jsp开发版简单配置及图片上传
- MVC使用百度开源文本编辑器UEditor实现图文并茂,字数限制,上传图片或涂鸦
- 百度在线编辑器UEditor(v1.3.6) .net环境下详细配置教程之更改图片和附件上传路径
- MVC使用百度开源文本编辑器UEditor实现图文并茂,字数限制,上传图片或涂鸦
- 百度Ueditor在线编辑器的配置和图片上传