gpt4 book ai didi

java - 如何在php中读取字节数组

转载 作者:行者123 更新时间:2023-11-29 07:59:23 24 4
gpt4 key购买 nike

我正在使用以下代码向服务器发送一个 mp3 文件。在服务器中,我想要一个 php 代码来接收这个 mp3 字节数组并将其转换为文件并存储在那里。

try {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
File file = new File(Environment.getExternalStorageDirectory()+ "/my.mp3");
final InputStream in = new FileInputStream(file);
final byte[] buf = new byte[2048];
int n;
while ((n = in.read(buf)) >= 0) {
out.write(buf, 0, n);
}
final byte[] data = out.toByteArray();
String urlString = "http://10.0.0.56/upload.php";
HttpPost postRequest = new HttpPost(urlString);
postRequest.setEntity(new ByteArrayEntity(data));
HttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(postRequest);
HttpEntity entity = response.getEntity();
InputStream ins = entity.getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(ins));
String temp_str;
StringBuilder sb = new StringBuilder();
while((temp_str = br.readLine()) != null) {
sb.append(temp_str);
}
Log.e("response", sb.toString());
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
return "exception";
}

任何人都知道如何使用 php 读取 mp3 文件。

最佳答案

您的 upload.php 可能看起来像这样:

$mp3_bin = file_get_contents('php://input');
file_put_contents( '/path/to/my/mp3', $mp3_bin );

简短而甜美;)

编辑:

# A bit of golfing gives a one liner:
file_put_contents('/path/to/my.mp3', file_get_contents('php://input'));

关于java - 如何在php中读取字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15429910/

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