add sample download file and article code (#4370)
This commit is contained in:
parent
523fbf433c
commit
3925638b74
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.servlets;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.OutputStream;
|
||||||
|
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.annotation.WebServlet;
|
||||||
|
import javax.servlet.http.HttpServlet;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
@WebServlet("/download")
|
||||||
|
public class DownloadServlet extends HttpServlet {
|
||||||
|
private final int ARBITARY_SIZE = 1048;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
|
resp.setContentType("text/plain");
|
||||||
|
resp.setHeader("Content-disposition", "attachment; filename=sample.txt");
|
||||||
|
|
||||||
|
OutputStream out = resp.getOutputStream();
|
||||||
|
InputStream in = req.getServletContext().getResourceAsStream("/WEB-INF/sample.txt");
|
||||||
|
|
||||||
|
byte[] buffer = new byte[ARBITARY_SIZE];
|
||||||
|
|
||||||
|
int numBytesRead;
|
||||||
|
while ((numBytesRead = in.read(buffer)) > 0) {
|
||||||
|
out.write(buffer, 0, numBytesRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
in.close();
|
||||||
|
out.flush();
|
||||||
|
}
|
||||||
|
}
|
1
javax-servlets/src/main/webapp/sample.txt
Normal file
1
javax-servlets/src/main/webapp/sample.txt
Normal file
@ -0,0 +1 @@
|
|||||||
|
nice simple text file
|
Loading…
x
Reference in New Issue
Block a user