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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM