2012-05-08 12 views

答えて

0

私は自分のアプリでこのコードを使用しました。その働いた。それはあなたを助けるかもしれません。

private String doFileUpload() { 

    final DataOutputStream dos; 

    String exiting_file_path = filePath; 
    String boundary = "*****"; 
    String twohypens = "--"; 
    String lineend = "\r\n"; 
    int maxBufferSize = 1 * 1024 * 1024; 
    int buferSize; 
    int bytesRead; 
    String url_string = "your URL"; 

    try { 
     Log.d("Debug", "Start Uploading"); 
     FileInputStream fileInStream = new FileInputStream(
       exiting_file_path); 
     URL url = new URL(url_string); 
     httpUrlConn = (HttpURLConnection) url.openConnection(); 

     httpUrlConn.setDoInput(true); 
     httpUrlConn.setDoOutput(true); 
     httpUrlConn.setUseCaches(false); 
     httpUrlConn.setRequestMethod("POST"); 
     httpUrlConn.setRequestProperty("connection", "Keep-Alive"); 
     httpUrlConn.setRequestProperty("content-type", 
       "multipart/form-data;boundary=" + boundary); 

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

     dos.writeBytes(twohypens + boundary + lineend); 
     dos.writeBytes("content-disposition:form-data; name=\"upload_file\"; filename=\"" 
       + exiting_file_path + "\"" + lineend); 
     dos.writeBytes(lineend); 

     int buferAvailable = fileInStream.available(); 
     buferSize = Math.min(buferAvailable, maxBufferSize); 
     final byte[] buffer = new byte[buferSize]; 

     bytesRead = fileInStream.read(buffer, 0, buferSize); 

     while (bytesRead > 0) { 
      dos.write(buffer, 0, buferSize); 
      buferAvailable = fileInStream.available(); 
      buferSize = Math.min(buferAvailable, maxBufferSize); 
      bytesRead = fileInStream.read(buffer, 0, buferSize); 
     } 
     dos.writeBytes(lineend); 
     dos.writeBytes(twohypens + boundary + twohypens + lineend); 
     fileInStream.close(); 
     dos.flush(); 
     dos.close(); 

    } catch (FileNotFoundException e) { 
     Toast.makeText(getApplicationContext(), "" + e, 1).show(); 
     e.printStackTrace(); 
    } catch (MalformedURLException e) { 
     Toast.makeText(getApplicationContext(), "" + e, 1).show(); 
     e.printStackTrace(); 
    } catch (IOException e) { 
     Toast.makeText(getApplicationContext(), "" + e, 1).show(); 
     e.printStackTrace(); 
    } 

    try { 
     DataInputStream inStream = new DataInputStream(
       httpUrlConn.getInputStream()); 

     while ((STRRRR = inStream.readLine()) != null) { 
      Log.d("Debug", "Server Response " + STRRRR); 
     } 
     inStream.close(); 

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

    return STRRRR; 
} 
+0

whats STRRRR ???? – antonoVodka

+0

これは単なるStringです。サーバーからの応答を取得するアップロードされた詳細ファイルを表示します。もう1つPHP Webサービスを使用しますか? –

0

あなたはおそらく「http://example.com」として指定するように、URLと面白いことをしたと、サーバーはすぐに「http://example.com/」あなたをリダイレクトするには、(上のスラッシュに注意してください末尾)をリダイレクトコード302で置き換えます。

サーバーから返されたヘッダーをチェックします。通常、サーバーには正しい場所が含まれています。

+0

これをチェックするには?サーバーから返されたヘッダーを確認します。通常、サーバーには正しい場所が含まれています。 – antonoVodka

関連する問題