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

发布于 2024-10-17 10:11:50 字数 1561 浏览 2 评论 0原文

我正在尝试将图像从 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 +  "" );
             }
         }

I am trying to upload an image from an Android device to a remote server. 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 +  "" );
             }
         }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿童谣 2024-10-24 10:11:50

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

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

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);
}
梦情居士 2024-10-24 10:11:50

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

I think this example can help you out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文