如何在Android中的服务器上发布大型视频? [英] How to post large video on a server, in Android?

查看:50
本文介绍了如何在Android中的服务器上发布大型视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试发布大型视频(将近1 GB).

I am trying to post a large video (nearly 1 GB).

我正在使用FTP将视频发送到服务器,但是一段时间后上传停止.在服务器上,视频崩溃了,但是我可以上传较小尺寸的视频.

I am using FTP to send video to a server, but the upload stops after a while. On the server the video crashes, but I am able to upload a smaller sized video.

我还使用HTTP将视频发送到服务器,以Base64加密字符串发送,但是编码时出现内存不足异常.

I've also used HTTP to send video to the server, sent as a Base64 enoded string, but there is an out of memory exception while encoding.

我尝试将视频上传为文件,但没有成功.将大型视频上传到服务器的最佳方法是什么?

I've tried to upload the video as a file, but without success. What is the best way to upload a large video to a server?

推荐答案

使用HTTP POST,并将内容发布为基于表单文件上传(MIME类型:multipart/form-data).该系统是网络上用于发送表格和/或上传文件的标准系统.

Use HTTP POST, and post content as Form-based File Upload (mime type: multipart/form-data). This system is standard on web for sending forms and/or uploading files.

使用HTTP分块发布模式,因此无需事先知道大小,并且可以一小部分地流式传输任何文件.您仍然必须在服务器上编写一些代码(例如在PHP中)以接受该文件并执行所需的操作.

Use HTTP chunked post mode, so that size doesn't need to be known beforehand, and you can stream any file in small parts. You still have to make some code on server (e.g. in PHP) to accept the file and do what is needed.

使用HttpURLConnection发起连接.然后使用我的附加类发送数据.它将创建适当的标头,等等,您将其用作OutputStream向其中写入原始数据,然后调用close,就可以完成了.您可以覆盖其onHandleResult以处理产生的错误代码.

Use HttpURLConnection to initiate connection. Then use my attached class to send the data. It will create proper headers, etc, and you'll use it as OutputStream to write your raw data to it, then call close, and you're done. You can overrite its onHandleResult to handle resulting error code.

public class FormDataWriter extends FilterOutputStream{
   private final HttpURLConnection con;

   /**
    * @param formName name of form in which data are sent
    * @param fileName
    * @param fileSize   size of file, or -1 to use chunked encoding
    */
   FormDataWriter(HttpURLConnection con, String formName, String fileName, long fileSize) throws IOException{
      super(null);
      this.con = con;
      con.setDoOutput(true);

      String boundary = generateBoundary();
      con.setRequestProperty(HTTP.CONTENT_TYPE, "multipart/form-data; charset=UTF-8; boundary="+boundary);
      {
         StringBuilder sb = new StringBuilder();
         writePartHeader(boundary, formName, fileName==null ? null : "filename=\""+fileName+"\"",
               "application/octet-stream", sb);
         headerBytes = sb.toString().getBytes("UTF-8");

         sb = new StringBuilder();
         sb.append("\r\n");
         sb.append("--"+boundary+"--\r\n");
         footerBytes = sb.toString().getBytes();
      }
      if(fileSize!=-1) {
         fileSize += headerBytes.length + footerBytes.length;
         con.setFixedLengthStreamingMode((int)fileSize);
      }else
         con.setChunkedStreamingMode(0x4000);
      out = con.getOutputStream();
   }

   private byte[] headerBytes, footerBytes;

   private String generateBoundary() {
      StringBuilder sb = new StringBuilder();
      Random rand = new Random();
      int count = rand.nextInt(11) + 30;
      int N = 10+26+26;
      for(int i=0; i<count; i++) {
         int r = rand.nextInt(N);
         sb.append((char)(r<10 ? '0'+r : r<36 ? 'a'+r-10 : 'A'+r-36));
      }
      return sb.toString();
   }

   private void writePartHeader(String boundary, String name, String extraContentDispositions, String contentType, StringBuilder sb) {
      sb.append("--"+boundary+"\r\n");
      sb.append("Content-Disposition: form-data; charset=UTF-8; name=\""+name+"\"");
      if(extraContentDispositions!=null)
         sb.append("; ").append(extraContentDispositions);
      sb.append("\r\n");
      if(contentType!=null)
         sb.append("Content-Type: "+contentType+"\r\n");
      sb.append("\r\n");
   }

   @Override
   public void write(byte[] buffer, int offset, int length) throws IOException{
      if(headerBytes!=null) {
         out.write(headerBytes);
         headerBytes = null;
      }
      out.write(buffer, offset, length);
   }

   @Override
   public void close() throws IOException{
      flush();
      if(footerBytes!=null) {
         out.write(footerBytes);
         footerBytes = null;
      }
      super.close();
      int code = con.getResponseCode();
      onHandleResult(code);
   }
   protected void onHandleResult(int code) throws IOException{
      if(code!=200 && code!=201)
         throw new IOException("Upload error code: "+code);
   }
}

这篇关于如何在Android中的服务器上发布大型视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆