将视频从java上传到php服务器时出错

问题描述:

我正在开发一个可以将视频发送到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

您使用的是过时类,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

只是这个jar文件apache.commons.logging添加到项目中,和你做

哦,伙计,你只需要

apache.commons.logging jar 

添加到您的项目。 愚蠢