mercredi 1 juillet 2020

method extractFileName(Part) is undefined for the type JSP (Full code)

I'm working on a small project where I'm trying to dd profile so the user can upload his profile pic so while I was searching for references I found some Youtube videos and I started coding and I want to link the image which user uploaded as his profile image. There I'm getting the error "The method extractFileName(Part) is undefined for the type"(in jsp)

  <%@page 
       import="java.sql.*,java.text.*,javax.servlet.*, 
       java.security.MessageDigest,java.security.NoSuchAlgorithmException 
       ,java.io.File,java.io.PrintWriter,java.awt.List 
     ,java.nio.file.Path,java.nio.file.Paths,javax.servlet.http.Part"%> 

     <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%>
<%!
%>
<%
String username=(String) session.getAttribute("nkey");
String f_name =request.getParameter("f_name");
String l_name =request.getParameter("l_name");

javax.servlet.http.Part part =request.getPart("profile_link");
String id_link =request.getParameter("id_link");
String age =request.getParameter("age");
String gender =request.getParameter("gender");
String address =request.getParameter("address");
String pincode =request.getParameter("pincode");
String qualification =request.getParameter("qualification");
String year =request.getParameter("year");
String college_name =request.getParameter("college_name");
String state =request.getParameter("state");
String country =request.getParameter("country");
%>
<%!
private static final String SAVE_DIR="Pictures";
%>
<%response.setContentType("text/html;charset=UTF-8");%>
<% 
PrintWriter outt=response.getWriter();
String savePath="C:/Users/thots/Desktop/My World/internsip/Student_Portal 2.0/WebContent"+File.separator+SAVE_DIR;
File fileSaveDir=new File(savePath);

String fileName=extractFileName(part);(error here)

part.write(savePath + File.separator + fileName);

String filePath=savePath+File.separator + fileName;     
%>
<%
try 
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/Student_Portal 2.0","root","");
    Statement st=con.createStatement();
    int i=st.executeUpdate("insert into student_details values('"+username+"','"+f_name+"','"+l_name+"','"+filePath+"','"+id_link+"','"+age+"','"+gender+"','"+address+"','"+pincode+"','"+qualification+"','"+year+"','"+college_name+"','"+state+"','"+country+"')");
    if(i>0)
    {
        response.sendRedirect("index.jsp");
        Statement stt=con.createStatement();
        String sql="update user_login set P_Status='1' where username='"+username+"'";
        stt.executeUpdate(sql);
    }
    else
    {
        System.out.println("Failed");
    }

} 
catch (Exception e) 
{
    e.printStackTrace();
}
%>

Html code

<form action="add_student_details.jsp" enctype="multipart/form-data" method="post">
                        <div class="container-fluid">
                            <div>
                                    <label for="img">Upload Profile Picture</label>
                                     <input name="profile_link" type="file" class="form-control" required>
                            </div>
                            <br>
                            <div>
                                    <label for="img">Upload Your ID card</label>
                                    <input name="id_link" type="file" class="form-control" required>
                            </div>
                            <br>
                            <div class="row">
                                <div class="col-6">
                                     <input name="f_name" type="text" class="form-control" placeholder="First name" required>
                                </div>
                                <div class="col-6">
                                    <input name="l_name" type="text" class="form-control" placeholder="Last name" required>
                                </div>
                            </div>
                            <br>
                            <div class="row">
                                <div class="col-6">
                                     <input name="age" type="number" class="form-control" placeholder="Age" required>
                                </div>
                                <div class="col-6">
                                      <select name="gender" class="form-control" required>
                                        <option>Male</option>
                                        <option >Female</option>
                                      </select>
                                </div>
                            </div>
                            <br>
                            <div>
                                <textarea name="address" rows="4" cols="55" placeholder="Your Address" id="w3review" required></textarea>
                            </div>
                            <br>
                            <div>
                                <input name="pincode" type="number" class="form-control" placeholder="Pincode" required>
                            </div>
                        </div>  
                        <br>
                        <hr>
                        <div class="modal-body">
                         <h4>Educational Info</h4>
                        </div>
                        <div class="container-fluid">
                            <div class="row">
                                    <div class="col-6">
                                          <select name="qualification" class="form-control" required>
                                          <option selected disabled>Select Your Qualification</option>
                                            <option>B.Tech</option>
                                            <option >B.E</option>
                                          </select>
                                    </div>
                                    <div class="col-6">
                                          <select name="year" class="form-control" required>
                                          <option selected disabled>Select Your Year</option>
                                            <option>I</option>
                                            <option >II</option>
                                            <option>III</option>
                                            <option >IV</option>
                                          </select>
                                    </div>
                                </div>
                                <br>
                                <div >
                                          <select name="college_name" class="form-control" required>
                                          <option selected disabled>Select Your College</option>
                                            <option>ABCD College Of Engineering And Technology</option>
                                            <option >XYZ College Of Engineering And Technology</option>
                                          </select>
                                </div>
                                <br>
                                <div class="row">
                                    <div class="col-6">
                                          <select name="state" class="form-control" required>
                                          <option selected disabled>Select Your State</option>
                                            <option>Telangana</option>
                                            <option >Kerala</option>
                                          </select>
                                    </div>
                                    <div class="col-6">
                                          <select name="country" class="form-control" required>
                                          <option selected disabled>Select Your Country</option>
                                            <option>India</option>
                                            <option>United States</option>
                                          </select>
                                    </div>
                                </div>
                                <br>
                                <hr>
                                <center>
                                <button type="submit" class="submit-btn">Submit</button>
                                </center>
                            </div>
                            <br>
                       </form>

          

Sorry for sharing the whole code. i tried multiple ways but none worked for me someone please help me Thank you.




Aucun commentaire:

Enregistrer un commentaire