Note The “getResourceAsStream” method with a forward slash (“/”), which represent the root of your web application. Download javax.servlet.jar. Javax.servlet/javax.servlet.jar.zip( 123 k) The download jar file contains the following class files or Java source files. Download Instruction: Click the product name or the file name to start the download. JSR-000315 Java Servlet 3.0 Final Release jar and schema for evaluation. Read file from the source location using FileInputStream and write to ServletOutputStream to send as response. Also Read: Upload File to Server Using Servlet Example. Download File From Server Using Servlet Example. This page contains a download link. Download servlet-api.jar. Servlet/servlet-api.jar.zip( 166 k) The download jar file contains the following class files or Java source files. META-INF/LICENSE META-INF.
- Details
- Written by Nam Ha Minh
- Last Updated on 27 June 2019 | Print Email
- file_name: name of the file.
- file_data: contains binary data of the file. This column has type of longblob which can hold up to 4GB of binary data.
![Flash Flash](/uploads/1/2/6/2/126246172/105055457.png)
- Connect to the database and query for a record based on a specific condition such as upload ID or file name.
- Retrieve the file’s binary data using the method getBlob(column_name) of the returned ResultSet, and obtain its input stream as follows:
- Set content type and content length (file’s length) for the response.
- Set response’s header Content-Disposition so that the client will force the users downloading the file.
- Read byte arrays from the file’s input stream and write them to the client using the response’s output stream, until reaching end of the input stream.
Find Download File
doGet() method, so we will access it via normal URL. The upload id is passed via the URL’s parameter id. In case of error or no record found, the servlet sends appropriate message to the client.Supposing the application is deployed under the context DatabaseFileDownload on localhostServlet Download File Example
at port 8080, and we want to download file of the record whose upload_id is 123, type the following URL into browser’s address bar:http://localhost:8080/DatabaseFileDownload/downloadFileServlet?id=123
![Download Download](/uploads/1/2/6/2/126246172/480781046.jpg)
Related Java File Dowload Tutorials:
Other Java Servlet Tutorials:
Servlet Download Multiple Files
About the Author:
Nam Ha Minh is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook.[Eclipse project] | 675 kB |
[servlet code] | 3 kB |
[SQL script] | 0.2 kB |