【问题标题】:How to upload image and video on server using HttpUrl connection?如何使用 HttpUrl 连接在服务器上上传图像和视频?
【发布时间】:2012-07-09 22:10:55
【问题描述】:

我想仅使用 url 连接将视频连同该视频的缩略图上传到服务器。我可以上传视频,但我不知道如何同时上传图像。

这是我的参考代码:

            HttpURLConnection conn = null;
            DataOutputStream dos = null;
            DataInputStream inStream = null;
            String existingFileName = selectedPath;
            String lineEnd = "\r\n";
            String twoHyphens = "--";
            String boundary =  "*****";
            int bytesRead, bytesAvailable, bufferSize;
            byte[] buffer;
            int maxBufferSize = 1*1024*1024;
            String responseFromServer = "";
            String urlString = "http://xxx/yyy/zzz/hh.php;   //path of the php file on server


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


                bmThumbnail = ThumbnailUtils.createVideoThumbnail(existingFileName, 
                           Thumbnails.MICRO_KIND);


                dos = new DataOutputStream( conn.getOutputStream() );
                dos.writeBytes(twoHyphens + boundary + lineEnd);
                dos.writeBytes("Content-Disposition: form-data;name=\"uploadedfile\";filename=\"" + existingFileName +"\"" + lineEnd);
                // dos.writeBytes(bmThumbnail);
                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);

                //check server response
                int serverResponseCode = conn.getResponseCode();
                String serverResponseMessage = conn.getResponseMessage();
                Log.i("file sent to:" +existingFileName,"Code:" + serverResponseCode +"Message: "+ serverResponseMessage);

                // close streams
                Log.e("Debug","File is written");
                fileInputStream.close();
                dos.flush();
                dos.close();

有什么建议吗?提前致谢。

【问题讨论】:

标签: android file-upload http-upload


【解决方案1】:

使用多部分 http 帖子。您可以使用库来执行此操作,也可以手动编写 http 请求。

【讨论】:

  • 其实我是 android 新手。你能告诉我如何在 url 连接中使用多部分 http 帖子吗?我的代码正在正确上传视频,但我想发送该视频的缩略图以及它。
  • 那不是安卓的东西,真的。只是谷歌关于多部分帖子。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 2017-02-01
  • 2020-12-02
  • 2017-02-03
  • 2011-06-16
相关资源
最近更新 更多