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();

你可能感兴趣的:(jersey)