在我的任务中,我必须下载一个文件,它将在我的项目文件夹中,它正在工作而不使用servlet我的意思是当我使用普通的java类时它的工作但当我正在使它servlet它不工作时请任何人帮助我在这里..这是我的代码和SalarySlip是我的项目中的一个文件夹,我手动创建..
我更新了错误消息
public class main extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, java.io.IOException
{
try
{
Document document = new Document();
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream("D:/test/ReadPdf.pdf"));
document.open();
PdfReader reader = new PdfReader("SalarySlip/pivot.pdf");
int n = reader.getNumberOfPages();
PdfImportedPage page;
// Go through all pages
for (int i = 1; i <= n; i++) {
// only page number 2 will be included
page = writer.getImportedPage(reader, i);
Image instance = Image.getInstance(page);
document.add(instance);
}
document.close();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
<body>
<form name = "form" method="get" action="main">
<input type="submit" value="login" >
</body>
显示错误消息:
New/pivot.pdf not found as file or resource.
at com.itextpdf.text.io.RandomAccessSourceFactory.
createByReadingToMemory(RandomAccessSourceFactory.java:248)
at com.itextpdf.text.io.RandomAccessSourceFactory.
createBestSource(RandomAccessSourceFactory.java:173)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:219)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:207)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:197)
at com.journaldev.servlet.main.doGet(main.java:63)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain. internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain
.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.
invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.
invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.
AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor
.process(AbstractHttp11Processor.java:1023)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.
process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
答案 0 :(得分:1)
@BeforeTest public void StartBrowser(){ //在构建类中创建FirefoxProfile对象以访问其属性。 FirefoxProfile fprofile = new FirefoxProfile();
//Set Location to store files after downloading.
// this one is working
fprofile.setPreference("browser.download.dir","C:\\Users\\malik\\Desktop\\workspace\\payment\\CSVDownload");
但是需要像这样下载项目文件夹
fprofile.setPreference( “browser.download.dir”, “.. \ CSVDownload \”);
像这样我不知道我做了什么
fprofile.setPreference("browser.download.folderList", 2);
//Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"//MIME types Of MS Excel File.
+ "application/pdf;" //MIME types Of PDF File.
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" //MIME types Of MS doc File.
+ "text/plain;" //MIME types Of text File.
+ "text/csv"); //MIME types Of CSV File.
fprofile.setPreference( "browser.download.manager.showWhenStarting", false );
fprofile.setPreference( "pdfjs.disabled", true );
//Pass fprofile parameter In webdriver to use preferences to download file.
driver = new FirefoxDriver(fprofile);
}
@Test
public void OpenURL() throws InterruptedException{
driver.get("http://only-testing-blog.blogspot.in/2014/05/login.html");
//Download CSV File
driver.findElement(By.xpath("//a[contains(.,'Download CSV File')]")).click();
Thread.sleep(1000);
}
@AfterTest
public void CloseBrowser() {
//driver.quit();
}
}
答案 1 :(得分:0)
你的道路似乎错了
PdfReader reader = new PdfReader("SalarySlip/pivot.pdf");
将其更改为
PdfReader reader = new PdfReader("../SalarySlip/pivot.pdf");
如果它在WEB-CONTENT文件夹中
答案 2 :(得分:0)
String INPUTFILE = "/Salary/pivot.pdf";
PdfReader reader = new PdfReader(getServletContext().getRealPath(INPUTFILE));
它正在运作