使用Web服务?

时间:2010-08-06 06:15:48

标签: android web-services

我想复制服务器上的所有视频文件&将其保存在Web服务的Web内容文件夹中,以便以后所有人都可以看到它!

我该怎么办?

1 个答案:

答案 0 :(得分:1)

基本上你需要双方。第一个是在Android上。您必须将(在ASyncTask e.x中执行此操作)数据发送到您的Web服务。在这里,我为您做了一个小方法,它将一个文件和一些额外的POST值发送到URL:

private boolean handleFile(String filePath, String mimeType) {      
    HttpURLConnection connection = null;
    DataOutputStream outStream = null;
    DataInputStream inStream = null;

    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary = "*****";

    int bytesRead, bytesAvailable, bufferSize;

    byte[] buffer;

    int maxBufferSize = 1*1024*1024;

    String urlString = "http://your.domain.com/webservice.php";

    try {
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(new File(filePath));
        } catch(FileNotFoundException e) { }
        URL url = new URL(urlString);
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setUseCaches(false);

        SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());

        connection.setRequestMethod("POST");
        connection.setRequestProperty("Connection", "Keep-Alive");
        connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);            

        outStream = new DataOutputStream(connection.getOutputStream());

        // Some POST values
        outStream.writeBytes(addParam("additional_param", "some value");
      outStream.writeBytes(addParam("additional_param 2", "some other value");              

        // The file with the name "uploadedfile"
        outStream.writeBytes(twoHyphens + boundary + lineEnd);
        outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + filePath +"\"" + lineEnd + "Content-Type: " + mimeType + lineEnd + "Content-Transfer-Encoding: binary" + lineEnd);           
        outStream.writeBytes(lineEnd);

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

        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

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

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

        fileInputStream.close();
        outStream.flush();
        outStream.close();  
    } catch (MalformedURLException e) {
        Log.e("APP", "MalformedURLException while sending\n" + e.getMessage());
    } catch (IOException e) {
        Log.e("APP", "IOException while sending\n" + e.getMessage());
    }


    // This part checks the response of the server. If its "UPLOAD OK" the method returns true
    try {
       inStream = new DataInputStream( connection.getInputStream() );
       String str;

       while (( str = inStream.readLine()) != null) {
           if(str=="UPLOAD OK") {
               return true;
           } else {
               return false;
           }
       }
       inStream.close();
    } catch (IOException e){
        Log.e("APP", "IOException while sending\n" + e.getMessage());
    }
    return false;
}

现在,我们得到了另一面:http://your.domain.com/webservice.php 你的服务器。它需要一些逻辑,例如在PHP中,来处理发送的POST请求。 像这样的东西会起作用:

<?php
   $target_path = "videos/";      

   $target_path = $target_path.'somename.3gp';
   if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
       exit("UPLOAD OK");
   } else {
       exit("UPLOAD NOT OK");
   }
?>