我正试图发布一个大视频(近1GB)。
我正在使用FTP发送视频到服务器,但上传停止了一段时间。在服务器上视频崩溃,但我可以上传一个更小的视频。
我还使用HTTP将视频发送到服务器,作为Base64加密字符串发送,但在编码过程中出现了内存中的异常。
我试着把视频上传成一个文件,但没有成功。将大视频上传到服务器的最佳方式是什么?
发布于 2013-07-03 13:50:54
使用HTTP和POST内容作为基于表单的文件上传 (mime类型: multipart/form-data)。本系统是网络上发送表单和/或上传文件的标准。
使用HTTP块post模式,这样大小就不需要事先知道,而且您可以将任何文件流成小部分。您仍然需要在服务器上编写一些代码(例如,在PHP中)来接受该文件并执行所需的操作。
使用HttpURLConnection启动连接。然后使用我附加的类发送数据。它将创建适当的标头等,您将使用它作为OutputStream向其写入原始数据,然后调用close,您就完成了。您可以对它的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);
}
}发布于 2014-03-24 13:35:26
私有HttpsURLConnection conn = null;
Conn.setDoInput(真);
Conn.setDoOutput(真);
Conn.setUseCaches(假);
conn.setChunkedStreamingMode(1024);
https://stackoverflow.com/questions/16977605
复制相似问题