2012-09-25 6 views
0

파일을 서버에 업로드하고 파일 처리에 따라 서버에서 다른 응답을받습니다. 모든 것이 작동하지만 서버에서 응답하는 것은 매우 느립니다. 디버거에서 체크하고 다음 코드 줄을 실행하는 데 6 초가 걸립니다.매우 느린 실행중인 DataInputStream 열기

inStream = new DataInputStream(connection.getInputStream()); 

나는 웹 브라우저를 통해 동일한 파일과 코드를 테스트했으며 응답을 표시하는 데 약 1 ~ 2 초가 걸렸다. 여기에 내 전체 코드가 있습니다, 나는 그게 좋다고 생각하지만, 어쩌면 여기에 뭔가 제대로되어 있지 않은 것이있을 것입니다. 이 작업을 수행하는 더 좋은 방법이 있습니까? 또는 새로운 DataInputStream이 항상 느리게 진행될 것인가?

private String loadImageFromNetwork(String myfile) { 
     HttpURLConnection connection = null; 
     DataOutputStream outStream = null; 
     DataInputStream inStream = null; 
     String make = ""; 
     String model = ""; 
     String disp = ""; 
      String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 

     int bytesRead, bytesAvailable, bufferSize; 

     byte[] buffer; 

     int maxBufferSize = 1*1024*1024; 

     String urlString = "http://xxxxxxxxxxxxxx/upload.php"; 
     sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + myfile))); 

      try { 

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

      // open a URL connection to the Servlet 
      URL url = new URL(urlString); 

      // Open a HTTP connection to the URL 
      connection = (HttpURLConnection) url.openConnection(); 

      // Allow Inputs 
      connection.setDoInput(true); 

      // Allow Outputs 
      connection.setDoOutput(true); 

      // Don't use a cached copy. 
      connection.setUseCaches(false); 

      // Use a post method. 
      connection.setRequestMethod("POST"); 

      connection.setRequestProperty("Connection", "Keep-Alive"); 

      connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary); 

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

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

      // close streams 
      fileInputStream.close(); 
      outStream.flush(); 
      outStream.close(); 


      } 
      catch (MalformedURLException ex) { 
        ex.printStackTrace(); 
      } 

      catch (IOException ioe) { 
        ioe.printStackTrace(); 
      } 

      //------------------ read the SERVER RESPONSE 
      try { 

       inStream = new DataInputStream(connection.getInputStream()); 

        String str; 

        while ((str = inStream.readLine()) != null) 
        { 
          disp = disp + str; 

        } 

        inStream.close(); 


      } 
      catch (IOException ioex){ 
        ioex.printStackTrace(); 
      } 
      return disp; 

    } 

답변

0

서버에서 새 스레드로 응답을 읽으려면 코드를 이동해야합니다. 예 :

private class ReadResponse implements Runnable { 
    public void run() { 
       //------------------ read the SERVER RESPONSE 
     try { 

      inStream = new DataInputStream(connection.getInputStream()); 

       String str; 

       while ((str = inStream.readLine()) != null) 
       { 
         disp = disp + str; 

       } 

       inStream.close(); 


     } 
     catch (IOException ioex){ 
       ioex.printStackTrace(); 
     } 
     //return disp; 
     //here you need to show your display on UI thread 
    } 
    } 
} 

하고 파일을 업로드하기 전에 읽기 스레드를 시작합니다.

+0

제안 해 주셔서 감사합니다. 파일 업로드가 지연되거나 처리가 예상보다 오래 걸리는 경우 작동하지 않습니다? – user1197941