请看他下面的代码
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> </p>
<p> </p>
<p> </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> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
<p> </p>
</div>
</body>
</html>
小服务程序
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);
}
}
如您所见,我无法将文件从 JSP 发送到 Servlet。我不需要发送文件,但至少需要文件的完整位置(它只是发送文件名)。如何将文件或文件的完整位置从 JSP 发送到 servlet?