gpt4 book ai didi

java - 如何在 Perl 中简单地输出二进制文件并从 Android (Java) 读取它?

转载 作者:行者123 更新时间:2023-12-01 04:29:32 24 4
gpt4 key购买 nike

我的代码中缺少什么?也许是标题(我尝试了很多)。图片在客户端收到,但无法读取(这意味着它一定已损坏,信息被添加或删除)。

服务器端是:

my $file = "<The path of the file>";

my $length = (stat($file)) [10];
print "Content-type: image/jpg\n";
print "Content-length: $length \n\n";

#open FH,"$file";
#binmode STDOUT;
#while(<FH>){ print }
#close FH;

binmode STDOUT;
open my $file_s,'<', $file || die "Could not open $file: $!";
my $buffer = "";

while (read($file_s, $buffer, 1024)) {
print $buffer;
}
close($file_s);

Android端是:

String filename = Environment.getExternalStorageDirectory().getPath() + "/somename.jpg";
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("<some url>");
StringBuilder response = new StringBuilder();
Charset chars = Charset.forName("UTF-8"); // Setting up the encoding

try {
HttpResponse httpResponse = httpclient.execute(httppost);
if (httpResponse.getStatusLine().getStatusCode() == 200) {
HttpEntity messageEntity = httpResponse.getEntity();
InputStream is = messageEntity.getContent();
long filesize = httpResponse.getEntity().getContentLength();
FileOutputStream fileOutput = new FileOutputStream(new File(filename));
byte[] buffer = new byte[1024];

int len;
while ((len = is.read(buffer, 0, 1024)) > 0) {
fileOutput.write(buffer, 0, len);
}

fileOutput.close();
}
}
catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

最佳答案

Perl 的正确代码是:

my $file = "<The path of the file>";

my $length = (stat($file)) [10];
print "Content-type: application/binary\n";
print "Content-length: $length \n\n";

open FH,"$file";
binmode STDOUT;
while(<FH>){
print
}
close FH;

关于java - 如何在 Perl 中简单地输出二进制文件并从 Android (Java) 读取它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18121052/

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