【问题标题】:Send image file in to web server将图像文件发送到 Web 服务器
【发布时间】:2012-03-08 07:02:53
【问题描述】:

我尝试将图像文件发送到网络服务器并按照现有教程进行操作,但是当我执行此方法时出现错误。我从数据库中调用路径图像,因此本地名称与数据库相同的所有图像都可以上传。我的方法是这样的:

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)

我不知道我的代码会发生什么。我认为这是正确的。任何人都可以帮助我吗??

【问题讨论】:

  • 请在 com.sat.alfaloc.MenuUtama 类的 doFileUpload(...) 方法中的某处指定第 232 行。
  • 第 232 行是 inStream = new DataInputStream ( conn.getInputStream() ); ..谢谢

标签: android image upload web


【解决方案1】:

我认为当您尝试创建一个 conn 对象时,您的代码会流入第一个 try-catch 块的一个 catch 子句。随后,您在为空的 conn 对象上调用 getInputStream()。我怀疑当您尝试连接到脚本时发生了 IOException

http://10.234.165.232/uploader.php. 

要问自己两个问题: 10.234.165.232 是否可以 ping 通? 10.234.165.232 是否有一个 Web 服务器在端口 80 上侦听?

只有我的两分钱。希望你能尽快解决你的问题! :)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-07
    相关资源
    最近更新 更多