2012-03-08 24 views
0

イメージファイルをWebサーバーに送信しようとしましたが、既存のチュートリアルに従いますが、このメソッドを実行するとエラーが発生します。データベースからパス画像を呼び出すので、データベースと同じローカル名の画像をすべてアップロードできます。私のこのような方法:イメージファイルをWebサーバーに送信

private void doFileUpload(){ 
HttpURLConnection conn = null; 
DataOutputStream dos = null; 
DataInputStream inStream = null; 
Cursor c = helper.getUpImage(almagId); 
c.moveToFirst(); 
String existingFileName = helper.getGam1(c); 
String lineEnd = "\r\n"; 
String twoHyphens = "--"; 
String boundary = "*****"; 
int bytesRead, bytesAvailable, bufferSize; 
byte[] buffer; 
int maxBufferSize = 1*1024*1024; 
String responseFromServer = ""; 
String urlString = "http://10.234.165.232/uploader.php"; 
try 
{ 
    //------------------ CLIENT REQUEST 

FileInputStream fileInputStream = new FileInputStream(new File(existingFileName)); 
    // open a URL connection to the Servlet 
    URL url = new URL(urlString); 
    // 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); 
    dos = new DataOutputStream(conn.getOutputStream()); 
    dos.writeBytes(twoHyphens + boundary + lineEnd); 
    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + existingFileName + "\"" + 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); 
    // close streams 
    Log.e("Debug","File is written"); 
    fileInputStream.close(); 
    dos.flush(); 
    dos.close(); 
} 
catch (MalformedURLException ex) 
{ 
     Log.e("Debug", "error: " + ex.getMessage(), ex); 
} 
catch (IOException ioe) 
{ 
     Log.e("Debug", "error: " + ioe.getMessage(), ioe); 
} 
//------------------ read the SERVER RESPONSE 
try { 
     inStream = new DataInputStream (conn.getInputStream()); 
     String str; 

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

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

そして、これは私が私のcode.Iで起こるかわからない

03-08 13:49:46.174: ERROR/AndroidRuntime(1277): FATAL EXCEPTION: main 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): java.lang.NullPointerException 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.sat.alfaloc.MenuUtama.doFileUpload(MenuUtama.java:232) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.sat.alfaloc.MenuUtama.onOptionsItemSelected(MenuUtama.java:113) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at android.app.Activity.onMenuItemSelected(Activity.java:2195) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at android.view.View$PerformClick.run(View.java:8816) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at android.os.Handler.handleCallback(Handler.java:587) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at android.os.Handler.dispatchMessage(Handler.java:92) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at android.os.Looper.loop(Looper.java:123) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at java.lang.reflect.Method.invokeNative(Native Method) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at java.lang.reflect.Method.invoke(Method.java:521) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-08 13:49:46.174: ERROR/AndroidRuntime(1277): at dalvik.system.NativeStart.main(Native Method) 

私のエラーですが、これはcorrect.Anyoneが私を助けることができると思い??

+0

com.sat.alfaloc.MenuUtamaクラス内のdoFileUpload(...)メソッドのどこかに232行目を指定してください。行232の –

+0

はこの 'inStream = new DataInputStream(conn.getInputStream());' ..ありがとう – akubabas

答えて

0

私は、あなたのコードが最初のtry-catchブロックのcatch節の1つに流れ込むと思います。次に、nullのconnオブジェクトに対してgetInputStream()を呼び出します。 は10.234.165.232ピング-ことができます:私はあなたが自問する

http://10.234.165.232/uploader.php. 

二つの質問でスクリプトに接続しようとしたときにIOExceptionが発生したと思われますか? 10.234.165.232にポート80でリッスンしているWebサーバーがありますか?

ちょうど私の2セントです。すぐにあなたの問題を解決したいと思っています! :)

関連する問題