2014-01-22 71 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 
}