SHARE
TWEET

Untitled

a guest Oct 26th, 2017 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package teste;
  2.  
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.File;
  5. import java.io.FileInputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import javax.servlet.ServletException;
  9. import javax.servlet.annotation.WebServlet;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13.  
  14. @WebServlet(urlPatterns = {"/produto/imagens"})
  15. public class ImagemServlet extends HttpServlet {
  16.  
  17.     @Override
  18.     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  19.        try (InputStream is = new FileInputStream(new File("C:\\Projetos\\setas1.png"))) {
  20.            response.setContentType("image/png");
  21.            response.getOutputStream().write(getBytesFromInputStream(is));
  22.        }
  23.     }
  24.  
  25.     public static byte[] getBytesFromInputStream(InputStream is) throws IOException {
  26.         ByteArrayOutputStream os = new ByteArrayOutputStream();
  27.         byte[] buffer = new byte[1024 * 1024];
  28.  
  29.         int len;
  30.         while ((len = is.read(buffer)) != -1) {
  31.             os.write(buffer, 0, len);
  32.         }
  33.  
  34.         return os.toByteArray();
  35.     }
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top