title: Jpg encoding with Java
body: <p>I stumbled onto this problem today, let me explain the issue:</p>
  
  <p>I have this Java web application that deals with a lot of image files. It only allows JPGs files (company policy I guess...). The files are stored on some fixed location, and the upload process and thumbnail generation is going pretty well. The pictures are shown using a Java class because the company doesn´t want to show the path.</p>
  
  <p>The thing is that some of the pictures shown are dark; not all black, just a little darker than the original uploaded file. I checked the uploaded files and thumbnails and they look well, they are not darkened at all.</p>
  
  <p>This is where the thing gets weird: I tested on localhost, and the pictures display correctly, when I test on the server they look dark. The only difference between the two is that on localhost I work on Windows and the test server is Linux. Both use JBOSS 4.2.0.</p>
  
  <p>This is the method that does the trick:</p>
  
  <pre><code>protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
          throws Exception {              
  
      String ruta = rutaBase + File.separator;
      String fileName=(String)request.getParameter("fileName");
  
      if (fileName==null) 
          fileName="";
      if (!fileName.equals("")) {
          RemoteFileClient oRemoteFileClient = new RemoteFileClient();
          ServletOutputStream output = response.getOutputStream();
          if (oRemoteFileClient.isAlive()) {
              String file2download=ruta+fileName;
              byte[] contenido=oRemoteFileClient.downloadFile(file2download);                 
              if (contenido!=null) {
                  String myContentType="";
                  boolean isImage=false;
                  String nombreFichero = fileName.toLowerCase();
                  String extension = nombreFichero.substring(nombreFichero.lastIndexOf(".")+1);
                  if (extension.equals("jpg") ||
                      extension.equals("jpeg") ||
                      extension.equals("JPG") ||
                      extension.equals("JPEG")) {
                      myContentType="image/jpeg";
                      isImage=true;
                  } 
                  else if (extension.equals("gif") || extension.equals("GIF")) {
                      myContentType="image/gif";
                      isImage=true;
                  } 
                  else if (extension.equals("png") || extension.equals("PNG")) {
                      myContentType="image/png";
                      isImage=true;
                  } 
                  else if (extension.equals("doc") || extension.equals("DOC") || 
                              extension.equals("rtf") || extension.equals("RTF")) {
                      myContentType="application/msword";
                  } 
                  else if (extension.equals("bin") || 
                             extension.equals("exe")) {
                      myContentType="application/octet-stream";
                  } 
                  else if (extension.equals("zip")) {
                      myContentType="application/x-zip";
                  } 
                  else if (extension.equals("pdf")) {
                      myContentType="application/pdf";
                  } 
                  else if (extension.equals("txt")) {
                      myContentType="text/plain";
                  } 
                  else if (extension.equals("xls")) {
                      myContentType="application/ms-excel";
                  } 
                  else if (extension.equals("ppt")) {
                      myContentType="application/ms-powerpoint";
                  }
                  response.setContentType(myContentType);
                  if (isImage) {
                      BufferedImage im = ImageIO.read(new ByteArrayInputStream(contenido));                           
                      JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(output);
                      //JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(im);
                      //param.setQuality(1.0f,true);
                      //encoder.encode(im, param);
                      encoder.encode(im);
                  } else {
                      output.write(contenido);
                  }
                  output.flush();
                  output.close();
              }
          } 
          else{
              output.println("No se puede conectar con el servidor de archivos&lt;br&gt;");
          }
      }
  
      return null;
  }
  </code></pre>
  
  <p>Then, on the JSPs where I need to display the pictures I just do this:</p>
  
  <pre><code>&lt;img class="pic" alt="Foto destacada" src="descargarFichero.do?fileName=&lt;c:out value="${foto.nombre}"/&gt;/thumb_&lt;c:out value="${foto.nombre}"/&gt;"/&gt;
  </code></pre>
  
  <p>The action "descargarFichero.do" calls the method I posted.</p>
  
  <p>My wild guess is that there is some issue with the JPG encoding but I don't know much about these things so I gladly hear (read) your suggestions.</p>
  
  <p>Remember, just some of the pictures are showing dark, not all of them, I'm looking the troubled files but haven't found anything suspicious yet.</p>
  
  <p>Thanks a lot</p>
  
  <p>P.S. Another method you might need</p>
  
  <pre><code>public byte[] downloadFile(String filename) {
      byte downloadfile[] = null;
      try {
          byte buffer[];
          File file = new File(filename);
          if(file.exists() || (!file.isDirectory())) {
              buffer = new byte[(int)file.length()];
              BufferedInputStream input = new BufferedInputStream(new FileInputStream(filename));
              input.read(buffer, 0, buffer.length);
              input.close();
              downloadfile = buffer;
          } else {
              //break MISSING_BLOCK_LABEL_65;
              return null;
          }
      } catch(Exception exception) {
          //System.out.println("FileImpl: " + e.getMessage());
          exception.printStackTrace();
          return null;
      }
      return downloadfile;
  }
  </code></pre>