我遇到以下问题。
但是下载后,我无法打开设备上的文件。 有人知道问题是什么吗?
String file = "18228";
HttpClient httpclient=new MyHttpClient(mContext); // MyHttpCient handles https
HttpPost httppost= new HttpPost("https://example.com/json/index.php"); // make sure the url is correct.
HttpResponse response=httpclient.execute(httppost);
nameValuePairs.add(new BasicNameValuePair("requestFile",file));
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
File SDCardRoot = Environment.getExternalStorageDirectory();
File fileDownload = new File(SDCardRoot + "/MyFiles" , "General-information-Example.jpg");
InputStream input = null;
OutputStream output = null;
byte[] buffer = new byte[1024];
try {
System.out.println("Downloading file...");
input = response.getEntity().getContent();
output = new FileOutputStream(fileDownload);
for (int length; (length = input.read(buffer)) > 0;) {
output.write(buffer, 0, length);
}
System.out.println("File successfully downloaded!");
} finally {
if (output != null) try { output.close(); } catch (IOException logOrIgnore) {}
if (input != null) try { input.close(); } catch (IOException logOrIgnore) {}
}

<?php
header('Content-Type: '.$loadFile['header'].'');
header('Content-Length: ' . filesize($loadFile['file']));
header('Pragma: public');
header('Accept-Ranges: bytes');
header('Cache-Control: no-cache');
header('Content-Disposition: filename="' . basename($loadFile['file']));
header('X-Pad: avoid browser bug');
readfile($loadFile['file']);
exit;
?>
&#13;
04-12 12:52:35.080 4822-4999/info.androidhive.customlistviewvolley I/System.out﹕ Downloading file...
04-12 12:52:47.410 4822-4999/info.androidhive.customlistviewvolley I/System.out﹕ File successfully downloaded!
04-12 12:53:08.370 4822-4822/info.androidhive.customlistviewvolley D/OpenGLRenderer﹕ Flushing caches (mode 0)
04-12 12:53:08.400 4822-4822/info.androidhive.customlistviewvolley D/OpenGLRenderer﹕ Flushing caches (mode 1)
&#13;
答案 0 :(得分:0)
在关闭输出之前放置output.flush();
你的finally语句应该是这样的:if (output != null) try {output.flush(); output.close(); } catch (IOException logOrIgnore) {}
答案 1 :(得分:0)