我正在尝试使用download_url下载文件,但实际上它不是下载链接
当我点击download_url Actual file下载时,实际上包含文件名和扩展名
现在我想发现文件名和扩展名
我试过这个
FilenameUtils.getExtension(DOWNLOAD_URL); FilenameUtils.getName(DOWNLOAD_URL)
还有这个
URLUtil.guessFileName(download_url,null,null);
但是我得到一个空字符串,如何查找文件名和扩展名?
答案 0 :(得分:1)
检查以下下载PDF文件并在PDF查看器中直接打开的代码:
downloadButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String URL = "www.XXXXX.com";
if (NetworkUtil.isConnectingToInternet(getActivity())) {
downloadPDFAsyncTask pdfAsyncTask = new downloadPDFAsyncTask();
pdfAsyncTask.execute();
} else {
NetworkUtil.showDialog(getActivity(),
R.string.internetTitle,
R.string.internetMessage);
}
}
});
} catch (NullPointerException e) {
e.printStackTrace();
}
}
private class downloadPDFAsyncTask extends
AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
try {
NetworkUtil.showProgressDialog(getActivity());
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... aurl) {
try {
System.out.println("URL >>>>> " + URL);
URL url = new URL(URL);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
// connect
urlConnection.connect();
checkAndCreateDirectory("/fileDirectory");
file = new File(rootDir, "fileDirectory");
System.out.println("file >>>>> " + file);
FileOutputStream fileOutput = new FileOutputStream(file);
// Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
// this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();
System.out.println("totalSize >>>>> " + totalSize);
// create a buffer...
byte[] buffer = new byte[1024 * 1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
}
// close the output stream when complete //
fileOutput.close();
} catch (final MalformedURLException e) {
e.printStackTrace();
} catch (final IOException e) {
e.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String unused) {
super.onPostExecute(unused);
try {
NetworkUtil.hideProgressDialog(getActivity());
PackageManager packageManager = getActivity()
.getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List<?> list = packageManager.queryIntentActivities(testIntent,
PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0 && file.isFile()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
} else {
NetworkUtil
.showDialog2(getActivity(), "Error",
"PDF Reader application is not installed in your device");
}
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
// function to verify if directory exists
public void checkAndCreateDirectory(String dirName) {
File new_dir = new File(rootDir + dirName);
if (!new_dir.exists()) {
new_dir.mkdirs();
}
}