界面
<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,不是好方法,还没有深入研究)