Issue
I'm having a problem with adding the image in jsp/Servlet. I get the following error:
C:\Users\palex\eclipse-workspace\AgendaJSP_Path\img (Access Denied)
I have windows 10; I did not understand if it is an operating system problem or other.
I put the structure of the project
and the code where I add an image:
package pack_person;
import java.io.File;
import java.io.PrintWriter;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class addpersonimage
*/
@WebServlet("/addpersonimage")
@MultipartConfig(fileSizeThreshold=1024*1024*2,
maxFileSize=1024*1024*10,
maxRequestSize=1024*1024*50)
public class addpersonimage extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public addpersonimage() {
super();
// TODO Auto-generated constructor stub
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("name");
Part part=request.getPart("file");
String namefile=extractFileName(part);
String path="C:\\Users\\palex\\eclipse-workspace\\AgendaJSP_Path\\img" + File.separator + namefile;
File fileSaveDir= new File(path);
part.write(path + File.separator);
String message=null;
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/persons","root","pssw");
PreparedStatement pst = con.prepareStatement("insert into persons values (?,?,?)");
pst.setString(1, name);
pst.setString(2, namefile);
pst.setString(3, path);
int row = pst.executeUpdate();
if(row>0) {
message= "Successfully";
}
} catch (Exception e) {
out.println(e);
}
request.setAttribute("Message", message);
getServletContext().getRequestDispatcher("/Message.jsp").forward(request, response);
}
private String extractFileName(Part part) {
String contentDisp = part.getHeader("content-disposition");
String[] items = contentDisp.split(";");
for (String s : items) {
if (s.trim().startsWith("namefile")) {
return s.substring(s.indexOf("=") + 2, s.length()-1);
}
}
return "";
}
}
Solution
So you're trying to write file to the path C:\Users\palex\eclipse-workspace\AgendaJSP_Path\img and you get "access denied"
- What are file access rights assigned to this path? Does the user account the server is running as have write privilege to this path?
- What server are you running the servlet in? Some servers, example Tomcat Apache, are sandboxed and can only write to paths allowed by the sandbox. Again for Tomcat you need to write your file to one of these paths or add your path to tomcat9.service which on Ubuntu is in /usr/lib/systemd/system (where 9 is the current version number of tomcat) So check path permissions and check server sandbox.
Answered By - ccubed
Answer Checked By - Willingham (JavaFixing Volunteer)