我试图获取从PDF文件:无法提取PDF文件作为二进制数据
网址:https://domain_name/xyz/_id/download/
其中它不指向一个直接的PDF文件和每个独特的文件被下载 解释一个特定的012字段。
我把浏览器和PDF文件的地址栏这个环节被立即下载, 而当我试图通过HttpsURLConnection的它的内容类型是“text/html的”形式, 而应该把它牵在'application/pdf'中。
我连接,但文件总是“text/html的”形式获取下载之前还试图“调用setRequestProperty”到“应用程序/ PDF”。
方法我使用的是“GET”
1)我需要使用的HttpClient,而不是HttpsURLConnection的吗?
2)这些类型的链接是用来提高安全性吗?
3)请指出我的错误。
4)如何知道服务器上存在的文件名?
我下面主要代码粘贴,我已经实现了:
URL url = new URL(sb.toString());
//created new connection
HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
//have set the request method and property
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/pdf");
Log.e("Content Type--->", urlConnection.getContentType()+" "+ urlConnection.getResponseCode()+" "+ urlConnection.getResponseMessage()+" "+urlConnection.getHeaderField("Content-Type"));
//and connecting!
urlConnection.connect();
//setting the path where we want to save the file
//in this case, going to save it on the root directory of the
//sd card.
File SDCardRoot = Environment.getExternalStorageDirectory();
//created a new file, specifying the path, and the filename
File file = new File(SDCardRoot,"example.pdf");
if((Environment.getExternalStorageState()).equals(Environment.MEDIA_MOUNTED_READ_ONLY))
//writing the downloaded data into the file we created
FileOutputStream fileOutput = new FileOutputStream(file);
//this will be used in reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
//this is the total size of the file
int totalSize = urlConnection.getContentLength();
//variable to store total downloaded bytes
Log.e("Total File Size ---->", ""+totalSize);
int downloadedSize = 0;
//create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0; //used to store a temporary size of the buffer
//Reading through the input buffer and write the contents to the file
while ((bufferLength = inputStream.read(buffer)) > 0) {
//add the data in the buffer to the file in the file output stream (the file on the sd card
fileOutput.write(buffer, 0, bufferLength);
//adding up the size
downloadedSize += bufferLength;
//reporting the progress:
Log.e("This much downloaded---->",""+ downloadedSize);
}
//closed the output stream
fileOutput.close();
我寻觅了很多,无法得到结果。 如果可能,请尝试详细说明我的错误,因为我第一次实施这个 的事情。
**试图直接读取PDF链接,如:http://labs.google.com/papers/bigtable-osdi06.pdf ,他们很容易被下载的,而且他们的 'Content-Type的' 也是 '应用程序/ PDF' **
感谢。
您是否检查过服务器响应的MIME类型? – 2011-03-10 08:05:41