繁体   English   中英

如何正确发送带有 DataOutPutStream 的 http 发布请求,以便服务器可以处理它

[英]How send the http post request with DataOutPutStream properly so server can handle it

我希望有人告诉我只写一个文件和一个带有另一种字节的文件有什么不同。

服务器使用,python3 flask

我想也许 android retrofit 等有用,但我想尝试使用经典方法 HTTPUrlConnection 所以我成功地将一个或多个字符串参数发送到服务器。 我也成功地将文件发送到服务器。 - 我的文件将只有 5 秒的音频或视频 mp4,由真正的 android 创建。

当我只尝试两个参数和一个字节列表 len(list) = 2 时,我可以取回我发送的文件,但是字节的 concat 样式无法实现。

但是当我将两者结合起来时,我发现当文件被切成多部分时,文件就无法恢复。

我知道分隔符很有用,我尝试用一串“----------”将其拆分到服务器端。

list= request.data.split(b"------------------------------")
newList= list[1:]
data = b""
for part in newList:
    data += part 

我如何恢复文件(python)

def createAudioFromDataReceived(fileName, data):
with open(fileName, 'wb') as wfile:
    wfile.write(data)

写入 dataOutPutStream 的基本代码

public void writeFilesParamToDataOutputStream(HttpURLConnection conn, File file, String action) throws IOException {

    byte[] buffer;
    FileInputStream fileInputStream = new FileInputStream(file);
    DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

    buffer = new byte[1024 * 1024];

    int length = 0;
    while ( ( length = fileInputStream.read( buffer ) ) > 0 ) {
        dos.write(buffer, 0, length);
    }

    dos.flush();
    fileInputStream.close();
    dos.close();
}

向 dataOutputStream 添加额外的行

//Bytes
byte[] bytes = "toSend".getBytes();
dos.write(bytes);
dos.write("------------------------------");

ops,之前看到的参考大致是这种如何将数据从服务器发送到Android?

我无法想象字节有很多“-”并且需要“/r/n”...

分隔符应该是类似的东西

String delimiter = "--aaWEdFXvDF--" + "\r\n";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM