当我使用 request.getParameter("txtPhoto"); 时它返回文件名 (image.jpg),但我想获取完整路径名 (C:\Images\image.jpg),因为我需要该完整位置来将文件保存在 MySQL 数据库中。我希望你能帮助我。
------------------------JSP页面
<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());
}
}
最佳答案
您无法获取本地文件路径,因为它在服务器端没有用处。它只会对黑客有用。这就是浏览器不发送它的原因。
关于Java Servlet 和 JSP,如何从文件中获取完整路径名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28017956/