1

What am I missing in my code? Maybe headers (I tried a lot of them). The picture is received on the client side, but it cannot read it (meaning it must have been corrupted, either information is added or subtracted).

The server side is:

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);

The Android side is:

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();
}
4

1 回答 1

0

The right code for Perl is:

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;
于 2013-08-08T08:18:30.150 回答