java - 如何正确发送带有 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("------------------------------");
解决方案
ops,参考之前看到的大致是这种如何将数据从服务器发送到Android?
我无法想象字节有很多“-”并且需要“/r/n”......
分隔符应该是类似的东西
String delimiter = "--aaWEdFXvDF--" + "\r\n";
推荐阅读
- google-apps-script - 将查询字符串转换为请求正文响应错误
- mysql - 使用 mysql 命令行在 mysql 数据库中搜索单个字符串
- ios - App Store Connect: removing & reclaiming app to change bundle ID?
- php - 带有 Where 子句 PHP 的 Parse Server 的 GET api
- php - 使用 Curl 和 PHP 将文件发送到 Anonfile
- c - 从 C 中的文件中读取 ASCII 字符
- apache-spark - Spark Streaming Job 内存使用量随时间增加
- c# - 在 C# 中构建 UWP TabView (Microsoft.Toolkit.Uwp.UI.Controls;)
- python - TypeError: input() 接受 0 个位置参数,但给出了 1 个
- javascript - 为什么“this”关键字在以下 html 代码中不起作用?