Laksanakan Servlet Muat Turun Fail Mudah
Untuk melaksanakan servlet muat turun fail ringkas, ikuti langkah berikut:
Buat a Servlet:
Laksanakan Servlet :
Berikut ialah contoh pelaksanaan servlet:
public class DownloadServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); // Retrieve file name and type using ID from database response.setContentType("fileType"); response.setHeader("Content-disposition", "attachment; filename=yourcustomfilename.pdf"); File file = new File("fileName"); OutputStream out = response.getOutputStream(); try (FileInputStream in = new FileInputStream(file)) { byte[] buffer = new byte[4096]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } } finally { out.flush(); } } }
Atas ialah kandungan terperinci Bagaimana untuk Melaksanakan Servlet Muat Turun Fail Mudah?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!