2012-03-08 115 views
0

我嘗試發送圖像文件到網絡服務器,我按照現有的教程,但是當我執行這個方法時,我有一個錯誤。我從數據庫調用路徑圖像,因此本地名稱中與圖像數據庫相同的所有圖像都可以上傳。我喜歡這種方法:發送圖像文件到網絡服務器

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); 
} 
} 

這是我的錯誤

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) 

我不知道發生了什麼我code.I認爲這是correct.Anyone能幫助我嗎?

+0

請指定com.sat.alfaloc.MenuUtama類中doFileUpload(...)方法中哪一行是232行。第232行的 – 2012-03-08 07:15:06

+0

是這個'inStream = new DataInputStream(conn.getInputStream());'..謝謝你 – akubabas 2012-03-08 07:41:37

回答

0

我想當你嘗試創建一個conn對象時,你的代碼會流入第一個try-catch塊的catch子句之一。然後,隨後調用conn對象上的getInputStream(),該對象爲null。我懷疑,一個IOException異常,當您試圖連接到腳本在

http://10.234.165.232/uploader.php. 

兩個問題要問你自己發生了: 是10.234.165.232響應ping操作? 是否有10.234.165.232的Web服務器在端口80偵聽?

只是我的兩分錢。希望你很快解決你的問題! :)