jsp
<script>
layui.use("upload",function(){
var upload = layui.upload;
var uploadInst = upload.render({
elem: '#test1'
,url: '/jyals/upload/images'
,before: function(obj){
obj.preview(function(index, file, result){
$('#demo').attr('src', result);
});
}
,done: function(res){
if (res.code == 0) {
layer.msg("上传成功!");
$("#imageName").val(res.fileId);
$("#picId").val(res.fileName);
$("#picName").val("gktp");
$("#picAdress").val(res.filePath);
$("#picType").val(res.fileType);
console.log(res.filePath);
}else{
layer.msg("上传失败!");
}
}
,error: function(){
var demoText = $('#demoText');
demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
demoText.find('.demo-reload').on('click', function(){
uploadInst.upload();
});
}
});
});
</script>
upload
@RequestMapping(value="/images",method = RequestMethod.POST)
@ResponseBody
public JSONObject uploadImage(MultipartFile file,HttpServletRequest request,HttpSession session){
/**
* 仅服务器端保存文件,数据库端由表单上传具体处理保存
*/
JSONObject json = new JSONObject();
String orPath = request.getSession().getServletContext().getRealPath("/opt/tomcat/lbpic/");
try{
json = baseServiceImpl.uploadlbFile(file, orPath,session,request);
json.put("code", 0);
}catch (Exception e) {
json.put("code", 1);
e.printStackTrace();
}
return json;
}
layui
layedit.set({
uploadImage: {
url: '/jyals/upload/imgs'
,type: 'post'
,done : function(res, index, upload) {
if (res.code == 0) {
layer.msg("上传成功!");
$("#imageName").val(res.fileId);
console.log(res.filePath);
} else {
layer.msg("上传失败!");
$('#tpyl').html("");
}
}
}
});
var editIndex = layedit.build('LAY_demo_editor');
@RequestMapping(value="/imgs",method = RequestMethod.POST)
@ResponseBody
public JSONObject uploadImgs(MultipartFile file,HttpServletRequest request,HttpSession session){
/**
* 仅服务器端保存文件,数据库端由表单上传具体处理保存
*/
JSONObject json = new JSONObject();
JSONObject json1 = new JSONObject();
String orPath = request.getSession().getServletContext().getRealPath("/opt/tomcat/pic/");
try{
json.put("code", 0);
json1=baseServiceImpl.uploadFile(file, orPath,session,request);
JSONObject data = new JSONObject();
String filename = json1.getString("fileName");
String name = filename.substring(0,16);
String path = "http:/jyals/uploadImages/";
data.put("src",path+filename);
json.put("data", data);
}catch (Exception e) {
json.put("code", 1);
json.put("msg", "fail");
e.printStackTrace();
}
return json;
}
}
BaseServiceImpl
public JSONObject uploadlbFile(MultipartFile file, String path, HttpSession session, HttpServletRequest request)
throws Exception {
// 生成一个文件名16位数字+字母
String fileRandomNameId = Integer.toHexString(new Random().nextInt())
fileRandomNameId += Integer.toHexString(new Random().nextInt())
String fileRandomName = Integer.toHexString(new Random().nextInt())
fileRandomName += Integer.toHexString(new Random().nextInt())
String realName = file.getOriginalFilename()
String suffixName = realName.substring(realName.lastIndexOf("."))
fileRandomName += suffixName
path = "/opt/tomcat/lbpic/"
File tempFile = new java.io.File(path, fileRandomName)
if (!tempFile.getParentFile().exists()) {
tempFile.getParentFile().mkdirs()
}
if (tempFile.exists()) {
tempFile.delete()
}
// tempFile.createNewFile()
// file.transferTo(tempFile)
session = request.getSession()
session.setAttribute("picname", tempFile)
session.setAttribute("multpart", file)
session.setAttribute("path", path)
session.setAttribute("fileName", fileRandomName)
// 封装文件的信息
JSONObject fileInfor = new JSONObject()
fileInfor.put("fileId", fileRandomNameId)
fileInfor.put("fileName", fileRandomName)
fileInfor.put("fileType", suffixName)
fileInfor.put("filePath", tempFile.getAbsolutePath())
return fileInfor
}