xheditor上传图片到服务器

<script type="text/javascript">
//富文本编辑器初始化
    $('#msgWriteAddMsgContent').xheditor({tools:'mini', html5Upload:false,
upMultiple:'1',upImgUrl:"/msg/msgWriteUploadImage.action",
upImgExt:"jpg,jpeg,gif,png",onUpload:insertUpload});
    function insertUpload(arrMsg){
        console.log(arrMsg);
        var i,msg;
        for(i=0;i<arrMsg.length;i++)
        {
            msg=arrMsg[i];
            $("#xheImgUrl").append(msg);
            $("#imageUrl").attr("value",msg);
        }  

    }

 
</script>
<textarea id="msgWriteAddMsgContent" name="msgContent" cols="61" rows="14" style="width: 550px" maxlength="1000"></textarea>
 
在某些情况下xheditor编辑器在第一次可以编辑,但是第二次的时候不能编辑,比如用jquery-UI弹窗中的编辑器就可能会出现此问题,解决方法是在关闭弹窗时用 $( "#elm" ).xheditor( false );将xheditor编辑器删除,下次弹窗时重新加载
public void msgWriteUploadImage(){
        if (null!=filedata) {
            try {
                long fileName=System.currentTimeMillis();
                String uploadPath = ServletActionContext.getServletContext().getRealPath("/");//项目在tomcat服务器本地部署路径,文件需要有硬盘绝对路径才可存放,并不是workspace的路径
                InputStream is=new FileInputStream(filedata);
                File outFile = new File(uploadPath+"/uploadImg/"+fileName+".jpg");// 输出文件 
                String outPathString=outFile.getAbsolutePath();
                System.out.println(uploadPath+"/uploadImg/");
                OutputStream os = new FileOutputStream(outFile);
                byte[] buffer = new byte[1024];  
                int len = 0; 
                while ((len=is.read(buffer))!=-1) {
                    os.write(buffer,0,len);
                }
                is.close();
                os.close();
                response.setCharacterEncoding("utf-8");
                PrintWriter out=response.getWriter();
                out.println("{'err':'','msg':'"+request.getContextPath()+"/uploadImg/"+fileName+".jpg'}");//取项目相对路径
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            PrintWriter out;
            try {
                response.setCharacterEncoding("utf-8");
                out = response.getWriter();
                out.println("{'err':'上传文件为空!!','msg':''}");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值