当我使用request.getParameter(" txtPhoto");它返回文件的名称(image.jpg),但我想获得fullpathname(C:\ Images \ image.jpg),因为我需要完整的位置来将文件保存在MySQL数据库中。我希望你能帮忙。
----------------------- JSP Page
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Upload</title>
</head>
<body>
<form action="ImageController" method="post">
<h1>File</h1>
<input type="file" name="txtPhoto" placeholder="Upload Your Image" accept="image/gif, image/jpeg, image/png" /><br /><br />
<input type="submit" value="Save">
</form>
</body>
</html>
-----------------的Servlet
@WebServlet(name = "ImageController", urlPatterns = {"/ImageController"})
public class imagenservlet extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
if(request.getParameter("txtPhoto") == null)
{
response.sendRedirect("upload.jsp");
return;
}
PrintWriter out = response.getWriter();
FileInputStream input = null;
File theFile = new File(request.getParameter("txtPhoto"));
input = new FileInputStream(theFile);
out.println("FullPathname" + " " + theFile.getAbsolutePath());
}
}
答案 0 :(得分:3)
您无法获取本地文件路径,因为它在服务器端无用。它只会对黑客有用。这就是为什么浏览器不发送它。
答案 1 :(得分:0)
在JSP端声明隐藏的filePath
变量,为隐藏变量分配所需的路径并在servlet中获取该变量,它只是解决了。