简体   繁体   English

使用多部分功能从Android上传文件时出现问题

[英]problem uploading file from Android using multipart

I have the following code to send a file (mpeg file - about 20kb) from the phone to the server. 我有以下代码将电话中的文件(mpeg文件-大约20kb)发送到服务器。 However, it fails at the server end. 但是,它在服务器端失败。 Can anyone kindly telly me what mistake I am making at the client end ? 有人可以告诉我我在客户端犯了什么错误吗? Thanks. 谢谢。

ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //bm.compress(CompressFormat.JPEG, 75, bos);
        byte[] data = bos.toByteArray();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://example.com/upload.php");
        File file= new File("/mnt/sdcard/enca/aha.mpeg");
        FileBody bin = new FileBody(file);
        MultipartEntity reqEntity = new     MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("uploaded", bin);
        reqEntity.addPart("random", new StringBody(encameo1.random));
        reqEntity.addPart("fingerPrint", new StringBody(encameo1.fingerprint));
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
        System.out.println("Response: " + s);

php code: php代码:

<?php



$target_path = "uploaded_files/";

$target_path = $target_path . basename( $_FILES['userfile']['name']); 

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) 
{

    echo "The file ".  basename( $_FILES['userfile']['name'])." has been uploaded";

} 

else
{

    echo "There was an error uploading the file, please try again!";

}



?>

You're sending the part with the name uploaded : 您要发送的零件名称已uploaded

reqEntity.addPart("uploaded", bin);

However, PHP is expecting a part with the name userfile : 但是,PHP希望使用名称为userfile

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) 

Align it to be the same. 对齐使其相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM