I am newbie to servlets, I want to save an uploaded image to a dir in the web directory, here is my code:
String fileName = p.getSubmittedFileName();
InputStream fileContent = p.getInputStream();
File uploads = new File(this.getServletContext().getRealPath("/uploads"));
File file = File.createTempFile("somefilename-", ".ext", uploads);
try (InputStream input = p.getInputStream()) {
Files.copy(input, file.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
where p
is request.getPart()
.
The file is posted correctly, but I can't save it to the dir, why?