Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 下载HTML而不是文件_Java_Html_File_Url_Download - Fatal编程技术网

Java 下载HTML而不是文件

Java 下载HTML而不是文件,java,html,file,url,download,Java,Html,File,Url,Download,我正在使用Java代码从Internet下载一个文件并将其保存到某个目录 但是,代码会下载页面的HTML源代码,而不是文件内容 下面的代码说明了该问题: import java.awt.*; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.URL; import java.nio.channels.Channels; import java.nio.

我正在使用Java代码从Internet下载一个文件并将其保存到某个目录

但是,代码会下载页面的
HTML
源代码,而不是文件内容

下面的代码说明了该问题:

import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.channels.Channels;
import java.nio.channels.ReadableByteChannel;

public class JavaFileDownloadTest
{
    public static void download(String remoteURL, String targetFilePath)
            throws IOException
    {
        URL downloadableFile = new URL(remoteURL);
        ReadableByteChannel readableByteChannel = Channels.newChannel(downloadableFile.openStream());
        FileOutputStream fileOutputStream = new FileOutputStream(targetFilePath);
        fileOutputStream.getChannel().transferFrom(readableByteChannel, 0, Long.MAX_VALUE);
    }

    public static void main(String[] arguments) throws IOException
    {
        String userHome = System.getProperty("user.home");
        String fileName = "Test.txt";
        String targetFilePath = userHome + File.separator + "Downloads" + File.separator + fileName;
        download("http://bullywiiplaza.cuccfree.com/" + fileName, targetFilePath);
        Desktop.getDesktop().open(new File(targetFilePath));
    }
}
找到的文件包含文本

Hello StackOverflow!
但是,当使用上述代码下载时,我得到的是
HTML
源代码作为文件内容:

<html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("ae71113e4baf38cee1c1aacf0ae66c00");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; document.cookie="referrer="+escape(document.referrer); location.href="http://bullywiiplaza.cuccfree.com/Test.txt?ckattempt=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
函数toNumbers(d){var e=[];d.replace(/(..)/g,函数(d){e.push(parseInt(d,16));将e}函数返回hex(){for(var d=[],d=1==参数。长度和参数[0]。构造函数==数组?参数[0]:参数,e=“”,f=0;fd[f]?“0:”)+d[f]。toString(16);返回e.toLowerCase()}var a=toNumbers(“F655BA9D0968C635B4B=Tonb4b”)(“98344C2EE86C3994890592585B49F80”),c=t数字(“ae71113e4baf38cee1c1aacf0ae66c00”);document.cookie=“u test=“+toHex(slowAES.decrypt(c,2,a,b))+”;expires=Thu,37年12月31日23:55:55 GMT;path=/“document.cookie=”referer=“+escape(document.referer);location.href=”http://bullywiiplaza.cuccfree.com/Test.txt?ckattempt=1“本网站需要Javascript才能工作,请在浏览器中启用Javascript或使用支持Javascript的浏览器

这是为什么?我如何修复它?我已经尝试了各种库和方法来下载文件,但它们都产生了相同的“错误”结果。

我认为目标url执行一些javascript来提供文件。该脚本必须由某个javascript引擎解释(和执行)

因此,您需要一些解析来获得真实的文件url(而不仅仅是javascript),或者集成一些javascript引擎来执行脚本代码并获得结果

我认为这可以帮助你:

或者更好:


不存在。我认为url应该是存在的。

我将网站主机切换到,现在上面的代码按预期工作。

选择一个更好的文件主机。在设置一些cookie(可能用于分析)之前,您正在使用的主机拒绝提供文件。您所看到的是他们交付给还没有cookie的浏览器的代码。请注意,如果您在私人浏览器中转到该链接,它将重定向(使用您看到的代码)并在末尾添加
?cktrunt=1
。@马特:我注意到浏览器已重定向,但我不知道如何使用它以编程方式下载文件解决方案是:选择一个更好的文件主机。您选择的主机需要分析跟踪才能交付文件。以编程方式下载文件意味着Java是您的“浏览器”,它A)没有您的浏览器拥有的cookie,B)无法执行JavaScript来创建该cookie。只有浏览器才能满足该文件主机的要求。他们正在积极阻止cURL(通过
403
ing它),您必须下载依赖项(
aes.js
)在您执行他们的分析脚本之前。请尝试更好的主机,如GitHub Pages或其他东西抱歉,两者都存在,但使用
https
会引发
SSLHandshakeException
是的,对不起,您是对的。您有aes.js,它会截取请求并放置URL参数并获取页面。这不是一个简单的下载。您会怎么做你用这个来解决下载正确文件的问题吗?也许我的链接被破坏了,对不起。我原以为脚本enigne可以工作,但这只是为了执行你提供的脚本。在这种情况下,我认为很难实现你想要的,因为你的提供商阻止了访问。请参阅@Matt对你问题的评论。