http上傳

private void httpUpload() {
        
        //定義HttpClient對象
        HttpClient client = new DefaultHttpClient();
        //獲得HttpPost對象
        HttpPost post = new HttpPost("http://192.168.1.106:8001/2012/upload.php");
        post.addHeader("charset", HTTP.UTF_8);  
        //實例化
        MultipartEntity me = new MultipartEntity();
        
        try {
            
            me.addPart("content",new StringBody("12cccafasdfasdf"));
            me.addPart("title",new StringBody("csdnliwei"));
            me.addPart("local",new StringBody("beijing"));
            //設置流文件
            me.addPart("file", new InputStreamBody(new FileInputStream("/mnt/sdcard/test.jpg"), "image/pjpeg", "fengjie.jpg"));
            
            post.setEntity(me);
            //獲得響應消息
            HttpResponse resp = client.execute(post);
            
            if(resp.getStatusLine().getStatusCode()==200){
                
                Toast.makeText(this, "文件上傳文成!", 1).show();
                
            }
            
        } catch (Exception e) {
            
            e.printStackTrace();
        }
        
    }
發佈了34 篇原創文章 · 獲贊 115 · 訪問量 33萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章