gpt4 book ai didi

java - 从下载链接发现文件名和扩展名

转载 作者:行者123 更新时间:2023-12-01 13:46:47 24 4
gpt4 key购买 nike

我正在尝试使用 download_url 下载文件但实际上这不是下载链接

当我点击 download_url Actual file 时下载开始,其中实际包含文件名和扩展名

现在我想发现文件名和扩展名

我已经尝试过了

FilenameUtils.getExtension(download_url); FilenameUtils.getName(download_url)

还有这个

URLUtil.guessFileName(download_url, null, null);

但是我得到一个空字符串,如何查找文件名和扩展名?

最佳答案

检查下面下载 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();
}
}

关于java - 从下载链接发现文件名和扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20308780/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com