我想将来将图像上载到磁盘驱动器位置,它可能是服务器驱动器位置,并且它将使用应从数据库中获取的文件路径位置在网页上显示相同的图像。
我试图将图像上传到项目目录,但是图像文件没有被上传。但是,当我刷新项目目录时,它将出现在其中。
我提供的路径是项目位置目录。
@MultipartConfig
//@WebServlet("/NewAddProject")
public class NewAddProject extends HttpServlet {
private File file;
boolean flag1 = false, flag2 = false, flag3 = false;
String p_id;
String img_sr_no;
String project_name = null;
String project_type = null;
String project_location = null;
String project_photo = null;
String project_ststus = null;
javax.servlet.http.Part img1part;
Part imgpart;
String imgName = null;
// String pdfName = null;
String content_type1;
String store_id;
String created_date;
PreparedStatement pst;
int i;
/**
* @see HttpServlet#HttpServlet()
*/
public NewAddProject() {
super();
// TODO Auto-generated constructor stub
}
public void init() {
myOldFilePath = getServletContext().getInitParameter("file-upload");
if (!new File(myOldFilePath).exists()) {
(new File(myOldFilePath)).mkdir();
}
filePath = myOldFilePath + "\\" + "NewProjectDetails" + "\\";
File directory1 = new File(filePath);
if (directory1.exists()) {} else {
directory1.mkdir();
}
System.out.println(filePath);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
java.io.PrintWriter out = response.getWriter();
String imagePath = "D:\\JAVA PROJECTS 2019\\RealValueProperties\\RealValueProperties_04_03_2019\\RealValueProperties\\WebContent\\projectimages\\images\\img";
String absolutePathToImages = request.getSession().getServletContext().getRealPath("/projectimages/");
final String DIRECTORY_PATH = absolutePathToImages + "images" + File.separator + "";
project_name = request.getParameter("project_name");
System.out.println(project_name);
project_type = request.getParameter("property_type");
System.out.println(project_type);
project_location = request.getParameter("project_location");
System.out.println(project_location);
project_ststus = request.getParameter("project_ststus");
System.out.println(project_ststus);
imgpart = request.getPart("project_image"); // for image
if (imgpart != null) {
try {
imgName = imgpart.getSubmittedFileName();
imgName = GetImageID.getImageID() + imgName;
System.out.println("image name" + imgName);
flag1 = true;
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println("path: " + filePath);
imgpart.write(imagePath + imgName);
// imgpart.write(new File(DIRECTORY_PATH) + File.separator + "img" + File.separator + imgName);
System.out.println("uploaded : " + DIRECTORY_PATH);
try {
Class.forName("com.mysql.jdbc.Driver");
System.out.println("Driver loaded...");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/realvalueproperties", "root", "");
System.out.println("Connected...");
String query2 = "INSERT into addnewproject ( p_id, project_name, project_type, " +
"project_location, project_image,project_ststus)" + "values(?,?,?,?,?,?)";
PreparedStatement pst2 = conn.prepareStatement(query2);
pst2.setString(1, null);
pst2.setString(2, project_name);
pst2.setString(3, project_type);
pst2.setString(4, project_location);
// pst2.setString(5, filePath + imgName);
pst2.setString(5, "projectimages/images/img" + imgName);
pst2.setString(6, project_ststus);
int i = pst2.executeUpdate();
if (i > 0) {
System.out.println("Insered to dbt ");
response.sendRedirect("add_new_projects.jsp");
}
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
conn.close();
// response.sendRedirect("student_home.jsp");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
我希望图像文件应该上传到特定的驱动器位置。它可能是本地或服务器驱动器位置。该路径应存储在数据库中,并在网页上显示。