1

I have a web Spring boot based application where users can upload images, the problem the images are big like 3 or 4 MB each. is there any way to compress this images so I can reduce images to some 50 or 80 KB like most web application . I save the images in file System not database. here the important part of my application related to images upload.

add.jsp :

<div class="col-md-3 ">
      <springform:input  type="file"  path="photos" name="photos" style=" visibility:  hidden ; " onchange="imagepreview(this);"/>
       <label for="photos" >
         <img class="images img-thumbnail"  id="imgpreview" src="/imagetemp/?chemintemp=${chemintemp}"  onError="this.onerror=null;this.src='/images/noimage.gif';" />  
       </label> 
     </div>

query.js:

function imagepreview(input){
      if (input.files && input.files[0]){
        var filerd =new FileReader();
        filerd.onload=function (e){
            $("#imgpreview").attr('src',e.target.result);
        };
        filerd.readAsDataURL(input.files[0]);
      }
  };

Controler :

@RequestMapping(value="/imagetemp",method = RequestMethod.GET)
    public @ResponseBody void affichimagetemp(//@RequestParam("chemintemp") String img,
            Model md, HttpSession session,
            HttpServletResponse response,
            HttpServletRequest request) throws IOException,
            NullPointerException

    {
        String img=(String) session.getAttribute("chemintemp");
        if (img!=null){

            try {
                System.out.println("img /imagetemp :"+img);
                File imageFile = new File(img);
                response.setContentType("image/jpeg");
                InputStream in=new FileInputStream(imageFile);
                IOUtils.copy(in, response.getOutputStream());
                in.close();
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

           }

    }
Omar B.
  • 441
  • 11
  • 39

0 Answers0