本篇文章给大家谈谈如何使用.htaccess检查文件是否存在于caching中,如果没有则加载正常脚本,同时本文还将给你拓展android–如何检查文件是否存在,如果没有在异步任务的sdcard中创建
本篇文章给大家谈谈如何使用.htaccess检查文件是否存在于caching中,如果没有则加载正常脚本,同时本文还将给你拓展android – 如何检查文件是否存在,如果没有在异步任务的sdcard中创建新文件、c# – 使用Try / Catch检查文件是否存在、C++(五) access函数判断文件是否存在、C/C++.判断文件是否存在(_access)等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:- 如何使用.htaccess检查文件是否存在于caching中,如果没有则加载正常脚本
- android – 如何检查文件是否存在,如果没有在异步任务的sdcard中创建新文件
- c# – 使用Try / Catch检查文件是否存在
- C++(五) access函数判断文件是否存在
- C/C++.判断文件是否存在(_access)
如何使用.htaccess检查文件是否存在于caching中,如果没有则加载正常脚本
过去一天,我一直在使用.htaccess文件,但只有适度的成功。 我编写了一个caching脚本,它生成每个页面的caching版本,并将其存储在www.mysite.com/cache/并且保持与实际文件相同的目录结构,但是它会将.html添加到最后。 所以如果实际的文件是:
www.mysite.com/blue/turtleneck
caching文件将是:
www.mysite.com/cache/blue/turtleneck.html
我需要检查caching版本是否存在,如果存在,请加载它。 如果它不存在,我需要加载实际的文件。 另外,我需要一些强制它加载非caching版本的方法。 我在想也许在URL的最后join/nocache/来加载/nocache/副本。 例:
www.mysite.com/blue/turtleneck/nocache/
我一直在努力与此,任何帮助将非常感激。
只有存在查询string时,才会对图像文件请求重写规则
我如何在Nginx中使用.htaccess文件
stream行的技术来debugging.htaccess
如何使用htaccess或httpd.conf将所有子目录redirect到root?
如何在不发送404头的情况下redirect未find的URL?
503 htaccess文件只能部分工作
如何停止使用htaccess规则直接执行的PHP页面?
选项FollowSymLinks或SymLinksIfOwnerMatch已closures
VirtualDocumentRoot处理不存在的文件夹
使用htaccessredirect一个目录,但不是其中的文件
使用这些规则:
Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / # do not do anything for already existing files RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule .+ - [L] # serve cached file if exists RewriteCond %{DOCUMENT_ROOT}/cache/$1.html -f RewriteRule (.*) /cache/$1.html [L]
这需要放在网站根文件夹中的.htaccess文件中。 如果放置在其他地方,可能需要进行一些调整。
规则将在重写之前检查缓存是否存在。
它将检查任何不存在的资源( “#对已经存在的文件不做任何事情”规则将不会传递对真实文件的请求)。
如果你愿意的话,你可以摆脱“#对已经存在的文件做任何事情”的规则 – 它仍然应该工作正常(可能需要这样做根据您的应用程序/网站的逻辑)。
如果/cache/文件夹或任何其他文件夹不是实际的文件夹在一个网站的根,但别名,那么这很可能不会工作。
总结
以上是小编为你收集整理的如何使用.htaccess检查文件是否存在于caching中,如果没有则加载正常脚本全部内容。
如果觉得小编网站内容还不错,欢迎将小编网站推荐给好友。
android – 如何检查文件是否存在,如果没有在异步任务的sdcard中创建新文件
String pdf; String filenameWithExtension=""; protected void onCreate(Bundle savedInstanceState) { Intent intent=getIntent(); pdf=intent.getStringExtra("pdfurl"); String PATH2 = Environment.getExternalStorageDirectory()+"/pictures"; Log.v("log_tag initial path","PATH: " + PATH2); File file2 = new File(PATH2); if(file2.exists()==true) { }else { Log.v("directory is created","new dir"); file2.mkdir(); } /**extracting the pdf filname from url**/ int slashIndex = pdf.lastIndexOf('/'); int dotIndex = pdf.lastIndexOf('.'); filenameWithExtension=pdf.substring(slashIndex + 1); DownloadFile downloadFile = new DownloadFile(filenameWithExtension); downloadFile.execute(pdf); } private class DownloadFile extends AsyncTask<String,Integer,String>{ String filename=""; public DownloadFile(String _filename) { this.filename=_filename; } String PATH = Environment.getExternalStorageDirectory()+"/pictures/"+filename; File filecheck=new File(PATH); @Override protected String doInBackground(String... str) { Log.v("name of file",this.filename); if(filecheck.exists()){ Log.v("in if condition","file is alredy exist"); }else{ Log.v("in else condition","file not present...."); try { URL url = new URL(str[0]); HttpURLConnection c = (HttpURLConnection)url.openConnection(); c.setRequestMethod("GET"); c.setDoOutput(true); c.connect(); // this will be useful so that you can show a typical 0-100% progress bar int lenghtOfFile = c.getContentLength(); // downlod the file // InputStream input = new BufferedInputStream(url.openStream()); //OutputStream output = new FileOutputStream("/sdcard/somewhere/nameofthefile.ext"); String PATH = Environment.getExternalStorageDirectory()+ "/pictures/"; Log.v("log_tag","PATH: " + PATH); File file = new File(PATH); Boolean check=filecheck.createNewFile(); Log.v("check file creation..::",check.toString()); if(file.mkdirs()==false) { Log.v("file is alredy exist in sdcard","exist file"); }else { Log.v("file is created in card","new dir"); file.mkdirs(); } File outputFile = new File(file,filenameWithExtension); FileOutputStream fos = new FileOutputStream(outputFile); InputStream is = c.getInputStream(); long total = 0; byte[] buffer = new byte[1024]; int len1 = 0; while ((len1 = is.read(buffer)) != -1) { total +=len1; publishProgress((int)(total*100/lenghtOfFile)); fos.write(buffer,len1); } fos.close(); is.close(); } catch (IOException e) { Log.d("log_tag of background","Error: " + e); } } Log.v("In aync task filename extension",filenameWithExtension); return filenameWithExtension; }
解决方法
File.exists()
检查文件是否存在
File f = new File(filePathString); if(f.exists()) { /* do something */ } else { file.mkdirs(); //And your other stuffs goes here }
注意:exists()将为目录返回true
如果你想检查一个特定的文件是否存在,你必须使用File.isFile()
boolean fileExists = new File("path/to/file.txt").isFile();
新文件(“C:/”).exists()将返回true,但不允许您作为文件打开和读取.
你的代码中的问题是你的文件名是空的.
编辑2:
尝试这个 :
String filename=""; String PATH=""; File fileCheck=null; public DownloadFile(String _filename) { this.filename=_filename; PATH = Environment.getExternalStorageDirectory()+"/pictures/"+filename; filecheck=new File(PATH); }
要么
或者在AsyncTask的onPreExecute()中放置这两个语句
PATH = Environment.getExternalStorageDirectory()+"/pictures/"+filename; filecheck=new File(PATH);
c# – 使用Try / Catch检查文件是否存在
if (File.Exists("TextFile1.txt")) { MessageBox.Show("The file don't exist!","Problems!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation); }
我必须使用Try / Catch方式
try { code to check if file exist here } catch { error message here }
解决方法
C++(五) access函数判断文件是否存在
最近看到一个函数,第一觉得很sb,因为remove的定义在if内部,变成了局部变量,结果如果文件“234.bin”不存在的话,一定会出错的,因为remove的生存期有限。 结果,亮瞎我的: #includeiostream#include unistd.h#include stdio.h#include stdlib.husing na
最近看到一个函数,第一眼觉得很sb,因为remove的定义在if内部,变成了局部变量,结果如果文件“234.bin”不存在的话,一定会出错的,因为remove的生存期有限。
结果,亮瞎我的眼:
#include<iostream> #include "unistd.h" #include "stdio.h" #include "stdlib.h" using namespace std; int main() { if(access("234.bin",F_OK)) { bool remove=true; } if(remove) { cout<strong>结果各种悲剧,无论这个文件是否存在:</strong> <p><img src="/inc/test.jsp?url=http%3A%2F%2Fmy.csdn.net%2Fuploads%2F201206%2F07%2F1339078989_9217.jpg&refer=http%3A%2F%2Fblog.csdn.net%2Femaste_r%2Farticle%2Fdetails%2F7643479" alt="C++(五) access函数判断文件是否存在" ></p> <p><br></p> <p>事实上,我个人认为这个问题出在这个<ahref="https://www.php.cn/zt/16380.html" target="_blank">access</a>函数的返回值上,它的返回值是</p> <p>0 如果文件是指定的mode<br></p> <p>-1 如果出错</p> <p>所以上述程序,无论是找到文件(0),还是找不到(-1),都是false,所以应该是永远都进不了if(remove)的。。<br></p> <p>所以应该是:</p> <p><span>立即学习</span>“<a href="https://pan.quark.cn/s/6e7abc4abb9f"rel="nofollow" target="_blank">C++免费学习笔记(深入)</a>”;</p> <pre>if(0 == access("234.bin",F_OK)) { remove = true; }
原来:
remove是一个已经存在的函数,函数地址不为空,所以一直都能进 if(remove){}
大家,以后判断文件是否存在,用以下的代码比较好:
#include<iostream> #include "unistd.h" #include "stdio.h" #include "stdlib.h" using namespace std; int file_exist(char *file) { return (access(file,F_OK) == 0); } int main() { cout<strong>总结:</strong> <p>(一)用access函数注意返回值是 0 和-1,都是false<br></p> <p>(二)remove是个函数名,定义命名的时候注意不要用到系统的东东<br></p> <br></iostream>
C/C++.判断文件是否存在(_access)
1、
int _access(char* path,int mode)
头文件<io.h>
功能:确定文件或文件夹的访问权限。如果指定的存取方式有效,则函数返回0,否则函数返回-1。
参数path 是访问文件所在的路径名,mode是访问判断模式,
具体含义如下:
R_OK 只判断是否有读权限
W_OK 只判断是否有写权限
X_OK 判断是否有执行权限
F_OK 只判断是否存在
之前也使用过fopen判断文件是否存在,但_access函数更为方便。
2、代码: 环境:Win7x64,vs08x86
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <io.h>
#include <map>
#include <math.h>
#include <list>
#include <string>
#include <sstream>
#include <algorithm>// std::find(...)
#include <vector>
using namespace std;
//#include "stdafx.h"
#include <iostream>
#include <windows.h>
#include <math.h>
using namespace std;
void main()
{
int iRtn = _access("D:/G_资料_2018/20181119_xx", 0);// 这个文件夹是 存在的
printf("_access return(1) : %d\n", iRtn);
iRtn = _access("D:/G_资料_2018/20181119_xx_No", 0);// 这个文件夹是 不存在的
printf("_access return(2) : %d\n", iRtn);
iRtn = _access("D:/G_资料_2018/20181119_xx/芜湖两条线路/华二112线.g", 0);// 这个文件是 存在的
printf("_access return(3) : %d\n", iRtn);
iRtn = _access("D:/G_资料_2018/20181119_xx/芜湖两条线路/华二112线.no.g", 0);// 这个文件是 不存在的
printf("_access return(4) : %d\n", iRtn);
system("pause");
}
3、
4、
5、
今天的关于如何使用.htaccess检查文件是否存在于caching中,如果没有则加载正常脚本的分享已经结束,谢谢您的关注,如果想了解更多关于android – 如何检查文件是否存在,如果没有在异步任务的sdcard中创建新文件、c# – 使用Try / Catch检查文件是否存在、C++(五) access函数判断文件是否存在、C/C++.判断文件是否存在(_access)的相关知识,请在本站进行查询。
本文标签: