gpt4 book ai didi

java - 如何从浏览器保存 .pdf?

转载 作者:行者123 更新时间:2023-12-02 18:09:00 25 4
gpt4 key购买 nike

我尝试使用我在 stackoverflow 上找到的不同方法(包括 FileUtils IO )来保存 .pdf 文件,但是,我总是会损坏它。当我使用记事本打开损坏的文件时,我得到以下内容:

<HEAD>

<TITLE>
09010b129fasdf558a-
</TITLE>

</HEAD>


<HTML>

<SCRIPT language="javascript" src="./js/windowClose.js"></SCRIPT>

<LINK href="./theme/default.css" rel="stylesheet" type="text/css">
<LINK href="./theme/additions.css" rel="stylesheet" type="text/css">

<BODY leftmargin="0" topmargin="0">

<TABLE cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD class="mainSectionHeader">
<A href="javascript:windowClose()" class="allLinks">
CLOSE
</A>
</TD>

</TR>

</TABLE>

<script language='javaScript'>
alert('Session timed out. Please login again.\n');
window.close();
</script>



</BODY>

</HTML>

后来,我尝试使用@BalusC提供的答案从浏览器保存.pdf文件。这个解决方案非常有帮助:我能够摆脱 session 问题。但是,它也会生成损坏的 .pdf。但当我用记事本打开它时,它完全不同。不过,不再有登录问题:

<HTML>

<HEAD>

<TITLE>
Evidence System
</TITLE>

</HEAD>

<LINK href="./theme/default.css" rel="stylesheet" type="text/css">

<TABLE cellpadding="0" cellspacing="0" class="tableWidth760" align="center">
<TR>
<TD class="headerTextCtr">
Evidence System
</TD>
</TR>
<TR>
<TD colspan="2">
<HR size="1" noshade>
</TD>
</TR>
<TR>
<TD colspan="2">



<HTML>
<HEAD>
<link href="./theme/default.css" rel="stylesheet" type="text/css">
<script language="JavaScript">

function trim(str)
{
var trmd_str

if(str != "")
{
trmd_str = str.replace(/\s*/, "")
if (trmd_str != ""){

trmd_str = trmd_str.replace(/\s*$/, "")
}

}else{
trmd_str = str
}
return trmd_str
}

function validate(frm){
//check for User name
var msg="";
if(trim(frm.userName.value)==""){
msg += "Please enter your user id.\n";
frm.userName.focus();
}

if(trim(frm.password.value)==""){
msg += "Please enter your password.\n";
frm.userName.focus();
}

if (trim(msg)==""){
frm.submit();
}else{
alert(msg);
}
}

function numCheck(event,frm){
if( event.keyCode == 13){
validate(frm);
}
}

</script>
</HEAD>

<BODY onLoad="document.frmLogin.userName.focus();">

<FORM name='frmLogin' method='post' action='./ServletVerify'>
<TABLE width="100%" cellspacing="20">
<tr>
<td class="mainTextRt">
Username
<input type="text" name="userName" maxlength="32" tabindex="1" value=""
onKeyPress="numCheck(event,this.form)" class="formTextField120">
</TD>
<td class="mainTextLt">
Password
<input type="password" name="password" maxlength="32" tabindex="2" value=""
onKeyPress="numCheck(event,this.form)" class="formTextField120">
</TD>
</TR>

<tr>
<td colspan="2" class="mainTextCtr" style="color:red">
Unknown Error
</td>
</tr>

<tr>
<td colspan="2" class="mainTextCtr">
<input type="button" tabindex="3" value="Submit" onclick="validate(this.form)" >
</TD>
</TR>
</TABLE>

<INPUT TYPE="hidden" NAME="actionFlag" VALUE="inbox">
</FORM>

</BODY>
</HTML>

</TD>
</TR>
<TR>
<TD height="2"></TD>
</TR>
<TR>
<TD colspan="2">
<HR size="1" noshade>
</TD>
</TR>
<TR>
<TD colspan="2">
<LINK href="./theme/default.css" rel="stylesheet" type="text/css">

<TABLE width="80%" align="center" cellspacing="0" cellpadding="0">
<TR>
<TD class="footerSubtext">
Evidence Management System
</TD>
</TR>

<!-- For development builds, change the date accordingly when sending EAR files out to Wal-Mart -->
<TR>
<TD class="footerSubtext">
Build:&nbsp;&nbsp;v3.1
</TD>
</TR>

</TABLE>
</TD>
</TR>
</TABLE>

</HTML>
<小时/>

我还有哪些其他选择?

PS:当我尝试使用 CTRL+Shift+S 手动保存文件时,文件保存正常。

最佳答案

来自看起来只是 HTML 错误页面的错误响应:

alert('Session timed out. Please login again.\n');

因此,下载 PDF 文件似乎需要在有效的 HTTP session 中进行。 HTTP session 由 cookie 支持。 HTTP session 通常在服务器端包含有关当前 Activity 和/或登录用户的信息。

Selenium Web 驱动程序完全透明地自行管理 cookie。您可以通过编程方式获取它们,如下所示:

Set<Cookie> cookies = driver.manage().getCookies();

当在 Selenium 控制之外手动修改 java.net.URL 时,您应该确保自己的 URL 连接使用相同的 cookie(从而也维护相同的 HTTP session )。您可以在 URL 连接上设置 cookie,如下所示:

URLConnection connection = new URL(driver.getCurrentUrl()).openConnection();

for (Cookie cookie : driver.manage().getCookies()) {
String cookieHeader = cookie.getName() + "=" + cookie.getValue();
connection.addRequestProperty("Cookie", cookieHeader);
}

InputStream input = connection.getInputStream(); // Write this to file.

关于java - 如何从浏览器保存 .pdf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19059769/

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