Java 从下载链接查找文件名和扩展名
我正在尝试使用下载链接下载一个文件,但实际上它不是下载链接 当我点击下载链接时,下载开始,它实际上包含文件名和扩展名 现在我想发现文件名和扩展名 我试过这个 FilenameUtils.getExtension(下载url); FilenameUtils.getName(下载\u url) 还有这个 guessFileName(下载url,null,null)Java 从下载链接查找文件名和扩展名,java,android,download,Java,Android,Download,我正在尝试使用下载链接下载一个文件,但实际上它不是下载链接 当我点击下载链接时,下载开始,它实际上包含文件名和扩展名 现在我想发现文件名和扩展名 我试过这个 FilenameUtils.getExtension(下载url); FilenameUtils.getName(下载\u url) 还有这个 guessFileName(下载url,null,null) 但是我得到了一个空字符串,如何找到文件名和扩展名?检查下面的代码,下载PDF文件并直接在PDF Viewer中打开: downl
但是我得到了一个空字符串,如何找到文件名和扩展名?检查下面的代码,下载PDF文件并直接在PDF Viewer中打开:
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();
}
}
downloadButton.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串URL=“www.XXXXX.com”;
if(NetworkUtil.isConnectingToInternet(getActivity())){
downloadPDFAsyncTask pdfAsyncTask=新的downloadPDFAsyncTask();
pdfAsyncTask.execute();
}否则{
NetworkUtil.showDialog(getActivity(),
R.string.internetTitle,
R.string.internetMessage);
}
}
});
}捕获(NullPointerException e){
e、 printStackTrace();
}
}
私有类下载Pdfasynctask扩展
异步任务{
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
试一试{
NetworkUtil.showProgressDialog(getActivity());
}捕获(例外e){
e、 printStackTrace();
}
}
@凌驾
受保护的字符串背景(字符串…aurl){
试一试{
System.out.println(“URL>>>>>”+URL);
URL=新URL(URL);
HttpURLConnection urlConnection=(HttpURLConnection)url
.openConnection();
urlConnection.setRequestMethod(“GET”);
urlConnection.setDoOutput(true);
//连接
urlConnection.connect();
checkAndCreateDirectory(“/fileDirectory”);
file=新文件(rootDir,“fileDirectory”);
System.out.println(“文件>>>>”+文件);
FileOutputStream fileOutput=新的FileOutputStream(文件);
//用于从internet读取数据的流
InputStream InputStream=urlConnection.getInputStream();
//这是我们正在下载的文件的总大小
totalSize=urlConnection.getContentLength();
System.out.println(“totalSize>>>>>”+totalSize);
//创建一个缓冲区。。。
字节[]缓冲区=新字节[1024*1024];
int bufferLength=0;
而((bufferLength=inputStream.read(buffer))>0){
fileOutput.write(buffer,0,bufferLength);
downloadedSize+=缓冲区长度;
}
//完成后关闭输出流//
fileOutput.close();
}捕获(最终格式错误){
e、 printStackTrace();
}捕获(最终IOE例外){
e、 printStackTrace();
}捕获(最终异常e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串未使用){
super.onPostExecute(未使用);
试一试{
hideProgressDialog(getActivity());
PackageManager PackageManager=getActivity()
.getPackageManager();
Intent testIntent=新的意图(Intent.ACTION\u视图);
testIntent.setType(“application/pdf”);
List List=packageManager.QueryInputActivities(测试内容,
PackageManager.MATCH_(仅限默认值);
if(list.size()>0&&file.isFile()){
意图=新意图();
intent.setAction(intent.ACTION\u视图);
Uri=Uri.fromFile(文件);
setDataAndType(uri,“应用程序/pdf”);
星触觉(意向);
}否则{
NetworkUtil
.showDialog2(getActivity(),“错误”,
“您的设备中未安装PDF阅读器应用程序”);
}
}捕获(NullPointerException e){
e、 printStackTrace();
}捕获(例外e){
e、 printStackTrace();
}
}
}
//函数来验证目录是否存在
public void checkAndCreateDirectory(字符串dirName){
File new_dir=新文件(rootDir+dirName);
如果(!new_dir.exists()){
新目录mkdirs();
}
}