简体   繁体   English

如何使用Android捕获图像并将其存储在服务器上

[英]How to capture an image and store on server using android

I am trying to upload an image from an Android device to a remote server. 我正在尝试将图像从Android设备上传到远程服务器。 So far I have the following code but the image being saved on the remote server is always zero bytes. 到目前为止,我有以下代码,但是保存在远程服务器上的图像始终为零字节。

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

I review thw code and get the solution here it works surely. 我查看了这些代码,并在这里确定了可以正常使用的解决方案。

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

我认为这个例子可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM