我正在使用通用Android WebView应用程序创建应用程序来访问网页。此网页有一些可下载的pdf,仅供登录用户使用。 内置的下载管理器不起作用,因为pdf是由插件生成的,并通过http响应发送。 我尝试自己实现连接,调整此代码http://www.codejava.net/java-se/networking/use-httpurlconnection-to-download-file-from-an-http-url
它工作正常,连接建立得很好,但是文件没有下载,因为显然收到的Content-Length是-1。可能是什么问题?
以下是代码:
public class HttpDownloadUtility extends AsyncTask<String, Integer, String> {
private static final int BUFFER_SIZE = 4096;
/**
* Downloads a file from a URL
* @param fileURL HTTP URL of the file to be downloaded
* @param saveDir path of the directory to save the file
* @throws IOException
*/
public String downloadFile(String fileURL, String saveDir) throws IOException {
String fileName = "";
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestProperty("Cookie", android.webkit.CookieManager.getInstance().getCookie("http://mywebsite.com"));
httpConn.connect();
int responseCode = httpConn.getResponseCode();
// always check HTTP response code first
if (responseCode == HttpURLConnection.HTTP_OK) {
String disposition = httpConn.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println(url.toString());
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out.println("No file to download. Server replied HTTP code: " + responseCode);
}
httpConn.disconnect();
return fileName;
}
@Override
protected String doInBackground(String... url) {
String downloadedFile = "";
try{
downloadedFile = downloadFile(url[0], Environment.DIRECTORY_DOWNLOADS);
}catch(Exception e){
System.out.println(e.toString());
}
return downloadedFile;
}
}
答案 0 :(得分:0)
我刚发现DownloadManager可以配置我需要的cookie。这是代码,以防任何人需要它(我不再使用我在问题中提到的类)。
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "mypdf.pdf");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(1);
request.addRequestHeader("Cookie", CookieManager.getInstance().getCookie(url));
DownloadManager manager = (DownloadManager)getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
manager.enqueue(request);