簡體   English   中英

使用多部分功能從Android上傳文件時出現問題

[英]problem uploading file from Android using multipart

我有以下代碼將電話中的文件(mpeg文件-大約20kb)發送到服務器。 但是,它在服務器端失敗。 有人可以告訴我我在客戶端犯了什么錯誤嗎? 謝謝。

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代碼:

<?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!";

}



?>

您要發送的零件名稱已uploaded

reqEntity.addPart("uploaded", bin);

但是,PHP希望使用名稱為userfile

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

對齊使其相同。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM