2010-07-08 25 views
9

Tôi đã tìm kiếm giải pháp này trong nhiều giờ, nhưng tôi không thể tìm thấy bất kỳ giải pháp nào.Trình tải lên tệp của Android với php phía máy chủ

Về cơ bản, tôi muốn tải lên, từ thiết bị Android của tôi, tệp tới trang web http. Tuy nhiên, tôi không có đầu mối nào để làm điều này. Tôi đang sử dụng java trên thiết bị và tôi muốn sử dụng PHP ở phía máy chủ của mọi thứ. Tôi chỉ muốn tải lên các tập tin, không làm bất cứ điều gì ưa thích với họ trên máy chủ.

Có ai có thể cung cấp mã và/hoặc liên kết tốt với những gì tôi cần không? Tôi có ít hoặc không có kinh nghiệm trong việc này, và tôi đang thua lỗ.

Xin cảm ơn, NS

PS. Tôi không có kinh nghiệm trong việc viết mã PHP.

Trả lời

11

Yeah, vì vậy tôi thấy java-side của sự vật. Điều này làm việc, vì vậy ... yeah.

public class Uploader extends Activity { 

    private String Tag = "UPLOADER"; 
    private String urlString = "YOUR_ONLINE_PHP"; 
    HttpURLConnection conn; 

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

     String exsistingFileName = "/sdcard/uploader/data/testfile"; 

     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); 
     } 
    } 
} 
+1

Tôi đã thử nghiệm mã này nhưng không thể tải tệp lên quá 2MB, bất kỳ giải pháp nào cho điều đó. – PiyushMishra

+0

Rất tiếc, mã này chỉ hoạt động đối với các tệp nhỏ. Chúng tôi đã gặp phải vấn đề đó, nhưng chúng tôi không cần tải lên nhiều hơn một vài kB, vì vậy chúng tôi đã loại bỏ nó. Chúng tôi không còn làm việc với dự án đó nữa. Cách tôi nhìn thấy nó hoặc là với "int bytesAvailable = fileInputStream.available();", trong đó fIS không bao giờ có nhiều hơn 2MB sẵn có (không), hoặc bộ đệm byte: int maxBufferSize = 1000; byte [] buffer = byte mới [byteKhả dụng]; Thử thay đổi maxBufferSize để xem điều gì xảy ra. –

+0

khi bạn nói "mặt java của sự vật", điều đó có nghĩa là mã này dành cho 'ứng dụng gốc' ??? .... bởi vì nếu có, thì sẽ không có vấn đề gì nếu người dùng không có ứng dụng? họ sẽ phải tải nó lên thiết bị Android của họ trước? ... cảm ơn. – dsdsdsdsd

9

Nếu bạn muốn tải lên các file có dung lượng lớn hơn 2MB bạn cần phải chỉnh sửa PHP của bạn configuraton

; Maximum allowed size for uploaded files. 
; http://php.net/upload-max-filesize 
upload_max_filesize = 2M 

Ví dụ:

; Maximum allowed size for uploaded files. 
; http://php.net/upload-max-filesize 
upload_max_filesize = 10M 

Điều đó sẽ cho phép bạn upload file lên đến 10MB kích thước

+0

Cảm ơn bạn đã làm rõ! –

Các vấn đề liên quan