前台代码:
<script type="text/javascript" src="/static/plugins/wangEditor.min.js"></script>
<div id="editor">
</div>
<script type="text/javascript">
var E = window.wangEditor
var editor = new E('#editor')
editor.customConfig.showLinkImg = false
editor.customConfig.uploadFileName = 'myFileName'
editor.customConfig.uploadImgServer = '/backstage/payauto/upload'
editor.customConfig.uploadImgHooks = {
customInsert: function (insertImg, result, editor) {
var url =result.data;
insertImg(url);
}
}
editor.create()
</script>
后台代码:
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
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.MultipartFile;
/**
* @author zombie
*/
@Controller
@RequestMapping("/backstage/payauto/")
public class PayautoController {
@RequestMapping("upload")
@ResponseBody
public Map<String, String> upload(@RequestParam(value="myFileName") MultipartFile file, HttpServletRequest request) {
Map<String, String> map = new HashMap<String, String>();
String separator = System.getProperty("file.separator");
// 用于前端图片显示的路径 http://localhost:8080/upload/
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath()
+ separator +"upload" + separator;
// 用于保存图片至项目的路径 D:\_eclipsework\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps\JYSystem\ upload\
// 或者 String uploadDir = request.getSession().getServletContext().getRealPath("upload") + separator;
String uploadDir = ProjectPath.getProjectPath() + separator +"upload" + separator;
byte[] bytes = null;
try {
bytes = file.getBytes();
File dirPath = new File(uploadDir);
if (!dirPath.exists()) {
if (!dirPath.mkdirs()) {
}
}
/**
* 构建新的图片名称
*/
String fileName = file.getOriginalFilename();
int index = fileName.lastIndexOf(".");
String extName = index > -1 ? fileName.substring(index) : ""; // .jpg
String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
String newFileName = uuid + extName;
/**
* 保存图片至项目
*/
String filePath = uploadDir + newFileName;
File descFile = new File(filePath);
FileCopyUtils.copy(bytes, descFile);
map.put("data", basePath + newFileName);
} catch (IOException e) {
e.printStackTrace();
}
return map;
}
}
ProjectPath.java
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
public class ProjectPath {
private static String rootPath = "";
private ProjectPath() {
init();
}
private static void init() {
String path = ProjectPath.class.getResource("ProjectPath.class")
.toString();
try {
path = URLDecoder.decode(path, "utf-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
path.replaceAll("\\\\", "/");
int index = path.indexOf("WEB-INF");
if (index == -1) {
index = path.indexOf("bin");
}
if (index == -1) {
index = path.indexOf("lib");
}
if (index == -1) {
int index2 = path.indexOf("jar!");
if (index2 != -1) {
path = path.substring(0, index2);
index = path.lastIndexOf("/");
}
}
path = path.substring(0, index);
if (path.startsWith("jar")) {
path = path.substring(9);
}
if (path.startsWith("file")) {
path = path.substring(5);
}
if (path.endsWith("/") || path.endsWith("\\")) {
path = path.substring(0, path.length() - 1);
}
// linux环境下第一个/是需要的
String os = System.getProperty("os.name").toLowerCase();
if (os.startsWith("win")) {
path = path.substring(1);
}
rootPath = path;
}
/**
* 获取应用的根目录,路径分隔符为/,路径结尾无/
*
* @return
*/
public static String getProjectPath() {
if ("".equals(rootPath)) {
init();
}
return rootPath;
}
}