View Javadoc
1   package fr.paris.lutece.plugins.ctv.web;
2   
3   import java.awt.image.BufferedImage;
4   import java.io.ByteArrayInputStream;
5   import java.io.IOException;
6   import java.io.OutputStream;
7   import java.util.Base64;
8   
9   import javax.imageio.ImageIO;
10  import javax.servlet.ServletException;
11  import javax.servlet.http.HttpServlet;
12  import javax.servlet.http.HttpServletRequest;
13  import javax.servlet.http.HttpServletResponse;
14  
15  import fr.paris.lutece.plugins.ctv.bo.Emprise;
16  import fr.paris.lutece.plugins.ctv.business.IEmpriseService;
17  import fr.paris.lutece.plugins.ctv.business.IStockageImageService;
18  import fr.paris.lutece.portal.service.spring.SpringContextService;
19  import fr.paris.lutece.portal.service.util.AppPropertiesService;
20  
21  public class EmpriseImageServlet extends HttpServlet
22   {
23  	
24  	private static final long serialVersionUID = 1L;
25  
26  	@Override
27  	public void doGet(HttpServletRequest request, HttpServletResponse resp)
28              throws ServletException, IOException {
29  		
30  	    IStockageImageService stockageService = SpringContextService.getBean( "stockageImageService" );
31  	    IEmpriseService empriseService = SpringContextService.getBean( "empriseService" );
32  
33  		String pathFolder = AppPropertiesService.getProperty( "ctv.directory.upload" );
34  		
35  		int empriseId = Integer.parseInt(request.getParameter("emprise_id"));
36  		
37  		Emprise emprise = empriseService.findEmpriseById(empriseId );
38          String imageBase64 = stockageService.getImageFromServer( pathFolder, emprise.getEmpriseDateCreation( ), empriseId );
39          OutputStream out = resp.getOutputStream();
40          writeOutputStream(imageBase64, out);
41  
42          resp.setContentType("image/jpg");
43          resp.setHeader("Pragma", "");
44          resp.setHeader("Cache-Control", "");
45          resp.setHeader("Content-Disposition", "inline; fileName=emprise.jpg");
46      }
47  
48      private void writeOutputStream(String value, OutputStream outputStream) throws IOException {
49          byte[] imgBytes = Base64.getDecoder().decode(value.replace("data:image/jpeg;base64,",""));
50          BufferedImage bufImg = null;
51          if(imgBytes.length > 0) {
52              bufImg = ImageIO.read(new ByteArrayInputStream(imgBytes));
53  
54          } else {
55          	bufImg = new BufferedImage(1, 1,
56          		    BufferedImage.TYPE_INT_RGB);
57          }
58      	ImageIO.write(bufImg, "jpg", outputStream);
59  
60      }
61  
62  }