【问题标题】:How to capture an image and store on server using android如何使用android捕获图像并存储在服务器上
【发布时间】:2011-06-26 19:35:47
【问题描述】:

我正在尝试将图像从 Android 设备上传到远程服务器。到目前为止,我有以下代码,但保存在远程服务器上的图像始终为零字节。

     HttpURLConnection conn;

     String serverpath = "http://110.172.27.47:9499";
     String MREPORTER_SERVLET_PATH="/mreporter/servlet/MReporterServlet";


     url = serverpath + MREPORTER_SERVLET_PATH;

     try
     {
         URL url = new URL( serverpath + MREPORTER_SERVLET_PATH );
         conn = (HttpURLConnection) url.openConnection();
         conn.setDoInput(true);
         conn.setDoOutput(true);
         conn.setUseCaches(false);

         conn.setRequestMethod("POST");

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

         conn.setRequestProperty(USER_NAME_REQUEST_PARAMETER, "parth");
         conn.setRequestProperty(EVENT_NAME_REQUEST_PARAMETER, "null" );
         conn.setRequestProperty(CAMERAID_REQUEST_PARAMETER, "1");
         conn.setRequestProperty(ACTION_MODE_PARAMETER, "POST_DATA_ACTION" );

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

         Log.i("Response", conn.getResponseCode() + "" );

         int response = conn.getResponseCode();

         if ( response == 200 )
         {
             Log.i("size",data.length + "");

             if ( data.length > 0 )
             {
                 dos.write(data);
                 Log.i("200","OK" );
             }
             else
             {
                 Log.i( "Error", response +  "" );
             }
         }

【问题讨论】:

    标签: android image-uploading


    【解决方案1】:

    我查看了代码并在这里找到了解决方案,它确实有效。

         HttpURLConnection conn;
         String serverpath = "http://110.172.27.47:9499";
         String MREPORTER_SERVLET_PATH="/mreporter/servlet/MReporterServlet";
    
    
         url = serverpath + MREPORTER_SERVLET_PATH;
    
         try
         {
             URL url = new URL( serverpath + MREPORTER_SERVLET_PATH );
             conn = (HttpURLConnection) url.openConnection();
             conn.setDoInput(true);
             conn.setDoOutput(true);
             conn.setUseCaches(false);
    
             conn.setRequestMethod("POST");
    
             conn.setRequestProperty("Connection", "Keep-Alive"); 
             conn.setRequestProperty("Content-Type", "multipart/form-data;boundary="+ "*****" );
    
             conn.setRequestProperty(USER_NAME_REQUEST_PARAMETER, "parth");
             conn.setRequestProperty(EVENT_NAME_REQUEST_PARAMETER, "null" );
             conn.setRequestProperty(CAMERAID_REQUEST_PARAMETER, "1");
             conn.setRequestProperty(ACTION_MODE_PARAMETER, "POST_DATA_ACTION" );
    
             DataOutputStream dos = new DataOutputStream(conn.getOutputStream());
    
             Log.i("Response", conn.getResponseCode() + "" );
    
             int response = conn.getResponseCode();
             dos.write(data);
    }
    

    【讨论】:

    • 不要把你的回答作为评论给出适当的规范(如果你知道的话),这样它对其他人也有用
    【解决方案2】:

    我认为这个example 可以帮助你。

    【讨论】:

      猜你喜欢
      • 2017-03-14
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 2015-03-02
      • 1970-01-01
      • 2019-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多