使用ssi框架+layui写的项目,其中用到了layui的富文本,图片上传接口怎么也写不出来,各位神仙请帮帮我
js:
layer = layui.layer;
table = layui.table;
var layedit = layui.layedit;
layedit.set({
uploadImage: {
url: ctx + '/jszjh/jszjhfbgl!srcUrl.do?' //接口url
,type: 'post' //默认post
}
});
var layidx = layedit.build('wznr',{
height: 700}); //建立编辑器
jsp:
<textarea id="wznr" name="wznr" style="display: none;"></textarea>
后台Action不知道怎么写
拿走不谢
public String srcUrl(){
Map map = new HashMap();
Map mapSrc = new HashMap();
map.put("code", 1); //0表示上传成功
map.put("msg","上传失败"); //提示消息
mapSrc.put("src", "");
mapSrc.put("title", "");
map.put("data", mapSrc);
String rootPath = request.getSession().getServletContext().getContextPath();//根目录
try{
if(file==null){
responseJson(map);
return null;
}
long maxSize = (1024 * 1024) * 10;
long fileSize = file.length();
String size = this.getFileSize(fileSize);
if (fileSize > maxSize) {
map.put("msg","上传失败,资源信息最大上传10MB,当前文件" + size + "!"); //提示消息
responseJson(map);
return null;
}
String path="C:\\Users\\ASUS\\Desktop\\jszjh";
// String path=Constants.JSZJHFBGLPATH;
File p = new File(path);
if(!p.exists()) p.mkdirs();
String nbwjm = parseWjFile(file, fileFileName,path);
map.put("code", 0); //0表示上传成功
map.put("msg","上传成功"); //提示消息
mapSrc.put("src", rootPath+"/province/other/jszjh/jsp/pcCreate.jsp?pth="+path+ File.separator + nbwjm);
mapSrc.put("title", nbwjm);
map.put("data", mapSrc);
responseJson(map);
return null;
}catch(Exception x){
responseJson(map);
return null;
}
}
// 获取文件大小
public String getFileSize(long fileLength) {
String size = "0";
double len = fileLength;
DecimalFormat df = new DecimalFormat("0.0");
if (len > 0.1 * 1024 * 1024) {
size = df.format(len / (1024 * 1024)) + "MB";
} else if (len > 0.1 * 1024) {
size = df.format(len / 1024) + "KB";
} else {
size = len + "B";
}
return size;
}
/**
* 复制文件
*/
public String parseWjFile(File file, String wjmc, String filePath) {
// 新文件
File dir = new File(filePath);
if (!dir.exists()) {
dir.mkdirs();
}
String wjid = System.currentTimeMillis() + wjmc;
File uploadCopyWord = new File(filePath + File.separator + wjid);
FileUtil.copyFile(file, uploadCopyWord);
return wjid;
}
srcUrl这个传入request的请求,从请求里获取文件数据,保存即可
参考代码:
String path=ServletActionContext.getServletContext().getRealPath("/upload");
try {
for(int i=0;i<file.length;i++)
FileUtils.copyFile(file[i], new File(path,fileFileName[i]));
} catch (IOException e) {
e.printStackTrace();
}
return Action.SUCCESS;