gpt4 book ai didi

android - 如何在 Android 的服务器上发布大视频?

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:00:02 24 4
gpt4 key购买 nike

我正在尝试发布一个大视频(将近 1 GB)。

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

我也使用 HTTP 将视频发送到服务器,作为 Base64 enoded 字符串发送,但是在编码时出现内存不足异常。

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

最佳答案

使用 HTTP POST,并将内容发布为 Form-based File Upload (MIME 类型:多部分/表单数据)。该系统是网络上用于发送表单和/或上传文件的标准系统。

使用 HTTP chunked post 模式,这样就不需要事先知道大小,您可以将任何文件分成小部分进行流式传输。您仍然需要在服务器上编写一些代码(例如在 PHP 中)以接受文件并执行所需的操作。

使用 HttpURLConnection 发起连接。然后使用我的附加类发送数据。它将创建适当的标题等,您将使用它作为 OutputStream 将原始数据写入其中,然后调用关闭,您就完成了。您可以重写它的 onHandleResult 来处理产生的错误代码。

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 - 如何在 Android 的服务器上发布大视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16977605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com