Springboot 使用wangEditor3.0上传图片

wangEditor是一个很好用的富文本编辑器,可以上传图片到服务器上,存放位置为Tomcat目录下

Springboot启动时自带Tomcat,重新启动时以前上传的图片便会丢失

研究了一下,给出一种可行性方法,思路就是每次上传图片至Tomcat时复制同名图片到指定文件夹中,下次启动时从指定文件夹复制图片到Tomcat中

如果大家有更好的方法不吝赐教

开发工具STS,Springboot2.0

JS代码

<div id="div1" style="left:15%;width:70%;position:absolute;">
    <p>欢迎使用 wangEditor 编辑器</p>
</div>
<button id="btn1">获取html</button>
<button id="btn2">获取text</button>
<script type="text/javascript" src="/wangEditor.min.js"></script>
<script type="text/javascript">
    var E = window.wangEditor
    var editor = new E('#div1')
    editor.customConfig.debug=true;
    editor.customConfig.uploadImgServer = '/upload'
    editor.customConfig.uploadFileName = 'myFileName';
    editor.customConfig.debug = true
    editor.create()
    
	    document.getElementById('btn1').addEventListener('click', function () {
        // 读取 html
    }, false)
    document.getElementById('btn2').addEventListener('click', function () {
        // 读取 text
        alert(editor.txt.text())
    }, false)
</script>

  Controller

   @RequestMapping("/upload")
	public Map upload(HttpServletRequest request,@RequestParam(value="myFileName")MultipartFile file) throws IllegalStateException, IOException {
		String pathRoot = request.getSession().getServletContext().getRealPath("upload"); 
		String fileName = file.getOriginalFilename();//获取图片名称
		String path = pathRoot+"\\"+fileName;
		System.out.println(path);
		  File dest = new File(path);
  	    // 检测是否存在目录
  	    if (!dest.getParentFile().exists()) {
  	        dest.getParentFile().mkdirs();// 新建文件夹
  	    }
  	    file.transferTo(dest);// 文件写入
  	    
		String path2="D:\\aim\\upload"+"\\"+fileName;
		UtilTools.copyFile(path, path2);
    	Map map=new HashMap();
    	map.put("errno", 0);
    	path="http://localhost:8069/upload/"+fileName+"";
    	List list=new ArrayList();
    	list.add(path);
    	map.put("data", list);
    
    	
    	
    	return map;
    	
	}

  工具类

package com.ggr.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

public class UtilTools {
	
	/** 
	* 复制单个文件 
	* @param oldPath String 原文件路径 如:c:/fqf.txt 
	* @param newPath String 复制后路径 如:f:/fqf.txt 
	* @return boolean 
	*/ 
	public static void copyFile(String oldPath, String newPath) { 
	try { 
	int bytesum = 0; 
	int byteread = 0; 
	File oldfile = new File(oldPath); 
	if (oldfile.exists()) { //文件存在时 
	InputStream inStream = new FileInputStream(oldPath); //读入原文件 
	FileOutputStream fs = new FileOutputStream(newPath); 
	byte[] buffer = new byte[1444]; 
	int length; 
	while ( (byteread = inStream.read(buffer)) != -1) { 
	bytesum += byteread; //字节数 文件大小 
	System.out.println(bytesum); 
	fs.write(buffer, 0, byteread); 
	} 
	inStream.close(); 
	} 
	} 
	catch (Exception e) { 
	System.out.println("复制单个文件操作出错"); 
	e.printStackTrace(); 

	} 

	} 

	public static void copyFolder(String oldPath, String newPath) { 

		try { 
		(new File(newPath)).mkdirs(); //如果文件夹不存在 则建立新文件夹 
		File a=new File(oldPath); 
		String[] file=a.list(); 
		File temp=null; 
		for (int i = 0; i < file.length; i++) { 
		if(oldPath.endsWith(File.separator)){ 
		temp=new File(oldPath+file[i]); 
		} 
		else{ 
		temp=new File(oldPath+File.separator+file[i]); 
		} 

		if(temp.isFile()){ 
		FileInputStream input = new FileInputStream(temp); 
		FileOutputStream output = new FileOutputStream(newPath + "/" + 
		(temp.getName()).toString()); 
		byte[] b = new byte[1024 * 5]; 
		int len; 
		while ( (len = input.read(b)) != -1) { 
		output.write(b, 0, len); 
		} 
		output.flush(); 
		output.close(); 
		input.close(); 
		} 
		if(temp.isDirectory()){//如果是子文件夹 
		copyFolder(oldPath+"/"+file[i],newPath+"/"+file[i]); 
		} 
		} 
		} 
		catch (Exception e) { 
		System.out.println("复制整个文件夹内容操作出错"); 
		e.printStackTrace(); 

		} 
	}
}

  Listener

package com.ggr.listener;

import java.io.File;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.annotation.WebListener;
import com.ggr.util.UtilTools;

/*
 * 注册监听器
 * 监听文件
 */
@WebListener
public class BolgContextListener implements ServletContextListener{

	@Override
	public void contextInitialized(ServletContextEvent sce) {
		String path1=sce.getServletContext().getRealPath("upload");
		String path2="D:\\aim\\upload";
		File path1File=new File(path1);
		File path2File=new File(path2);
		if(path2File.exists()){
			System.out.println("复制文件数量:"+path2File.list().length);;
			UtilTools.copyFolder(path2, path1);
		}else{
			path2File.mkdirs();
		}
		System.out.println("path1:"+path1);
		System.out.println("path2:"+path2);
		System.out.println("Context监听器注册");		
	}

	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		System.out.println("Context监听器销毁");
	}

}

  

posted @ 2018-04-26 09:23  思齐博客  阅读(618)  评论(0编辑  收藏  举报