2015-11-25 82 views
2

我正在创建一个应用程序在Android上传图片的服务器上。为此,我在php中创建了一个api。但我不能上传的图片上服务器 这里是我的Android代码无法上传图像到Android应用程序使用PHP API的服务器

FileInputStream fileInputStream = new FileInputStream(
         sourceFile); 
       // URL url = new URL(
       // "http://www.mystashapp.com/Loyalty/mobileservice.php?action=upload_image"); 
       URL url = new URL(
         "http://www.mvcangularworld.com/api.php"); 
       // Open a HTTP connection to the URL 
       conn = (HttpURLConnection) url.openConnection(); 
       conn.setDoInput(true); // Allow Inputs 
       conn.setDoOutput(true); // Allow Outputs 
       conn.setUseCaches(false); // Don't use a Cached Copy 
       conn.setRequestMethod("POST"); 
       conn.setRequestProperty("Connection", "Keep-Alive"); 
       conn.setRequestProperty("ENCTYPE", "multipart/form-data"); 
       conn.setRequestProperty("Content-Type", 
         "multipart/form-data;boundary=" + boundary); 
       conn.setRequestProperty("uploaded_file", fileName); 

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

       dos.writeBytes(twoHyphens + boundary + lineEnd); 
       dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" 
         + fileName + "\"" + lineEnd); 

       dos.writeBytes(lineEnd); 

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

       while (bytesRead > 0) { 

        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(lineEnd); 
       dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

       // Responses from the server (code and message) 
       serverResponseCode = conn.getResponseCode(); 
       String serverResponseMessage = conn.getResponseMessage(); 

       InputStreamReader isr = new InputStreamReader(
         conn.getInputStream()); 

       BufferedReader br = new BufferedReader(isr); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = br.readLine()) != null) { 
        System.out.println(line); 
        sb.append(line); 
       } 

       isr.close(); 
       br.close(); 

       JSONObject json = new JSONObject(sb.toString()); 
       filePath = json.optString("filepath"); 

       Log.i("uploadFile", "HTTP Response is : " 
         + serverResponseMessage + ": " + serverResponseCode); 

       if (serverResponseCode == 200) { 
        // Toast.makeText(context, "File Upload Completed.", 
        // Toast.LENGTH_LONG).show(); 
        Log.e("message", "image upload complete"); 
       } 

       // close the streams // 
       fileInputStream.close(); 
       dos.flush(); 
       dos.close(); 

这里是我的PHP API

<?php 

$target_path = 'andriod/'; // Path to move uploaded files 
$response = array(); 

$file_upload_url = $target_path; 
//$filename = $_POST['filename']; 
$server_ip = "http://www.mvcangularworld.com";//gethostbyname(gethostname()); 
// final file url that is being uploaded 
$file_upload_url = $server_ip . '/' . $target_path; 

if (isset($_FILES['fileName']['name'])) { 
    $target_path = $target_path . basename($_FILES['fileName']['name']); 
    // reading other post parameters 


    $response['file_name'] = basename($_FILES['fileName']['name']); 

    try { 
     // Throws exception incase file is not being moved 
     if (!move_uploaded_file($_FILES['fileName']['tmp_name'], $target_path)) 
     { 
      // make error flag true 
      $response['error'] = true; 
      $response['message'] = 'Could not move the file!'; 
     } 

     // File successfully uploaded 
     //echo $file_upload_url . basename($_FILES['filename']['name']); 
     $response['message'] = 'File uploaded successfully!'; 
     $response['error'] = false; 
     $response['file_path'] = $file_upload_url . basename($_FILES['fileName']['name']); 
    } 
    catch (Exception $e) { 
     // Exception occurred. Make error flag true 
     $response['error'] = true; 
     $response['message'] = $e->getMessage(); 
    } 
} else { 
    // File parameter is missing 
    $response['error'] = true; 
    $response['message'] = $_FILES['fileName']['name']; 
} 

// Echo final json response to client 
echo json_encode($response, JSON_UNESCAPED_SLASHES); 
?> 

它总是把我扔在别的条件,给我这个效应初探作为回报

{"error":true,"message":null} 

我无法将我的文件上传到服务器。任何机构都知道这个问题,请告诉我。

回答

1

改变这一行

conn.setRequestProperty("uploaded_file", fileName); 

conn.setRequestProperty("fileName", fileName); 

,这也

dos.writeBytes("Content-Disposition: form-data; name=\"filename\";filename=\"" 
        + fileName + "\"" + lineEnd); 

你retriving文件在PHP filename,但名字uploaded_file传递。更改它的名称它将工作

+0

非常感谢您节省我的时间ravi –

0

1)将图像转换为字符串作为字节数组或base64(字符串格式)。

reference1

reference2

2)你的PHP API,尝试接收字符串作为REST风格的(如JSON)或正常。

3)在你的php中,将字符串转换为图像。但应限制图像格式(例如移动将发送JPEG格式和PHP将转换为字符串.JPEG)

reference3

reference4

你可以在谷歌搜索作为“机器人上传图片到服务器“

相关问题