2010-08-06 3 views

답변

1

기본적으로 양면이 필요합니다. 첫 번째는 Android에 있습니다. 데이터를 웹 서비스에 보내야합니다 (ASyncTask e.x에서 수행). 이제, 우리는 다른면을 가지고있다

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 서버 여기에는 URL에 파일과 몇 가지 추가 POST 값을 보내는 당신을위한 약간의 방법을했다. 보내진 POST 요청을 처리하려면 PHP와 같은 일부 논리가 필요합니다. 이 같은 뭔가 작동합니다 :

<?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"); 
    } 
?> 
+0

이봐 감사 톤! 그것은 나에게 차기 시작을 제공하는 것이 꽤 좋다. 나는 그걸로 일할 것이고 어떤 이슈라도 다시 만날 수 있다면 – NickHalden

+0

고마워, 아무런 문제가 없다. ASyncTask가있는 것이 중요하다. 상상해보십시오. 업로드 할 5MBi 파일이 있고 그 화면에 euser가 꽂혀 있고 아무것도로드 아이콘이 없어도 아무 일도 일어나지 않는다고 상상해보십시오. –

관련 문제