我的代码中缺少什么?也许标题(我尝试了很多)。图片是在客户端接收到的,但是无法读取(这意味着它必须已损坏,必须添加或减去信息)。
服务器端是:
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;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句