2011-11-29 22 views
1

ファイルを書き込んでアップロードしようとしていますが、ファイルが正しく書き込まれていないようです(後でアップロードする必要があり、クラッシュしてファイルがありません)。私はGoogleのdocumentationのガイドラインに従っています。ここに私のコードだ:ファイルへのAndroidの書き込み

String fileLocation = "Hello"; 
    String TESTSTRING = new String("Hello Android"); 

    FileOutputStream fOut = openFileOutput(fileLocation, MODE_WORLD_READABLE); 

    fOut.write(TESTSTRING.getBytes()); 
    fOut.close(); 

私がアップロードしようとしている方法ですそれ:

HttpURLConnection connection = null; 
    DataOutputStream outputStream = null; 
    DataInputStream inputStream = null; 

    String pathToOurFile = fileLocation; 

    String Tag = "UPLOADER"; 
    HttpURLConnection conn = null; 



    String urlServer = "http://..."; //my server 
     String lineEnd = "\r\n"; 
     String twoHyphens = "--"; 
     String boundary = "*****"; 
     try { 
      // ------------------ CLIENT REQUEST 

      Log.e(Tag, "Inside second Method"); 

      FileInputStream fileInputStream = new FileInputStream(new File(fileLocation)); 
      // open a URL connection to the Servlet 
      URL url = new URL(urlServer); 
      // Open a HTTP connection to the URL 
      conn = (HttpURLConnection) url.openConnection(); 
      // Allow Inputs 
      conn.setDoInput(true); 
      // Allow Outputs 
      conn.setDoOutput(true); 
      // Don't use a cached copy. 
      conn.setUseCaches(false); 
      // Use a post method. 
      conn.setRequestMethod("POST"); 

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

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

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

      dos.writeBytes(twoHyphens + boundary + lineEnd); 
      dos 
        .writeBytes("Content-Disposition: post-data; name=uploadedfile;filename=" 
          + fileLocation + "" + lineEnd); 
      dos.writeBytes(lineEnd); 

      Log.e(Tag, "Headers are written"); 

      // create a buffer of maximum size 

      int bytesAvailable = fileInputStream.available(); 
      int maxBufferSize = 1000; 
      // int bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      byte[] buffer = new byte[bytesAvailable]; 

      // read file and write it into form... 

      int bytesRead = fileInputStream.read(buffer, 0, bytesAvailable); 

      while (bytesRead > 0) { 
       dos.write(buffer, 0, bytesAvailable); 
       bytesAvailable = fileInputStream.available(); 
       bytesAvailable = Math.min(bytesAvailable, maxBufferSize); 
       bytesRead = fileInputStream.read(buffer, 0, bytesAvailable); 
      } 

      // send multipart form data necesssary after file data... 

      dos.writeBytes(lineEnd); 
      dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

      // close streams 
      Log.e(Tag, "File is written"); 
      fileInputStream.close(); 
      dos.flush(); 
      dos.close(); 

     } catch (MalformedURLException ex) { 
      Log.e(Tag, "error: " + ex.getMessage(), ex); 
     } 

     catch (IOException ioe) { 
      Log.e(Tag, "error: " + ioe.getMessage(), ioe); 
     } 

     try { 
      BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      String line; 
      while ((line = rd.readLine()) != null) { 
       Log.e("Dialoge Box", "Message: " + line); 
      } 
      rd.close(); 

     } catch (IOException ioex) { 
      Log.e("MediaPlayer", "error: " + ioex.getMessage(), ioex); 
     } 
    } 

ここでは、サーバー上のPHPコードは次のとおりです。

$target_path = "./"; 
$target_path = $target_path . basename($_FILES['uploadedfile']['name']); 
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { 
echo "The file ". basename($_FILES['uploadedfile']['name']). 
" has been uploaded"; 
} else{ 
echo "There was an error uploading the file, please try again!"; 
} 
+0

例外はありますか? – gigadot

+0

どの部分がクラッシュしたか、ファイルを書き込んだり、ファイルを読み込んだりしていますか? – gyoda

+0

@gigadotいいえ。 – Tring

答えて

2

代わりの

FileInputStream fileInputStream = new FileInputStream(new File(fileLocation)); 
を使用して

使用

私はあなたが/そのようなルートディレクトリにファイルを書き込む必要があることをわからない

fileLocation = context.getFilesDir() + "Hello"; 

FileInputStream fileInputStream = openFileInput(fileLocation); 
+0

ありがとう!それはもはやクラッシュしませんが、ファイルはまだアップロードされていません。あなたはコードを見ることができますか?最初の投稿をサーバーのPHPコードで更新しました。 – Tring

+0

私は本当にPHPの人ではないので、私はそれを渡す必要があります。この質問はAndroidファイルの書き込み/読み込みの詳細なので、PHPコードで新しい質問を開くことをおすすめします。それがあなたを助けたら私の答えを受け入れる;) – gyoda

+0

もちろん!再度、感謝します。 – Tring

0

はこのような何かを試してみてください。

+0

ありがとうジョー、私はそれを私のアプリのローカルストレージ(ルートではない)に書き込むと思います。とにかく問題はそれを読み返して書くことであった。今解決されました! – Tring

0

まず、サーバーにファイルを送信するよりも、このような文字列を書き込みます。それはあるものを助けるでしょう。

 String resp = "Hello Andrid!!!"; 
     File file= new File("/sdcard/hello.xml");       
     FileOutputStream fos = new FileOutputStream(file); 

     try { 
      fos.write(resp.getBytes()); 
      fos.flush(); 
      fos.close();     
      Log.d("File Write is success","fine"); 
     } catch (Exception e) { 
      Log.d("Error in File write: ", ""+e.getMessage()); 
     } finally { 
      if (fos != null) { 
       fos = null; 
      } 
     } 
関連する問題