我有一个链接https://www.pivotaltracker.com/file_attachments/47755990/download来下载 docx 文件。当我在为我正确下载的浏览器文件上点击此 URL 时,但当我尝试使用 Android 代码下载时,它对我不起作用。我的代码如下
public JSONObject postRequest(String url) {
OutputStream output;
InputStream inputStream = null;
String result = "";
JSONObject response = null ;
File file = null;
HttpResponse httpResponse;
try {
String directoryPath = "SirConrad";
File newFolder = new File(Environment.getExternalStorageDirectory(), directoryPath);
if (!newFolder.exists()) {
newFolder.mkdir();
}
try {
file = new File(newFolder, "sachsd.docx");
file.createNewFile();
} catch (Exception ex) {
System.out.println("ex: " + ex);
}
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
httpGet.addHeader("accessToken","29c1322d3072d8fadbf2478c61fb6ff6");
httpGet.addHeader("content_type","application/vnd.openxmlformats-officedocument.wordprocessingml.document");
//httpGet.addHeader("UserID", "4");
httpResponse = httpclient.execute(httpGet);
// 9. receive response as inputStream
inputStream = httpResponse.getEntity().getContent();
output = new FileOutputStream(file);
byte data[] = new byte[4096];
long total = 0;
int count;
while ((count = inputStream.read(data)) != -1) {
output.write(data, 0, count);
}
} catch (Exception e) {
Log.d("InputStream", e.getLocalizedMessage());
}
// 11. return result
return response;
}
PS Link http://pp.sirconrad.com/img/EDDYAdminPanelDesignBreakdown.docx可以让我下载上述代码中的附件。
注意* 我正在使用 Pivotal Tracker api 下载此文件。