将输入类型文件插入mySQL表

时间:2017-07-11 15:52:28

标签: java mysql html5 servlets

我想将servlet从表单中获取的photoPath URL插入到我的表中,但是它无效。

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    String name = request.getParameter("name");
    String number = request.getParameter("contactNumber");
    String position = request.getParameter("position");
    String email = request.getParameter("email");
    String photoPath = request.getParameter("myImage");

    MySqlHandler.registerEmployee(name, number, position, email, photoPath);

}

如果我这样做

    try (PrintWriter out = response.getWriter()) {
        out.println(photoPath);
    }

url显示为150x150.png(这是我的img的名称)所以我知道字符串存储在那里。但是当我将它传递给我的registerEmployee函数时,它会阻止所有数据插入到我的表中。如果我将photoPath设置为某​​个静态字符串,则所有内容都会正确插入。可能有什么不对?我想也许是我从

获得的字符串
request.getParameter("myImage");

包含一些无法插入mysql表的奇怪字符?

  public static void registerEmployee(String name, String number, String position, String email, String photoPath) {
  initConnection();

  try {
      String query = " insert into employee_registry (name, number, position, email, photo_path)"
              + " values (?, ?, ?, ?, ?)";
      PreparedStatement preparedStatement = connection.prepareStatement(query);
      preparedStatement.setString(1, name);
      preparedStatement.setString(2, number);
      preparedStatement.setString(3, position);
      preparedStatement.setString(4, email);
      preparedStatement.setString(5, photoPath);
      preparedStatement.execute();
  } catch (Exception e) {
      e.printStackTrace();
      System.err.println("Exception" + e.getMessage());
  }

  closeConnection();
}

表格

<form action="" id="registerForm" method="post">
            <div class="col-md-12">
                <h3> Register Now</h3>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <label>Full Name: </label>
                </div>
                <div class="col-md-4">
                    <input name="name" type="text"/>
                </div>
                <div class="col-md-4">
                    <div class="error-wrapper">
                        <div class="hideError"> Field is blank </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <label>Position: </label>
                </div>
                <div class="col-md-4">
                    <input name="position" type="text"/>
                </div>
                <div class="col-md-4">
                    <div class="error-wrapper">
                        <div class="hideError"> Field is blank </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <label>Email: </label>
                </div>
                <div class="col-md-4">
                    <input name="email" type="text"/>
                </div>
                <div class="col-md-4">
                    <div class="error-wrapper">
                        <div class="hideError"> Field is blank </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <label>Contact Number: </label>
                </div>
                <div class="col-md-4">
                    <input name="contactNumber" type="text"/>
                </div>
                <div class="col-md-4">
                    <div class="error-wrapper">
                        <div class="hideError"> Field is blank </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-4">
                    <label>Photos: </label>
                </div>
                <div class="col-md-4">
                    <div class="photo-wrapper">
                        <input type="file" name="myImage" accept="image/*" />
                    </div>
                </div>
                <div class="col-md-4">
                    <div class="error-wrapper">
                        <div class="hideError"> Field is blank </div>
                    </div>
                </div>
            </div>

            <div class="row">
                <div class="col-md-12">
                    <input id="submitBtn" type="submit" value="Submit"/>
                </div>
            </div>
        </form>

0 个答案:

没有答案