简体   繁体   English

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

[英]Error when I read file on JSP page using PC remote

I have a question: 我有个问题:

I have this jsp client that requires the entry of a string that indicates the path of a file: 我有这个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>

Now, When I click on "Submit", I have this page JSP: 现在,当我单击“提交”时,我将看到此页面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>

Now, if I insert 现在,如果我插入

C:\\prova.xls C:\\ prova.xls

in a PC remote, I get this error: 在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)

Why I get this error? 为什么会出现此错误? What am I wrong? 我怎么了 I tried to write this path with "//", "\\", "\\" and "/" but I get always the same error. 我试图用“ //”,“ \\”,“ \\”和“ /”来编写此路径,但总是出现相同的错误。

JSP is a server side process. JSP是服务器端进程。 The file name and path that you enter in HTML / Javascript may not exist on the server. 您在HTML / Javascript中输入的文件名和路径可能在服务器上不存在。

Consider using a file upload to upload the file to the server where the JSP can action it. 考虑使用文件上传将文件上传到服务器,JSP可以在该服务器上对其进行操作。

Also consider using servlet technology and using code in JSP is frowned upon. 还考虑使用servlet技术,并且在JSP中使用代码不被接受。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM