簡體   English   中英

Android中處理HttpURLConnection的服務器端代碼

[英]Server side code to handle HttpURLConnection in Android

我正在使用 HttpURLConnection 類將文本文件上傳到我的 Apache 服務器。 對於服務器端代碼,我使用 php 來處理 POST 請求。 我想知道,除了 PHP 之外如何做同樣的事情,或者 PHP 是處理服務器端文件上傳的最佳方式?..此外,我在服務器端找不到任何 jsp 或 java 代碼來處理 http post 請求。 .任何除 PHP 之外做同樣事情的代碼片段都會非常有幫助..因為我認為 java 或 jsp 會更容易編碼..謝謝

上傳文件的Android代碼:

  public void upLoad()
{


     String exsistingFileName = path+"//"+"test.txt";

     String lineEnd = "\r\n";
     String twoHyphens = "--";
     String boundary = "*****";
     try {
         // ------------------ CLIENT REQUEST

         Log.e(Tag, "Inside second Method");

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

         // open a URL connection to the Servlet

         URL url = new URL(urlString);

         // Open a HTTP connection to the URL

         conn = (HttpURLConnection) url.openConnection();

         // Allow Inputs
         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);


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

         dos.writeBytes(twoHyphens + boundary + lineEnd);
         dos.writeBytes("Content-Disposition: post-data; name=uploadedfile;filename="
                         + exsistingFileName + "" + lineEnd);
         dos.writeBytes(lineEnd);

         Log.e(Tag, "Headers are written");

         // create a buffer of maximum size

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

         // read file and write it into form...

         int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable);

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

         // send multipart form data necesssary after file data...

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

         // close streams
         Log.e(Tag, "File is written");
         fileInputStream.close();
         dos.flush();
         dos.close();

     } catch (MalformedURLException ex) {
         Log.e(Tag, "error: " + ex.getMessage(), ex);
     }

     catch (IOException ioe) {
         Log.e(Tag, "error: " + ioe.getMessage(), ioe);
     }

     try {
         BufferedReader rd = new BufferedReader(new InputStreamReader(conn
                 .getInputStream()));
         String line;
         while ((line = rd.readLine()) != null) {
             Log.e("Dialoge Box", "Message: " + line);
         }
         rd.close();

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

服務器端的 PHP 代碼:

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

我認為這個問題沒有正確答案,PHP 可以像 JSP 甚至 ASP 一樣好。 取決於您的平台偏好,或者您是否計划擁有 Tomcat 與 apache 服務器,甚至是 microsoft iis 服務器。 我個人認為 PHP/Apache 是一個不錯的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM