2014-02-14 200 views
1

根據以前的帖子的建議,我試圖使用Android: Uploading image on server with php但是我得到一個文件未找到異常。Android上傳文件到服務器

這是我在上面的帖子中描述的功能。我對這些輸入是:

畫廊:uploadFile:Source File not exist :content://media/external/images/media/342 照片:uploadFile:Source File not exist: file:///storage/emulated/0/MyDir/blah

的是從lanched到catputre /選擇他們的意圖得出這些URI。任何想法,爲什麼我得到一個File Not Found異常?

private void doFileUpload(String exsistingFileName){ 
    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    DataInputStream inStream = null; 

    //String exsistingFileName = "/sdcard/six.3gp"; 
    // Is this the place are you doing something wrong. 

    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1*1024*1024; 
    String urlString = "http://192.168.1.5/upload.php"; 
    try 
    { 
     Log.e("MediaPlayer","Inside second Method"); 
     FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName)); 
     URL url = new URL(urlString); 
     conn = (HttpURLConnection) url.openConnection(); 
     conn.setDoInput(true); 
     // Allow Outputs 
     conn.setDoOutput(true); 
     // Don't use a cached copy. 
     conn.setUseCaches(false); 
     // Use a post method. 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("Connection", "Keep-Alive"); 
     conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 
     dos = new DataOutputStream(conn.getOutputStream()); 
     dos.writeBytes(twoHyphens + boundary + lineEnd); 
     dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + exsistingFileName +"\"" + lineEnd); 
     dos.writeBytes(lineEnd); 
     Log.e("MediaPlayer","Headers are written"); 
     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     buffer = new byte[bufferSize]; 
     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); 
     } 
     dos.writeBytes(lineEnd); 
     dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 
     BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
     String inputLine; 
     String LogString = ""; 

     while ((inputLine = in.readLine()) != null) { 
      LogString= LogString + inputLine; 
     } 

     Log.i(Utils.TAG, LogString); 
     // close streams 
     fileInputStream.close(); 
     dos.flush(); 
     dos.close(); 
    } 
    catch (MalformedURLException ex) 
    { 
     Log.e("MediaPlayer", "error: " + ex.getMessage(), ex); 
    } 
    catch (IOException ioe) 
    { 
     Log.e("MediaPlayer", "error: " + ioe.getMessage(), ioe); 
    } 

    //------------------ read the SERVER RESPONSE 
    try { 
     inStream = new DataInputStream (conn.getInputStream()); 
     String str;    
     while ((str = inStream.readLine()) != null) 
     { 
      Log.e("MediaPlayer","Server Response"+str); 
     } 
     /*while((str = inStream.readLine()) !=null){ 

     }*/ 
     inStream.close(); 
    } 
    catch (IOException ioex){ 
     Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); 
    } 
} 

回答

1

我得到找不到文件的異常

這是因爲這兩個時間都不路徑文件。你可以通過觀察他們來判斷。您也沒有按照my previous answer的說明操作。

替換:

FileInputStream fileInputStream = new FileInputStream(new File(exsistingFileName)); 

有:

InputStream contentInputStream = getContentResolver().openInputStream(Uri.parse(exsistingFileName)); 

(與contentInputStream替換fileInputStream出現爲你的方法的其餘部分)

需要注意的是:

  • 這假定您的doFileUpload()在繼承自Context的某個類上實現,例如ActivityService。如果doFileUpload()無法訪問getContentResolver(),您需要安排通過其他方式獲得ContentResolverdoFileUpload()

  • 你可以簡化通過傳遞您到doFileUpload()收到Uri,而不是將其轉換爲String,然後回一個Uri重要的一點。

  • 您將需要爲Content-Disposition:標題創建自己的文件名,因爲您沒有從Uri獲取文件名。

+0

我也說你最後的答案可能在我的頭上。 – jcaruso

+0

@jcaruso:除非你特別解釋*你不明白的東西,否則沒有人能夠提供澄清。 – CommonsWare

+0

我不僅僅是要讓你先解釋它,而不是先閱讀。雖然我理解並做了你所問的,但顯然我錯了。我討厭那些不做研究並期望會變得艱難的人。然而,回到上面的改變它似乎已經工作(我有一個文件至少上傳到我的服務器),但它沒有擴展名(JPG/PNG),雖然它確實呈現當我去的URL – jcaruso

0

您試圖從Uri獲取InputStream?這可能會更容易:

private void doFileUpload(Uri fileUri){ 
    // some code 
    InputStream inputStream = getContentResolver().openInputStream(fileUri); 
    // more code 
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + "some_file_name" +"\"" + lineEnd); 
    // the rest of the code 
}