springMVC wangeditor上传图片

前台代码:

<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;  
    }  

}  
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值