Когда я использую request.getParameter («txtPhoto»); он возвращает имя файла (image.jpg), но я хочу получить полное имя пути (C: \ Images \ image.jpg), потому что мне нужно, чтобы это полное место для сохранения файла в базе данных MySQL. Я надеюсь, что вы можете мне помочь.Java Servlet и JSP, Как получить полный путь от файла?
----------------------- страница 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>
-------------- --- Сервлет
@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());
}
}
вы должны сделать папку Изображения внутри вашего проекта directory.Then выбрать изображение оттуда так, то вы знаете, путь, т. е. ваш-projectpath/Images/myimage.jpg – silentprogrammer