使用 HtmlUnit 上传文件时收到错误
Receiving error when Uploading File using HtmlUnit
我目前正在尝试将文件自动上传到特定站点。我能够成功登录网站并导航到导入页面;但是,当我尝试导入文件时,收到错误消息。我的猜测是,这是因为我只是一个用户,没有被授予写入服务器的权限。但是,如果我手动执行导入,那么它是成功的。导入通常有四个阶段。但是,在第一步之后您可以看到发生了错误。下面列出了我的代码以及我收到的错误:
public static void main(String args[]) throws Exception {
login();
}
@SuppressWarnings("resource")
public static void login() throws Exception {
// Open the webclient using Internet Explorer (Chrome does not work).
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER);
// Get the first page
final HtmlPage page = webClient.getPage("http://telephone.qqest.com/phone/Login/Login.asp");
// Get the form that we are dealing with.
final HtmlForm loginForm = page.getFormByName("frmLogin");
final HtmlTextInput userName = loginForm.getInputByName("Login");
final com.gargoylesoftware.htmlunit.html.HtmlPasswordInput passWord = (com.gargoylesoftware.htmlunit.html.HtmlPasswordInput)
loginForm.getInputByName("Password");
final HtmlTextInput companyID = loginForm.getInputByName("Ident");
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getCookieManager().setCookiesEnabled(true);
//final HtmlInput login = loginForm.getInputByName("Login");
// Change the values of the text fields.
userName.setValueAttribute("xxxx");
passWord.setValueAttribute("xxxxx");
companyID.setValueAttribute("xxxxx");
//create a submit button - it doesn't work with 'input'
DomElement loginBtn = page.createElement("button");
loginBtn.setAttribute("type", "submit");
// append the button to the form
loginForm.appendChild(loginBtn);
// submit the form
loginBtn.click();
//navigate to page for import.
HtmlPage page3 = webClient.getPage("http://telephone.qqest.com/phone/Imports/Employee/Module.asp");
//populate the textfield with the specified file.
final HtmlForm importForm = page3.getFormByName("ImportForm");
final HtmlFileInput inputFile = importForm.getInputByName("UploadFile");
inputFile.setValueAttribute("C:\Users\thisFile.xls");
inputFile.click();
final HtmlSubmitInput importBtn = (HtmlSubmitInput)importForm.getInputByValue("Import");
try {
importBtn.fireEvent(Event.TYPE_INPUT);
}
catch (NullPointerException ex) {
System.err.println(ex);
}
HtmlPage page4 = webClient.getPage("http://telephone.qqest.com/phone/Imports/Employee/ImportFile.asp?FileType=application/vnd.ms-excel&FilePath=c%3A%5Cinetpub%5Cwwwroot%5Cphone%5CDownloads%5CImports%5C&FileName=thisFile.xls.asp");
System.out.println("Import Page: " + page4.asText());
}
我收到的错误:
第 1 阶段,共 4 阶段:上传文件 - 已完成
Microsoft JET 数据库引擎错误“80004005”
无法更新。数据库或对象是只读的。
/phone/Imports/Employee/ImportFile.asp,第 155 行
我找到了一个解决方案,并希望 post 将它提供给那些将来可能会觉得它有用的人。问题是我只是试图通过使用 URL 重定向到导入页面。之前我曾尝试提交带有 importBtn.click()
声明的表格;但是,我需要将此语句声明为它自己的 HtmlPage 变量。
final HtmlSubmitInput importBtn = (HtmlSubmitInput)importForm.getInputByValue("Import");
HtmlPage page4 = importBtn.click();
// loops until the page changes.
while(page4 == page3) {
// The page hasn't changed.
Thread.sleep(500);
}
System.out.println("Import Page: " + page4.asText());
我目前正在尝试将文件自动上传到特定站点。我能够成功登录网站并导航到导入页面;但是,当我尝试导入文件时,收到错误消息。我的猜测是,这是因为我只是一个用户,没有被授予写入服务器的权限。但是,如果我手动执行导入,那么它是成功的。导入通常有四个阶段。但是,在第一步之后您可以看到发生了错误。下面列出了我的代码以及我收到的错误:
public static void main(String args[]) throws Exception {
login();
}
@SuppressWarnings("resource")
public static void login() throws Exception {
// Open the webclient using Internet Explorer (Chrome does not work).
final WebClient webClient = new WebClient(BrowserVersion.INTERNET_EXPLORER);
// Get the first page
final HtmlPage page = webClient.getPage("http://telephone.qqest.com/phone/Login/Login.asp");
// Get the form that we are dealing with.
final HtmlForm loginForm = page.getFormByName("frmLogin");
final HtmlTextInput userName = loginForm.getInputByName("Login");
final com.gargoylesoftware.htmlunit.html.HtmlPasswordInput passWord = (com.gargoylesoftware.htmlunit.html.HtmlPasswordInput)
loginForm.getInputByName("Password");
final HtmlTextInput companyID = loginForm.getInputByName("Ident");
webClient.getOptions().setCssEnabled(false);
webClient.getOptions().setRedirectEnabled(true);
webClient.setAjaxController(new NicelyResynchronizingAjaxController());
webClient.getCookieManager().setCookiesEnabled(true);
//final HtmlInput login = loginForm.getInputByName("Login");
// Change the values of the text fields.
userName.setValueAttribute("xxxx");
passWord.setValueAttribute("xxxxx");
companyID.setValueAttribute("xxxxx");
//create a submit button - it doesn't work with 'input'
DomElement loginBtn = page.createElement("button");
loginBtn.setAttribute("type", "submit");
// append the button to the form
loginForm.appendChild(loginBtn);
// submit the form
loginBtn.click();
//navigate to page for import.
HtmlPage page3 = webClient.getPage("http://telephone.qqest.com/phone/Imports/Employee/Module.asp");
//populate the textfield with the specified file.
final HtmlForm importForm = page3.getFormByName("ImportForm");
final HtmlFileInput inputFile = importForm.getInputByName("UploadFile");
inputFile.setValueAttribute("C:\Users\thisFile.xls");
inputFile.click();
final HtmlSubmitInput importBtn = (HtmlSubmitInput)importForm.getInputByValue("Import");
try {
importBtn.fireEvent(Event.TYPE_INPUT);
}
catch (NullPointerException ex) {
System.err.println(ex);
}
HtmlPage page4 = webClient.getPage("http://telephone.qqest.com/phone/Imports/Employee/ImportFile.asp?FileType=application/vnd.ms-excel&FilePath=c%3A%5Cinetpub%5Cwwwroot%5Cphone%5CDownloads%5CImports%5C&FileName=thisFile.xls.asp");
System.out.println("Import Page: " + page4.asText());
}
我收到的错误:
第 1 阶段,共 4 阶段:上传文件 - 已完成 Microsoft JET 数据库引擎错误“80004005” 无法更新。数据库或对象是只读的。 /phone/Imports/Employee/ImportFile.asp,第 155 行
我找到了一个解决方案,并希望 post 将它提供给那些将来可能会觉得它有用的人。问题是我只是试图通过使用 URL 重定向到导入页面。之前我曾尝试提交带有 importBtn.click()
声明的表格;但是,我需要将此语句声明为它自己的 HtmlPage 变量。
final HtmlSubmitInput importBtn = (HtmlSubmitInput)importForm.getInputByValue("Import");
HtmlPage page4 = importBtn.click();
// loops until the page changes.
while(page4 == page3) {
// The page hasn't changed.
Thread.sleep(500);
}
System.out.println("Import Page: " + page4.asText());