使用PC远程读取JSP页面上的文件时出错

时间:2014-10-07 07:10:34

标签: java string web-services jsp fileinputstream

我有一个问题:

我有这个jsp客户端需要输入一个指示文件路径的字符串:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="action.jsp" method="post">
            Enter NameFile (Excel):<input type="text" name="filename"/><br/>
            <input type="submit" value="Submit"/>
            <input type="reset" value="Reset" />
        </form> 
    </body>
</html>

现在,当我点击“提交”时,我有这个页面JSP:

<%@page import="java.io.ByteArrayOutputStream"%>
<%@page import="java.io.FileInputStream"%>
<%@page import="java.io.File"%>
<%@page contentType="text/plain" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/plain; charset=UTF-8">
        <title>JSP Page Action</title>
    </head>
    <body>

    <%-- start web service invocation --%><hr/>
    <%
    String filename = request.getParameter("filename");

    File file = new File (filename);
    FileInputStream fis = new FileInputStream(file);
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    for (int readNum; (readNum = fis.read(buf)) != -1;) {
        bos.write(buf, 0, readNum); //no doubt here is 0
    }

    byte[] bi = bos.toByteArray();
    try {
    ws.MyWS_Service service = new ws.MyWS_Service();
    ws.MyWS port = service.getMyWSPort();
     // TODO initialize WS operation arguments here
    byte[] inputFile = bi;
    // TODO process result here
    java.lang.String result = port.mywsmethod(inputFile);
    out.println(result);
    } catch (Exception ex) {
    // TODO handle custom exceptions here
    }
    %>
    <%-- end web service invocation --%><hr/>
    </body>
</html>

现在,如果我插入

  

C:\ prova.xls

在PC遥控器中,我收到此错误:

07-Oct-2014 08:55:10.194 SEVERE [http-apr-8080-exec-41] org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [jsp] in context with path [/myWS_Client] threw exception [An exception occurred processing JSP page /action1.jsp at line 27

25: 
26:     File file = new File (filename);
27:     FileInputStream fis = new FileInputStream(filename);
28:     ByteArrayOutputStream bos = new ByteArrayOutputStream();


Stacktrace:] with root cause
 java.io.FileNotFoundException: C:\prova.xls (Impossibile trovare il file specificato)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at org.apache.jsp.action1_005f1_jsp._jspService(action1_005f1_jsp.java:96)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:335)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:505)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:277)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2381)
    at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2370)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:744)

为什么我收到此错误?我错了什么?我试着用“//”,“\”,“\”和“/”写这个路径,但我总是得到同样的错误。

1 个答案:

答案 0 :(得分:1)

JSP是服务器端进程。您在HTML / Javascript中输入的文件名和路径可能不存在于服务器上。

考虑使用文件上传将文件上传到JSP可以对其执行操作的服务器。

另外考虑使用servlet技术并且在JSP中使用代码是不受欢迎的。