2016-07-28 8 views
0

イメージをWebサーバーにアップロードする処理を高速化する方法はありますか?私が開発しているアプリでは画像をアップロードするのに時間がかかりすぎます。私のコードが動作し、私はサーバーにイメージを正常にアップロードできることを知っています。 私はこのコードを、ここで見つけたチュートリアルに基づいていません。Httpurlconnectionを使用したスピードアップイメージアップロード

public String uploadFile(String apiPath, String filePath, String type) 
    { 
    String path = ""; 
    String result = ""; 

    switch (type) 
    { 
     case "M": 
     path = "Merchant/" + apiPath; 
     break; 

     case "C": 
     path = "Customer/" + apiPath; 
     break; 
    } 

    Log.i(ApiSecurityManager.class.getSimpleName(), m_token); 

    String href = "http://tysomapi.fr3dom.net/" + path + "?token=" + m_token; 
    Log.i(ApiSecurityManager.class.getSimpleName(), href); 
    try 
    { 
     String myIp = getIp(); 
     String charset = "UTF-8"; 
     File file = new File(filePath); 

     PrintWriter writer; 
     OutputStream outputStream; 

     URL url = new URL(href); 
     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

     conn.setRequestProperty("User-Agent", "java"); 


     conn.setDoInput(true); 
     conn.setUseCaches(false); 
     conn.setRequestMethod("POST"); 
     conn.setRequestProperty("image", file.getName()); 
     conn.setRequestProperty("Content-Type", "multipart/form-data; boundary = " + boundary); 
     conn.setRequestProperty("X-Forwarded-For", myIp); 
     conn.setDoOutput(true); 
     outputStream = conn.getOutputStream(); 
     writer = new PrintWriter(new OutputStreamWriter(outputStream, charset), true); 

     writer.append(twoHyphens + boundary + LINE_FEED); 
     writer.append("Content-Disposition: form-data; name=\"image\"; filename=\"" + file.getName() + "\"" + LINE_FEED); 
     writer.append("ContentType: image/peg" + LINE_FEED); 
     writer.append(twoHyphens + boundary + LINE_FEED); 
     writer.flush(); 

     writer.append(twoHyphens + boundary + LINE_FEED); 
     writer.append("Content-Transfer-Encoding: binary").append(LINE_FEED); 
     writer.append(LINE_FEED); 
     writer.flush(); 

     FileInputStream inputStream = new FileInputStream(file); 
     byte[] buffer = new byte[4096]; 
     int bytesRead = -1; 
     while ((bytesRead = inputStream.read(buffer)) != -1) { 
     outputStream.write(buffer, 0, bytesRead); 
     } 
     outputStream.flush(); 
     inputStream.close(); 

     writer.append(LINE_FEED); 
     writer.flush(); 

     writer.append(LINE_FEED); 
     writer.append(twoHyphens + boundary + twoHyphens + LINE_FEED); 
     writer.close(); 

     Log.i(getClass().getSimpleName(), "Response Code: " + conn.getResponseCode()); 
     if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) 
     { 
     throw new RuntimeException("Failed : HTTP error code : " 
      + conn.getResponseCode()); 
     } 

     BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream()))); 

     String output; 

     while ((output = br.readLine()) != null) 
     { 
     result = result + output; 
     } 

     conn.disconnect(); 
    } 

    catch (
     MalformedURLException e 
     ) 

    { 
     e.printStackTrace(); 
    } 

    catch (
     IOException e 
     ) 

    { 
     e.printStackTrace(); 
    } 

    return result; 
    } 

答えて

関連する問題