私のコードには何が欠けていますか? たぶんヘッダーです(私はそれらの多くを試しました)。画像はクライアント側で受信されますが、読み取ることができません (つまり、情報が追加または削除されて破損している必要があります)。
サーバー側は次のとおりです。
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();
}