我正在研究这个问题。把这个问题放在这里希望看到一些很棒的见解。这是我的问题:
我正在开发一个Android应用程序,它从我的Web服务器下载文件以显示在应用程序中,并可供离线使用。
如果我的应用程序转到https://www.mydomain.com/abc/xyz.png下载文件,我怎样才能将相同的网址公开?即如果您在浏览器地址栏中输入https://www.mydomain.com/abc/xyz.png,您将被带到png文件以外的某个地方。
我想我在这里需要某种包装,但我是所有这一切的新手,并试图快速学习。
感谢任何帮助。
对于它的价值,我将使用mySql数据库与workbench 6.0或phpMyAdmin。
修改 的
回应前两个回复:
所以我的代码在我的Android应用程序中,我下载文件并将其保存到内部存储,看起来像这样(其中基本路径将我带到文件的web目录):
url = new URL(basePath + fileName);
URLConnection connection = url.openConnection();
int lenghtOfFile = connection.getContentLength();
inputStream = new BufferedInputStream(url.openStream());
outFile = new File(context.getFilesDir() + "/" + fileName);
fileStream = new FileOutputStream(outFile);
outputStream = new BufferedOutputStream(fileStream, DOWNLOAD_BUFFER_SIZE);
会改变说:
url = new URL(basePath + "download_file.php" + "?key=hardcodedkey&file=" + fileName);
URLConnection connection = url.openConnection();
int lenghtOfFile = connection.getContentLength();
inputStream = new BufferedInputStream(url.openStream());
outFile = new File(context.getFilesDir() + "/" + fileName);
fileStream = new FileOutputStream(outFile);
outputStream = new BufferedOutputStream(fileStream, DOWNLOAD_BUFFER_SIZE);
答案 0 :(得分:0)
一种简单的方法可能是使用某个密钥作为密钥参数和要下载的图像或文件来调用php脚本。
www.myweb.com/myfunction.php?pass=secretPassword&file=image.png
此密钥将在您的应用程序的调用中进行编码。
然后您可以检查密码是否是您期望的密码:
//hardcoded hashed pass with sha1, for example.
$myHashedPass = '40bd001563085fc35165329ea1ff5c5ecbdbbeef';
if(sha1($_GET['pass']) != $myHashedPass){
die();
}
//download of file...
在PHP应用程序内部,您可以根据密码开始下载。
答案 1 :(得分:0)
你有几个选择。
1。)不太好 - > .htaccess限制某些引用,除了Android之外。
2。)更大 - >如果GET var与pass相同,则使用PHP文件输出图像。 e.g。
if (isset($_GET["pass"]) && $_GET["pass"]=="8h932mgks984jsolm9sjt4ms") {
header('Content-type:image/png');
include('/path/to/thePngFile.png');
} else die("nothing to see here..");