2012-07-25 74 views
0

我用文件上傳圖片文件到網站。我收到了錯誤消息。你能給我一些建議嗎?感謝 操作系統:Android 4.0.3 phone:htc xhttp沒有迴應

error information

E/dalvikvm(11952): Could not find class 'org.apache.http.entity.mime.content.FileBody', referenced from method tw.tcc.tsvs.www.FileUploadPost.FileUploadPostActivity$1.onClick 

代碼

import java.io.BufferedReader; 
import java.io.File; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.mime.HttpMultipartMode; 
import org.apache.http.entity.mime.MultipartEntity; 
import org.apache.http.entity.mime.content.FileBody; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

private Button.OnClickListener jclove=new OnClickListener(){ 
public void onClick(View v) { 
try {  
      //file_post 
      File file = new File("/sdcard/android.jpg"); 
      try { 
        HttpClient client = new DefaultHttpClient(); 
        String postURL = "http://latest.tsd2497r1.ext.hipaas.hinet.net/"; 
        HttpPost post = new HttpPost(postURL); 
        FileBody bin = new FileBody(file); 
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
        reqEntity.addPart("myFile", bin); 
        post.setEntity(reqEntity); 
        HttpResponse response = client.execute(post); 
        HttpEntity resEntity = response.getEntity(); 
        if (resEntity != null) {  
          Log.i("RESPONSE",EntityUtils.toString(resEntity)); 
         } 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 


      //} 
     } catch (Exception e) { 
      e.printStackTrace(); 
     }  
    } 
}; 
+0

看到這個討論https://groups.google.com/forum/?fromgroups#!topic/android-developers/QQSWZ_sWdgU – 2012-07-25 09:58:44

+0

非常感謝。我會嘗試。 – user1430445 2012-07-25 10:46:54

+1

工作! ...感謝imrankhan – user1430445 2012-07-26 05:26:33

回答

1

從您的代碼和錯誤,我認爲你忘了添加httpmime-4.0.jar文件在您的構建路徑。

示例代碼:

HttpPost httpost = new HttpPost("url for upload file"); 

MultipartEntity entity = new MultipartEntity(); 
entity.addPart("myIdentifier", new StringBody("somevalue")); 
entity.addPart("myAudioFile", new FileBody(File)); 

httpost.setEntity(entity); 
HttpResponse response; 
response = httpclient.execute(httpost); 

看我在這太問題的答案。

upload an image and audio in One request in android

+0

我在我的構建路徑中添加了6個jar。可以嗎? (1)apache-mime4j-0.6.jar (2)commons-codec-1.3.jar (3)commons-logging-1.1.1.jar (4)httpclient-4.0.3.jar (5) httpcore-4.0.1.jar (6)httpmime-4.0.3.jar – user1430445 2012-07-25 10:49:16

+0

@ user1430445你是否在項目根目錄下的'libs'文件夾中添加了這些依賴關係? – sunil 2012-07-25 10:54:21

+0

構建路徑 - >添加外部JAR - >選擇那些6個jar文件 - >確定 是否有任何問題? – user1430445 2012-07-25 11:04:02

0
For uploading you should check with MIME Headers and buffer for file upload. 

You can try with code similar to given below :- 


DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); 


String fName = uploadPath.substring(uploadPath 
            .lastIndexOf("/") + 1); 

          dos.writeBytes(LINE_START + BOUNDRY + LINE_END); 
          dos.writeBytes("Content-Disposition: form-data; name=\"uploaded\";" 
            + " filename=\"" + fName + "\"" + LINE_END); 
          dos.writeBytes("Content-Type: image/JPEG" + LINE_END); 
          dos.writeBytes(LINE_END); 

          // create a buffer of maximum size 
          bytesAvailable = fileInputStream.available(); 
          bufferSize = Math.min(bytesAvailable, maxBufferSize); 
          buffer = new byte[bufferSize]; 

          // read file and write it into form... 
          bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
          totalBytes = 0; 

          while (bytesRead > 0) { 
           totalBytes += bytesRead; 
           dos.write(buffer, 0, bufferSize); 
           bytesAvailable = fileInputStream.available(); 
           bufferSize = Math.min(bytesAvailable, maxBufferSize); 
           bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
          } 

          // send multipart form data necesssary after file data... 
          dos.writeBytes(LINE_END); 
          dos.writeBytes(LINE_START + BOUNDRY + LINE_START + LINE_END); 

          // close streams 
          fileInputStream.close(); 
          dos.flush(); 
          dos.close(); 
+0

謝謝魔術師 – user1430445 2012-07-26 05:50:21

0

謝謝大家。我學到更多。 下面的鏈接對我來說最有用。
https://groups.google.com/forum/?fromgroups#!topic/android-developers/QQSWZ_sWdgU/discussion

除了使用httpmime.jar,第二個辦法是複製源(*。JAVA)到yourproject/src文件夾......最後directiry結構應該像yourproject/SRC /組織/阿帕奇/ HTTP /實體/ mime/... 您不應該將jar庫和源文件混合到這些庫中... Everythihng對我來說工作得很好,所以請謹慎操作。

源代碼(*。java)你可以從http://hc.apache.org/downloads.cgi下載
選擇HttpClient 4.2.1:SOURCE - > 4.2.1.zip。

File file = new File("/sdcard/android.jpg"); 
try { 
    HttpClient client = new DefaultHttpClient(); 
    String postURL = "http://192.168.12.90/album/upload.php"; //web site 
    HttpPost post = new HttpPost(postURL); 
    FileBody bin = new FileBody(file);  
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
    reqEntity.addPart("upf", bin); // first reference name(upf) should be the same as server side 
    post.setEntity(reqEntity); 
    HttpResponse response = client.execute(post); 
    HttpEntity resEntity = response.getEntity(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

//server side php code 
$Destdir="upload"; 
$tmp_filename=$_FILES['upf']['tmp_name'];//post reference(upf).It must be the same as addPart("upf" 
$originalfilename=$_FILES['upf']['name'];//post reference(upf).It must be the same as addPart("upf" 
$server_filename= $Destdir. "/" .basename($originalfilename); 
move_uploaded_file($tmp_filename,$server_filename);