Spring Boot集成xhEditor

本文介绍了一个基于Xheditor实现的富文本编辑器的图片上传功能,并详细说明了如何解决在集成Spring Security时遇到的问题,包括X-Frame-Options设置导致的显示问题及404错误。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

界面

<textarea id="md"></textarea>
<script type="text/javascript">
	$(function(){
		$("#md").xheditor({
		     tools:'simple',
		     skin:'nostyle',
		     upMultiple:true,
		     upImgUrl:'/upload',
		     upImgExt:"jpg,jpeg,gif,bmp,png",
		     onUpload:insertUpload,
		     html5Upload:false
	});
		
        function insertUpload(msg) {
            var _msg = msg.toString();
            var _picture_name = _msg.substring(_msg
                    .lastIndexOf("/") + 1);
            var _picture_path = Substring(_msg);
            $("#md").append(_msg);
        }
		
        function Substring(s) {
            return s.substring(s.substring(0,
                    s.lastIndexOf("/")).lastIndexOf("/"),
                    s.length);
        }
    })
</script>

Controller

@Controller
public class XheditorImgUpload {

	@RequestMapping(value = "/upload", method = RequestMethod.POST)
	@ResponseBody
	public String imgUpload(HttpServletRequest request,
			@RequestParam MultipartFile filedata) {

		String filedataFileName = filedata.getOriginalFilename();
		
		String path = request.getSession().getServletContext()
				.getRealPath("imgupload/");

		String newFileName = UUID.randomUUID().toString()
				+ filedataFileName.substring(filedataFileName.indexOf("."),
						filedataFileName.length());
		String message;
		String err = "";
		String msg = "/imgupload/" + newFileName;

		try {
			uploadFile(filedata.getBytes(), path, newFileName);
		} catch (Exception e) {
		    err = e.getMessage();
		}
		
		message = "{\"err\":\"" + err + "\",\"msg\":\"" + msg + "\"}";
		err = message;
		return message;
	}
	
	public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception { 
        File targetFile = new File(filePath);  
        if(!targetFile.exists()){    
            targetFile.mkdirs();    
        }
        BufferedOutputStream bos=new BufferedOutputStream(new FileOutputStream(filePath+fileName));
        bos.write(file);
        bos.flush();
        bos.close();
    }
}

集成Spring Security碰到的问题

Refused to display 'http://....' in a frame because it set 'X-Frame-Options' to 'deny'.
在security的配置bean中,在configure方法中添加   http.headers().frameOptions().disable();

上传报404
在security的配置bean中,在configure方法中添加   csrf().disable();(这个方法关闭了csrf,不是好方法,还没有深入研究)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值