JSP 图片上传到数据库
JSP Image Upload to Database
下面我有一个代码,它应该以输入元素的形式接受用户上传的图像并将其推送到 MySQL 数据库。我正在使用 Tomcat 9. 为什么它不工作但没有抛出异常?没有任何东西被推送到数据库,也没有办法告诉代码正在执行到什么时候。请注意,我已将 class 嵌入到 jsp 文件中,因为 IntelliJ 对单独的 servlet classes 存在问题。另请注意,代码受 BalusC's answer 影响。如果可能的话,谁能建议一个可以代替使用的替代代码?
<form action="trial4.jsp" method="post" enctype="multipart/form-data">
<input type="text" name="description"/>
<input type="file" name="file"/>
<input type="submit"/>
</form>
static String URL = "localhost:3306/";
static String DATABASE_NAME = "MYDB";
static String USERNAME = "user";
static String PASSWORD = "";
@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String description = request.getParameter("description"); // Retrieves <input type="text" name="description">
Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
InputStream fileContent = filePart.getInputStream();
// ... (do your job here)
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://" + URL + DATABASE_NAME, USERNAME, PASSWORD);
PreparedStatement ps = con.prepareStatement("insert into data(image) values(?)");
ps.setBinaryStream(1, fileContent);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
对于面临类似挑战的其他人,repo that has really helped. The repo owner值得喝杯咖啡。
编辑 03/2022
该文档提供了一个很好的solution,一个更好的是它不涉及第三方库。
下面我有一个代码,它应该以输入元素的形式接受用户上传的图像并将其推送到 MySQL 数据库。我正在使用 Tomcat 9. 为什么它不工作但没有抛出异常?没有任何东西被推送到数据库,也没有办法告诉代码正在执行到什么时候。请注意,我已将 class 嵌入到 jsp 文件中,因为 IntelliJ 对单独的 servlet classes 存在问题。另请注意,代码受 BalusC's answer 影响。如果可能的话,谁能建议一个可以代替使用的替代代码?
<form action="trial4.jsp" method="post" enctype="multipart/form-data">
<input type="text" name="description"/>
<input type="file" name="file"/>
<input type="submit"/>
</form>
static String URL = "localhost:3306/";
static String DATABASE_NAME = "MYDB";
static String USERNAME = "user";
static String PASSWORD = "";
@WebServlet("/upload")
@MultipartConfig
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
String description = request.getParameter("description"); // Retrieves <input type="text" name="description">
Part filePart = request.getPart("file"); // Retrieves <input type="file" name="file">
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
InputStream fileContent = filePart.getInputStream();
// ... (do your job here)
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://" + URL + DATABASE_NAME, USERNAME, PASSWORD);
PreparedStatement ps = con.prepareStatement("insert into data(image) values(?)");
ps.setBinaryStream(1, fileContent);
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}
}
}
对于面临类似挑战的其他人,repo that has really helped. The repo owner值得喝杯咖啡。
编辑 03/2022
该文档提供了一个很好的solution,一个更好的是它不涉及第三方库。