2011-06-22 63 views
0

嗨我寫了一個代碼,如下所示從我的android手機上傳文件到PHP服務器。應用程序運行時沒有任何錯誤,但文件未上傳到服務器。我哪裏做錯了 ?文件是不是從Android上傳到PHP服務器

公共類上傳延伸活動{

HttpURLConnection connection = null; 
DataOutputStream outputStream = null; 
DataInputStream inputStream = null; 

String pathToOurFile = "sdcard/Android/data/file.pdf"; 
String urlServer = "http://mpss.csce.uark.edu/~smandava/upload.php"; 
String lineEnd = "\r\n"; 
String twoHyphens = "--"; 
String boundary = "*****"; 
int bytesRead, bytesAvailable, bufferSize; 
byte[] buffer; 
int maxBufferSize = 1*1024*1024; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 



    try 
    { 
    FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile)); 

    URL url = new URL(urlServer); 
    connection = (HttpURLConnection) url.openConnection(); 

    // Allow Inputs & Outputs 
    connection.setDoInput(true); 
    connection.setDoOutput(true); 
    connection.setUseCaches(false); 

    // Enable POST method 
    connection.setRequestMethod("POST"); 

    connection.setRequestProperty("Connection", "Keep-Alive"); 
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

    outputStream = new DataOutputStream(connection.getOutputStream()); 
    outputStream.writeBytes(twoHyphens + boundary + lineEnd); 
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd); 
    outputStream.writeBytes(lineEnd); 

    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    buffer = new byte[bufferSize]; 

    // Read file 
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

    while (bytesRead > 0) 
    { 
    outputStream.write(buffer, 0, bufferSize); 
    bytesAvailable = fileInputStream.available(); 
    bufferSize = Math.min(bytesAvailable, maxBufferSize); 
    bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
    } 

    outputStream.writeBytes(lineEnd); 
    outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

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

    fileInputStream.close(); 
    outputStream.flush(); 
    outputStream.close(); 
    } 
    catch (Exception ex) 
    { 
    //Exception handling 
    } 


} 

}

//upload.php

$target_path = "http://mpss.csce.uark.edu/~smandava/files/"; 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
echo "The file ". basename($_FILES['uploadedfile']['name']). 
" has been uploaded"; 
} else{ 
echo "There was an error uploading the file, please try again!"; 
} 
+1

它運行時沒有錯誤,因爲您可以捕獲Java代碼中的所有潛在錯誤。在代碼的catch代碼塊中放入一個'Log'語句,看看它爲什麼失敗。 – Haphazard

+0

你可以發佈PHP文件中的代碼嗎?此外,您嘗試上傳的文件的大小以及php.ini中的upload_max_filesize的值是多少? PHP腳本是否已知可以工作? – Cez

+0

對不起,PHP代碼並不明顯,所以你可以忽略代碼 – Cez

回答

1

的$ target_path應該是一個文件系統路徑,而不是一個網站的網址。

$target_path = '/home/smandava/public_html/files/'; // something like this 
相關問題