2014-01-22 72 views
1


我目前正嘗試使用android將HTML文件上傳到我的燈服務器。 我真的不知道如何讓文件上傳到服務器。我正在做的是:以編程方式編寫HTML文件,然後將其上傳到網絡服務器。
我知道這是可能的。

我已經開始寫這個文件了。我也有我的服務器上上傳腳本:
Android:將html文件上傳到http服務器

<?php 
$target_path = "./"; 
$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!"; 
} 
?> 

我讀過關於如何做到這一點所有這些教程,但我只是不明白這一點。

+0

那你的Android的Java代碼? –

+0

沒有Java代碼,我根本找不到合適的代碼,我在網上找到的例子dooesn't工作(http://stackoverflow.com/questions/7018883/android-file-upload),我不能寫我自己的 – rhbvkleef

+1

在你的android代碼上傳,你確定它是在上傳HTML文件時在多部分實體嗎? – KaHeL

回答

0

您可以使用此由於Android代碼:

public int uploadFile(String sourceFileUri) { 

    HttpURLConnection conn = null; 
    DataOutputStream dos = null; 
    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 
    int bytesRead, bytesAvailable, bufferSize; 
    byte[] buffer; 
    int maxBufferSize = 1 * 1024 * 1024; 
    File sourceFile = new File(sourceFileUri); 

    if (!sourceFile.isFile()) { 
     Log.e("uploadFile", "Source File not exist :" + uploadFilePath + "" + uploadFileName); 
     return 0; 
    } 
    else 
    { 
     try { 
      // open a URL connection to the Servlet 
      FileInputStream fileInputStream = new FileInputStream(sourceFile); 
      URL url = new URL(upLoadServerUri); 

      // 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("uploadedfile", sourceFileUri); 

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

      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + sourceFileUri + "\"" + 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(); 

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

      if(serverResponseCode == 200){ 
       Log.e("uploadFile", "File Uploaded."); 
       // Congrats! 
      } 

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

     } catch (Exception e) { 
      Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e); 
     } 
     return serverResponseCode; 

    } // End else block 
}