2011-06-22 41 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 
相关问题