简体   繁体   English

将文件从JSP发送到Servlet

[英]Sending File from JSP to Servlet

Please have a look at he following code 请看下面的代码

JSP JSP

<%-- 
    Document   : index
    Created on : Nov 27, 2012, 1:11:48 PM
    Author     : Yohan
--%>

<%@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>
        <div>
  <div>Content for New Div Tag Goes Here</div>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
<p>&nbsp;</p>
  <div>
      <form method="post" action="FileSelector" enctype="multipart/form-data">
      Select File: <input type="file" name="location"/></div>
        <br>
        <input type="submit" value="Submit"/>
</form>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>
  <p>&nbsp;</p>


</div>
    </body>
</html>

Servlet Servlet的

package importWizard;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.*;
import javax.servlet.http.*;

public class FileSelector extends HttpServlet
{
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
    {
        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException
    {
        PrintWriter pw = response.getWriter();

        File location = (File)request.getParameter("location");


        pw.write(location);
    }
}

As you can see, I am unable to send the file from JSP to Servlet. 如您所见,我无法将文件从JSP发送到Servlet。 I don't need to send the file, but at least the complete location of the file (It is only sending the file name). 我不需要发送文件,但至少需要文件的完整位置(它只发送文件名)。 How can I send the file or the complete location of the file from JSP to servlet? 如何将文件或文件的完整位置从JSP发送到servlet?

I don't need to send the file, but at least the complete location of the file (It is only sending the file name). 我不需要发送文件,但至少需要文件的完整位置(它只发送文件名)。 How can I send the file or the complete location of the file from JSP to servlet? 如何将文件或文件的完整位置从JSP发送到servlet?

That's not possible using standard HTML <input type="file"> element. 使用标准HTML <input type="file">元素是不可能的。 It sends merely the entire file contents along with the filename as that's basically the only way for the server to get the file contents. 它只发送整个文件内容和文件名,因为这基本上是服务器获取文件内容的唯一方式。 The server has namely no direct access to the client's local disk file system, so the client's absolute disk file system path as sole information would have been useless. 服务器没有直接访问客户端的本地磁盘文件系统,因此客户端的绝对磁盘文件系统路径作为唯一信息将是无用的。 Note that the MSIE browser will due to a security bug send the full absolute client side disk file system path along instead of only the filename, but that's thus not how things are supposed to work. 请注意,MSIE浏览器将由于安全漏洞而发送完整的绝对客户端磁盘文件系统路径,而不仅仅是文件名,但这并不是事情应该如何工作。

If you really need to have only the client's absolute disk file system path, then your best bet is to create a (signed) applet or webstart application which obtains it by JFileChooser and finally embed it in the web page. 如果您确实只需要客户端的绝对磁盘文件系统路径,那么最好的办法是创建一个(签名的)applet或webstart应用程序,它由JFileChooser获取并最终嵌入到网页中。

See also: 也可以看看:

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

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