2014-11-08 142 views
1

我正在開發一個可以將視頻發送到php服務器的java應用程序。 我已經在android上完成了它,下面的代碼在android上工作完美,但現在當我嘗試在純java(而不是android)中的代碼時,我得到了異常。 這是我的Java桌面代碼,上傳視頻到PHP將視頻從java上傳到php服務器時出錯

try { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost(
       "http://b....vedioup.php"); 

     FileBody filebodyVideo = new FileBody(new File(Path)); 
     StringBody title = new StringBody("Filename: " + Path); 
     StringBody description = new StringBody(
       "This is a description of the video"); 

     MultipartEntity reqEntity = new MultipartEntity(); 
     reqEntity.addPart("videoFile", filebodyVideo); 
     reqEntity.addPart("title", title); 
     reqEntity.addPart("description", description); 
     httppost.setEntity(reqEntity); 

     // DEBUG 
     System.out 
       .println("executing request " + httppost.getRequestLine()); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity resEntity = response.getEntity(); 

     // DEBUG 
     System.out.println(response.getStatusLine()); 
     if (resEntity != null) { 
      System.out.println(EntityUtils.toString(resEntity)); 
     } // end if 

     if (resEntity != null) { 
      resEntity.consumeContent(); 
     } // end if 

     httpclient.getConnectionManager().shutdown(); 

    } 

,這是在控制檯輸出。

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory 
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187) 
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146) 
at BlankFoem.uploadFile(BlankFoem.java:351) 

很明顯,它沒有找到類的例外,但我該如何解決它。

感謝

+1

該錯誤主要講述一個缺少的類,這個類是在一個lib jar我認爲你需要在你的類路徑中包含'apache commons:logging' jar – Yazan 2014-11-08 10:37:28

+0

是啊@Yazan你是對的。謝謝 – 2014-11-08 10:56:17

回答

1

您使用的是過時類,DefaultHttpClient。這是它的API Documentation。正如您所看到的,它被標記爲已棄用,並且他們建議使用HttpClientBuilder代替。

使用不贊成使用的類將調用一個需要記錄工廠的父類(也不贊成使用)。我假設apache.commons.logging jar沒有安裝在你的classpath中,因此你會得到這個特殊的異常。

因爲他們推薦使用HttpClientBuilder而不是安裝日誌記錄罐,所以最好使用HttpClientBuilder

它在android上工作的原因是因爲你可能在那裏有一箇舊版本的Apache HttpClient庫。

以下鏈接指向新的Apache HTTP Components library,包括所有文檔和下載。

+1

噢,就是這樣。非常感謝。 – 2014-11-08 10:55:31

1

只是這個jar文件apache.commons.logging添加到項目中,和你做

0

哦,夥計,你只需要

apache.commons.logging jar 

添加到您的項目。 愚蠢

相關問題