• dojo + jersey 上传图片到数据库


    dojo + jersey 上传图片到数据库

    很气愤的一件事,我刚刚写好的一篇博客,点击提交时,博客园的程序报错!白写了!现在大家看到这篇是减缩版,代码是可以使用的,只是有些解释型语言,我不想在重复一遍了,希望博客园的管理者好好管理,不要再出现类似问题了。

    使用jersey发布上传图片服务需要依赖两个jar包:jersey-multiart.jar,mimepull.jar

    前端html:

    复制代码
    <form id="messageform" class="messagetable" enctype="multipart/form-data" method="post" >
            <div class="messagebody">
                <textarea name="message" rows="5" cols="5" data-dojo-attach-point="messageNode"></textarea>
                <input type="hidden" name="ids" id="checkedids">
                <input type="hidden" name="terminalid" id="terminalid">
            </div>
            <input name="file" type="file" accept="image/*" single name="file" data-dojo-attach-point="fileNode" data-dojo-attach-event="onchange: onFileLoad">
            <img data-dojo-attach-point="imgNode" src="" >
            <input type="submit" value="上传图片" data-dojo-attach-event="onclick:onUploadImg">
            <input type="button" value="发送" data-dojo-attach-event="onclick:onSendMessage">
        </form>
    复制代码

    javascript:

    复制代码
    uploadImg: function(form){
                        var myhostname = "http://" + window.location.hostname + ":"
                        + window.location.port;
                        var url = myhostname + "/TrackingSys/services/ConductControl/uploadImg";
                        
                        form.ids = this.getStrIds();
                        console.log(form);
                        iframe(url, {
                            form: form,
                            //url: url,
                            method: 'POST',
                            handleAs: "html"
                        }).then(lang.hitch(this, 'uploadSuccess'), lang.hitch(this, 'err'));
                    },
                    
                    uploadSuccess: function(res){
                        var contentNode = res.childNodes[0];
                        if (contentNode){
                            var content = contentNode.textContent? contentNode.textContent : contentNode.innerText;
                            var data = JSON.parse(content);
                            console.log(data);
                        }
                    },
    复制代码

    后台:注意返回的格式

    复制代码
    @POST @Path("/uploadImg")
        @Consumes(MediaType.MULTIPART_FORM_DATA)  // 消费注解必须是这个类型
        @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
        public String uploadImg(@FormDataParam("file") InputStream uploadedStream,
                @FormDataParam("ids") String ids, @FormDataParam("terminalid") String terminalid){
        
            String[] checkIds = ids.split(",");
            String result = dao.sendImgToTerminal(terminalid, uploadedStream, checkIds);
            String re = "<html>{\"result\":\"" + result + "\"}</html>";
            return re;
        }
    复制代码

    插入图片部分:不能使用被注释掉的那种写法

    复制代码
    rpt = conn.prepareStatement(sql);
            BufferedInputStream bs = new BufferedInputStream(picData);
            ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
            
            byte[] bytes = new byte[1024];
            int len=picData.read(bytes);
            while (len != -1) {
                outputStream.write(bytes, 0, len);
                len=picData.read(bytes);
            }
            rpt.setBytes(1, outputStream.toByteArray());
            //rpt.setBinaryStream(1, picData,picData.available() );
            rpt.setInt(2, imageId);
            rpt.executeUpdate();
    复制代码
     
     
     
    标签: dojojersey
  • 相关阅读:
    python开发第一篇:初识python
    python开发第二篇 :python基础
    nfs下的exportfs命令和nfs客户端重新挂载
    centos7安装epel源
    vim查找替换
    keepalive基础知识
    nginx基本配置各个参数说明
    centos7下搭建nfs服务
    systemctl命令
    mariadb入门
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/2985520.html
Copyright © 2020-2023  润新知