Java Android无法将多种类型的文件上载到PHP服务器

Java Android无法将多种类型的文件上载到PHP服务器,java,php,android,apache,Java,Php,Android,Apache,我正在尝试将一个图像文件和一个音频文件同时从Android上传到Php服务器,这是Android部分的上传方法: public String postFile(File image,File audio) throws Exception { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(uploadUrl); MultipartEntityBuilder builde

我正在尝试将一个图像文件和一个音频文件同时从Android上传到Php服务器,这是Android部分的上传方法:

public String postFile(File image,File audio) throws Exception {

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(uploadUrl);
    MultipartEntityBuilder builder = MultipartEntityBuilder.create();        
    builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

    FileBody fbImage = new FileBody(image);
    FileBody fbAudio = new FileBody(audio);

    builder.addPart("image", fbImage);  
    builder.addPart("audio", fbAudio);
    final HttpEntity yourEntity = builder.build();

    class ProgressiveEntity implements HttpEntity {
        @Override
        public void consumeContent() throws IOException {
            yourEntity.consumeContent();                
        }
        @Override
        public InputStream getContent() throws IOException,
                IllegalStateException {
            return yourEntity.getContent();
        }
        @Override
        public Header getContentEncoding() {             
            return yourEntity.getContentEncoding();
        }
        @Override
        public long getContentLength() {
            return yourEntity.getContentLength();
        }
        @Override
        public Header getContentType() {
            return yourEntity.getContentType();
        }
        @Override
        public boolean isChunked() {             
            return yourEntity.isChunked();
        }
        @Override
        public boolean isRepeatable() {
            return yourEntity.isRepeatable();
        }
        @Override
        public boolean isStreaming() {             
            return yourEntity.isStreaming();
        } // CONSIDER put a _real_ delegator into here!

        @Override
        public void writeTo(OutputStream outstream) throws IOException {

            class ProxyOutputStream extends FilterOutputStream {

                public ProxyOutputStream(OutputStream proxy) {
                    super(proxy);    
                }
                public void write(int idx) throws IOException {
                    out.write(idx);
                }
                public void write(byte[] bts) throws IOException {
                    out.write(bts);
                }
                public void write(byte[] bts, int st, int end) throws IOException {
                    out.write(bts, st, end);
                }
                public void flush() throws IOException {
                    out.flush();
                }
                public void close() throws IOException {
                    out.close();
                }
            } // CONSIDER import this class (and risk more Jar File Hell)

            class ProgressiveOutputStream extends ProxyOutputStream {
                public ProgressiveOutputStream(OutputStream proxy) {
                    super(proxy);
                }
                public void write(byte[] bts, int st, int end) throws IOException {

                    // FIXME  Put your progress bar stuff here!

                    out.write(bts, st, end);

                    if(end==bts.length-1){
                        dialog.dismiss();
                        Toast.makeText(TakePhotoActivity.this, "File Upload Complete.", Toast.LENGTH_SHORT).show();
                    }
                }
            }

            yourEntity.writeTo(new ProgressiveOutputStream(outstream));
        }

    };
    ProgressiveEntity myEntity = new ProgressiveEntity();

    post.setEntity(myEntity);
    HttpResponse response = client.execute(post); 

    System.out.println("UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU "+getContent(response));

    return getContent(response);

} 

public static String getContent(HttpResponse response) throws IOException {
    BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
    String body = "";
    String content = "";

    while ((body = rd.readLine()) != null) 
    {
        content += body + "\n";
    }
    return content.trim();
}
这是Php部分:

<?php

$file_path = "uploads/";

$file_path = $file_path . basename( $_FILES['image']['name']);
if(move_uploaded_file($_FILES['image']['tmp_name'], $file_path)) {
    echo "image success";
} else{
    echo "image fail";
}

$file_path = $file_path . basename( $_FILES['audio']['name']);
if(move_uploaded_file($_FILES['audio']['tmp_name'], $file_path)) {
    echo "audio success";
} else{
    echo "audio fail";
}
?>


但是在服务器上我只能看到音频文件,图像文件上传失败,我也试着上传两个图像文件,效果不错,但是当一个音频文件和一个图像文件,图像文件通常失败时,对此有什么建议吗?提前谢谢你。

我建议你跟进。因为你们的多党制是不同的,这一个有效。我试过了。快乐编码

    try
    {
        HttpClient client = new DefaultHttpClient();

        HttpPost post = new HttpPost(URL);

        MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
        entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

        entityBuilder.addTextBody(USER_ID, userId);
        entityBuilder.addTextBody(NAME, name);
        entityBuilder.addTextBody(TYPE, type);
        entityBuilder.addTextBody(COMMENT, comment);
        entityBuilder.addTextBody(LATITUDE, String.valueOf(User.Latitude));
        entityBuilder.addTextBody(LONGITUDE, String.valueOf(User.Longitude));

        if(file != null)
        {
            entityBuilder.addBinaryBody(IMAGE, file);
        }

        HttpEntity entity = entityBuilder.build();

        post.setEntity(entity);

        HttpResponse response = client.execute(post);

        HttpEntity httpEntity = response.getEntity();

        result = EntityUtils.toString(httpEntity);

        Log.v("result", result);
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

您只能一次性使用实体中的内容。但是你有
yourEntity.consumerContent()和此
返回yourEntity.getContent()和更多。所以我想这就是为什么excpetion@Raghunandan,只上传一个文件就行了,但当我试图同时上传两个文件时,只上传了一个文件,顺便说一句,代码来自我你想并行上传为什么不使用executor?@Raghunandan,我是android新手,你能更详细地介绍一下executor吗?非常感谢你,我的意思是拿出一份意大利面代码!它使你的问题更容易解决