0

我使用带有文件的帖子将图像文件上传到网站。我收到错误消息。你能给我一些建议吗?感谢系统:android 4.0.3 phone:htc x

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

3 回答 3

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

看看我在这个 SO 问题中的回答。

在 Android 的一个请求中上传图像和音频

于 2012-07-25T09:58:48.053 回答
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();
于 2012-07-25T10:01:33.707 回答
0

谢谢大家。我学到更多。下面的链接对我最有用。
https://groups.google.com/forum/?fromgroups#!topic/android-developers/QQSWZ_sWdgU/discussion

除了使用 httpmime.jar,第二种方法是将源代码(* .java)复制到 yourproject/src 文件夹中...最终的目录结构应该类似于 yourproject/src/org/apache/http/entity/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);
于 2012-07-26T06:14:21.357 回答