2014-04-17 2 views
3

크기가 20MB보다 큰 비디오를 업로드하려고 할 때 서버가 보내는 매개 변수를받지 못했습니다. 비디오를 보내고 jsonobject를 server.Am에 문자열로 보내고 있습니다. httpmime-4.1.1 jar.Below는 제 코드입니다. 안드로이드 개발에 익숙하지 않으면 도움이 될 것입니다. 해결책이 빨리 필요합니다. 나를동영상 업로드 안드로이드

내가 전에이 작업을 수행 한
String s = json; 
Log.d("message", "s==="+s); 
HttpClient client = new DefaultHttpClient(); 
HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout 
StringBuilder sq = null;                 // Limit 

String result = null; 
String result1 = null; 
try { 
    HttpPost post = new HttpPost(url); 

    MultipartEntity reqEntity = new MultipartEntity(
      HttpMultipartMode.BROWSER_COMPATIBLE); 



    Log.d("message","video"+ bin.getFilename()); 

    reqEntity.addPart("video",bin); 



    reqEntity.addPart("arguments", new StringBody(s.toString()));   

    post.setEntity(reqEntity); 
    int timeoutConnection = 60000; 
    HttpParams httpParameters = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(httpParameters, 
      timeoutConnection); 
    int timeoutSocket = 60000; 

    HttpConnectionParams.setTcpNoDelay(httpParameters, true); 

    try { 
     HttpResponse response = client.execute(post); 
     Log.v("@WebSErvice:"+response, "Result0" + response); 
     if (response == null) { 
      result = null; 
     } else { 

      try { 
       TieWebServicesComponents BWSC = new TieWebServicesComponents(
         TieWebServicesComponents.POST_TASK, mContext); 
       result = BWSC.inputStreamToString(response.getEntity() 
         .getContent()); 
       Log.v("message:CommunityHome Response"+response, "Result1" + result); 

      } catch (IllegalStateException e) { 
       // Log.e(TAG, e.getLocalizedMessage(), e); 

      } catch (IOException e) { 
       // Log.e(TAG, e.getLocalizedMessage(), e); 
      } 

     } 

    }catch(NullPointerException ne){ 


    } 
} 
catch(Exception e) { 
    e.printStackTrace(); 

} 

//Log.v("@WebSErvice:", "REsult" + result); 

답변

1

, 내가 80메가바이트 및 업로드 기능 아래 MultiPartEntity 시도를 사용까지 가능 트랙을 도와주세요.

public class asyncUploadtrack extends AsyncTask<Void, Integer, Void> 
    { 
     HttpResponse response; 
     private android.app.ProgressDialog dialog; 
     long totalSize; 

     protected void onPostExecute(String result) 
     { 

     } 
     @Override 
     protected void onPreExecute() 
     { 

     } 
     @Override 
     protected void onProgressUpdate(Integer... values) 
     { 

     } 
     protected Void doInBackground(Void... params) 
     { 
      Log.i("Do in Backgtound", "Background"); 
      String url =app.WEB_API_URL+"?op="+ Constant.METHOD_UPLOAD_TRACK; 
      File video = new File(trackPath); // path of file 
      Bitmap out = Constant.bitmapResize(TrackFragment.btmTrackImg,308, 296) ;//Bitmap.createScaledBitmap(TrackFragment.btmTrackImg, 408, 396,true); 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      out.compress(CompressFormat.JPEG, 50, bos); 
      byte[] data = bos.toByteArray(); 


      HttpClient httpclient = new DefaultHttpClient(); 
      HttpPost post = new HttpPost(url); 




      try 
      { 
       MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
       multipartEntity.addPart("music", new FileBody(video)); 
       multipartEntity.addPart("title",new StringBody(trackTitle)); 
       multipartEntity.addPart("user", new StringBody(app.preferences.getStringPref("userId"))); 


       multipartEntity.addPart("filename", new StringBody("song.mp3")); 
       multipartEntity.addPart("avatar",new ByteArrayBody(data, "trackImage")); 
       //After adding everything we get the content's lenght 
       totalSize = multipartEntity.getContentLength(); 
       post.setEntity(multipartEntity); 
       response = httpclient.execute(post); 
       try 
       { 
        Log.i("Response is",response.getStatusLine().toString()); 
            } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      catch (IOException e) 
      { 
       e.printStackTrace(); 
      } 
      return null; 
     } 
    } 
+0

본인은 서버가 사용자와 미래의 독자가 그것으로부터 배울 수 있도록 게시 한 코드에서 무엇이 여기 일어나고있는 문제에 대한 답변입니다 설명 할 수있는 값 – Divya

0

이 코드를 확인하면 도움이 될 것입니다.

public int uploadFile(String sourceFileUri) { 


     String fileName = 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()) { 

      dialog.dismiss(); 

      Log.e("uploadFile", "Source File not exist :"+imagepath); 

      runOnUiThread(new Runnable() { 
       public void run() { 
        messageText.setText("Source File not exist :"+ imagepath); 
       } 
      }); 

      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("uploaded_file", fileName); 

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

       dos.writeBytes(twoHyphens + boundary + lineEnd); 
       dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" 
             + fileName + "\"" + 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){ 

        runOnUiThread(new Runnable() { 
         public void run() { 
          String msg = "File Upload Completed.\n\n See uploaded file here : \n\n" 
            +" F:/wamp/wamp/www/uploads"; 
          messageText.setText(msg); 
          Toast.makeText(MainActivity.this, "File Upload Complete.", Toast.LENGTH_SHORT).show(); 
         } 
        });    
       } 

       //close the streams // 
       fileInputStream.close(); 
       dos.flush(); 
       dos.close(); 

      } catch (MalformedURLException ex) { 

       dialog.dismiss(); 
       ex.printStackTrace(); 

       runOnUiThread(new Runnable() { 
        public void run() { 
         messageText.setText("MalformedURLException Exception : check script url."); 
         Toast.makeText(MainActivity.this, "MalformedURLException", Toast.LENGTH_SHORT).show(); 
        } 
       }); 

       Log.e("Upload file to server", "error: " + ex.getMessage(), ex); 
      } catch (Exception e) { 

       dialog.dismiss(); 
       e.printStackTrace(); 

       runOnUiThread(new Runnable() { 
        public void run() { 
         messageText.setText("Got Exception : see logcat "); 
         Toast.makeText(MainActivity.this, "Got Exception : see logcat ", Toast.LENGTH_SHORT).show(); 
        } 
       }); 
       Log.e("Upload file to server Exception", "Exception : " + e.getMessage(), e); 
      } 
      dialog.dismiss();  
      return serverResponseCode; 

     } // End else block 
    } 

또한이 링크를 확인하십시오. http://sunil-android.blogspot.in/2013/09/file-upload-on-php-server-in-android.html

행운을 빈다.

+0

을받지 BT이 코드를 시도? 또한 이것은 링크에서 코드를 복사하는 것만 큼 많은 대답이 아닙니다. 다른 사람의 콘텐츠를 사용할 때 크레딧을 포함해야하지만 자신의 콘텐츠도 일부 포함해야합니다. –