]]>
// UploadServlet.java
@WebServlet("/UploadServlet")
public class UploadServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Part filePart = request.getPart("file");
String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString();
InputStream fileContent = filePart.getInputStream();
// Save the file to disk or database
// Here we save it to a directory for simplicity
Files.copy(fileContent, Paths.get("uploads/" + fileName),
StandardCopyOption.REPLACE_EXISTING);
}
}
]]>
// DatabaseOperations.java
public class DatabaseOperations {
public static void addFileToDatabase(String fileName, String filePath) {
try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/documentdb", "root", "password")) {
PreparedStatement pstmt = conn.prepareStatement("INSERT INTO files (name, path) VALUES (?, ?)");
pstmt.setString(1, fileName);
pstmt.setString(2, filePath);
pstmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
]]>
本站知识库部分内容及素材来源于互联网,如有侵权,联系必删!