I stumbled onto this problem today, let me explain the issue:
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.
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.
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.
This is the method that does the trick:
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<br>");
}
}
return null;
}
Then, on the JSPs where I need to display the pictures I just do this:
<img class="pic" alt="Foto destacada" src="descargarFichero.do?fileName=<c:out value="${foto.nombre}"/>/thumb_<c:out value="${foto.nombre}"/>"/>
The action “descargarFichero.do” calls the method I posted.
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.
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.
Thanks a lot
P.S. Another method you might need
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;
}
Following @EJP and @GuillaumePolet advice, I did this:
I skipped the JPG re-encoding part and it worked just fine. Also corrected some ugly code using the suggestions on the other comments (I don’t post it here because it’s really not related to the problem I was facing).
Thanks a lot for your contributions!