首页 > 解决方案 > 如何正确发送带有 DataOutPutStream 的 http post 请求,以便服务器可以处理它

问题描述

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

服务器使用,python3烧瓶

我认为也许 android 改造等有用,但我想尝试使用经典方法 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("------------------------------");

标签: javapythonandroid

解决方案


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

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

分隔符应该是类似的东西

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

推荐阅读