Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7889853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T06:12:14+00:00 2026-06-03T06:12:14+00:00

I stumbled onto this problem today, let me explain the issue: I have this

  • 0

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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T06:12:15+00:00Added an answer on June 3, 2026 at 6:12 am

    Following @EJP and @GuillaumePolet advice, I did this:

    ...
    response.setContentType(myContentType);
    BufferedImage im = ImageIO.read(new ByteArrayInputStream(contenido));                           
    output.write(contenido);
    output.flush();
    output.close();
    ...
    

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Today I stumbled onto this plain problem in Matlab: >> 1/(10^309) ans = 0
Today at work, I stumbled upon a problem that was driving me nuts. Basically
I've just started with db4o and I stumbled on to a problem. I have
I stumbled across this problem in F#. Suppose, I want to declare two types
I encountered an interesting thing today that I have never noticed before. It appears
While l was looking over some questions about MEF, I stumbled onto this particular
I stumbled onto this weird behavior with ggplot2s ordering of legends and just can't
i just stumbled onto this comment. public static int lowestOneBit(int i) { // HD,
I stumbled upon this open source project Fake It Easy , and I have
I have a free web site that streams real-time stock-options data. I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.