add sample download file and article code (#4370)

This commit is contained in:
pauljervis 2018-05-30 18:32:18 +01:00 committed by Eugen
parent 523fbf433c
commit 3925638b74
2 changed files with 36 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1 @@
nice simple text file