Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
an example setting content type on download
package images;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/image")
public class ImageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String image = req.getParameter("name");
resp.setContentType("image/jpg");
byte[] buffer = new byte[8192];
// NEVER DO THIS!!!!!! (UNLESS YOU ARE A TRAINED PROFESSIONAL)
FileInputStream fileInputStream = new FileInputStream("/tmp/" + image);
ServletOutputStream outputStream = resp.getOutputStream();
int read = fileInputStream.read(buffer,0,8192);
while (read != -1) {
outputStream.write(buffer,0,read);
read = fileInputStream.read(buffer);
}
fileInputStream.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.