一、关于配置之类的网上很多了,我就不赘述了
二、我的配置Simditor
1、在界面中配置Simditor
function initEditor() {
var editor = new Simditor({
textarea: $('#editor'),
// [ 'bold','italic','underline','strikethrough','fontScale','color','ol','ul' ,'blockquote','code' ,'table','link','image','hr','indent','outdent','alignment'],
toolbar: ['bold', 'italic', 'underline', 'strikethrough', 'fontScale', 'color', '|', 'ol', 'ul', 'blockquote', 'code', 'link', 'image', 'hr', 'alignment', 'indent', 'outdent', 'alignment'],
toolbarFloat: true,
upload: {
url: 'url', //文件上传的接口地址
params: null, //键值对,指定文件上传接口的额外参数,上传的时候随文件一起提交
fileKey: 'fileDataFileName', //服务器端获取文件数据的参数名
connectionCount: 3,
leaveConfirm: '正在上传文件'
}
});
}
2、我们打开Simditor.js 然后 根据appendTo($uploadBtn);或者uploadImage 这个来找到如下代码
return _this.input = $('<input/>', {
type: 'file',
title: _this._t('uploadImage'),
multiple: true,
name:'fileData',
accept: 'image/gif,image/jpeg,image/jpg,image/png,image/svg'
}).appendTo($uploadBtn);
name:'fileData',
accept: 'image/gif,image/jpeg,image/jpg,image/png,image/svg'
}).appendTo($uploadBtn);
注意:我是用 uploadImage 搜索的,出现这样代码的都添加入标红色的代码,这个是为了给后台拿到文件数据
2、完成上面的就基本完成界面上的配置了,下面就看后台吧
(1)、Strust 接口配置
<action name="img" class="UploadAction" method="imgUpload">
<interceptor-ref name="defaultStack"/>
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/jpeg,image/jpg,image/gif</param>
</interceptor-ref>
<result name="success" type="json">
<param name="root">msg</param>
</result>
</action>
(2)、接口文件 (下面diamante是参照网上大神的代码搞得,忘记看谁的了,借用借用)
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionContext;
import com.wulias.base.BaseAction;
import com.wulias.base.BaseDAO;
public class UploadAction extends BaseAction{
private static final long serialVersionUID = 1L;
private String err = "";
private File fileData; //上传文件
private String fileDataFileName; //文件名
public String imgUpload() throws Exception{
//获取response、request对象
ActionContext ac = ActionContext.getContext();
HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
response.setContentType("text/html;charset=gbk");
PrintWriter out = null;
try {
out = response.getWriter();
} catch (IOException e1) {
e1.printStackTrace();
}
String saveRealFilePath = ServletActionContext.getServletContext().getRealPath("/upload");
System.out.println(saveRealFilePath);
System.out.println(fileDataFileName);
System.out.println(fileData==null);
File fileDir = new File(saveRealFilePath);
if (!fileDir.exists()) { //如果不存在 则创建
fileDir.mkdirs();
}
File savefile;
savefile = new File(saveRealFilePath + "/" + fileDataFileName);
try {
FileUtils.copyFile(fileData, savefile);
} catch (IOException e) {
err = "错误"+e.getMessage();
e.printStackTrace();
}
String file_Name = request.getContextPath() + "/upload/" + fileDataFileName;
msg = "{\"success\":\"" + true + "\",\"file_path\":\"" + file_Name + "\"}";
return SUCCESS_STRING;
}
public String getErr() {
return err;
}
public void setErr(String err) {
this.err = err;
}
public File getFileData() {
return fileData;
}
public void setFileData(File fileData) {
this.fileData = fileData;
}
public String getFileDataFileName() {
return fileDataFileName;
}
public void setFileDataFileName(String fileDataFileName) {
this.fileDataFileName = fileDataFileName;
}
public static long getSerialversionuid() {
return serialVersionUID;
}
}
注意:BaseAction就是封装一些通用的字符串和属性 比如 msg 记得get\set
好了,只要你项目没问题的话一般都能上传图片了的