【问题标题】:The type Part is ambiguousPart 类型不明确
【发布时间】:2020-10-21 12:17:07
【问题描述】:

我正在做一个小项目,我正在尝试 dd 个人资料,以便用户可以上传他的个人资料图片,所以当我搜索参考资料时,我发现了一些 Youtube 视频,我开始编码,我想链接图片用户上传为他的个人资料图片。 那里我收到错误“类型部分不明确”(在 jsp 中)

<%@page 
 import="java.sql.*,java.text.*,javax.servlet.*,java.security.MessageDigest,
   java.security.NoSuchAlgorithmException 
      ,javax.mail.*,javax.mail.internet.*,
       java.io.File,java.io.PrintWriter,java.awt.List"%> 

     <%@ 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");

    Part part =request.getPart("profile_link");(here I'm getting the issue)
    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);
    
    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代码

<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>

很抱歉分享整个代码。 我尝试了多种方法,但没有一个对我有用 有人请帮帮我 谢谢。

【问题讨论】:

    标签: java html jsp web web-deployment


    【解决方案1】:

    该错误意味着编译器可以将类型 Part 解析为您正在导入的包中可用的多个类(或与您的一个自定义类发生名称冲突,该类也恰好命名为 Part )。

    在您的特定情况下,您要导入两个包,每个包都包含一个名为 Part 的类。

    javax.servlet.http.Part
    javax.mail.Part
    

    为避免此错误,您可以像这样为类使用完整的命名空间:

    javax.servlet.http.Part part =request.getPart("profile_link");
    

    不过,最佳做法是尽可能避免导入整个包,因为您只使用这些包中的特定类。

    不要:

    import java.servlet.*;
    

    做:

    import java.servlet.http.Part;
    

    【讨论】:

    • 嗨,先生,现在我在 String fileName=extractFileName(part) 行出现错误(“类型 __2F_Student_5F_Portal_20_2_2E_0_2F_WebContent_2F_add_5F_student_5F_details_2E_jsp 的方法 extractFileName(Part) 未定义”);我确实使用此代码解决了以前的问题 javax.servlet.http.Part part =request.getPart("profile_link");将这些添加到头文件中(java.nio.file.Path,java.nio.file.Paths,javax.servlet.http.Part)谢谢
    • 现在你又问了一个问题。 Stack Overflow 使用问答格式。你发布一个问题,你得到一个答案。如果答案解决了您的问题,您接受它并单独发布任何新问题。也不要编辑您的原始问题来提出新问题 - 这样做只会使搜索相同问题并发现答案与问题无关的人感到困惑,因为问题已被更改。
    • 只是为了让您开始,看起来您正试图在字符串文字上调用 extractFileName(),而不是在应该实现 extractFileName 方法的某个对象上调用。该方法可能来自您自己编写的类。
    • 是的,但是您在调用 extractFileName 时没有指定具有该方法的对象的实例。看起来您正在调用它,就好像 JSP 页面本身应该实现该方法一样——您确定这是对的吗?你真的应该发布第二个问题,因为我认为答案可能比你想象的更复杂,而且我已经有一段时间没有写过 JSP了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-04
    • 1970-01-01
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多