Why is Case-change of JPG to jpg in web project not taking effect?

时间:2015-05-04 19:33:27

标签: html apache jsp netbeans jpeg

I'm trying to fix a picture display issue and I don't know how to fix it. When I uploaded the files on to my computer some files had extension .jpg and others .JPG. I rename all the pictures and made sure all extensions were .jpg.

Everything seemed to work fine until I created a file path in my web project. I use <img class="img" alt="" src="${initParam.productGalleryImagePath}${product.id} (1).jpg" />.

All the pictures that were originally .jpg upload fine but all the picture that were originally .JPG and converted to .jpg won't upload if I use ${initParam.productGalleryImagePath}${product.id} (1).jpg they will only upload if I use ${initParam.productGalleryImagePath}${product.id} (1).JPG as if changing them to .jpg did not take effect... But with the last statement all the original .jpg wont upload.

So it's either .jpg to display the .jpg or .JPG to display the .JPG. My IDE is apparently case sensitive. My fear is when I will upload my project to my host... How do I fix this?

4 个答案:

答案 0 :(得分:1)

Case sensitivity is something that you must pay attention to. Technically the files are the same but programmatically case differences will break your code as you have discovered.

To be safe, its good you check the extension casing and convert all to a fixed form (say .jpg or .JPG) before you process them further.

Example of such a function is strtolower() in PHP.

Update Example of code to check if a file is actually an image;

function is_image($path)
{
    $a = getimagesize($path);
    $image_type = $a[2];

    if(in_array($image_type, array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP)))
    {
        return true;
    }

    return false;
}

Update2 : Here's a java code chuck link from SO -> Test if file is an image

答案 1 :(得分:1)

Renaming files only to change case can cause all sorts of trouble. It's even worse when you're dealing with source control systems. And worse again when sharing code between users on case-sensitive systems and those on case-insensitive systems. Not helped by things like Tomcat being case sensitive, even when running on a case-insensitive filesystem.

I wouldn't take a chance. Some potential steps:

  • copy the entire project to a new location and open it afresh - see if redeploying from here helps
  • move the jpg files into a new directory, or change something else in their names (just one character would be enough) - the idea being to foil any weird name caching issues
  • similar idea: rename them all to .jpeg

And in future, if you're adjusting the case of filenames, try to do it before adding them to your project.

答案 2 :(得分:1)

it's either .jpg to display the .jpg or .JPG to display the .JPG

.jpg & .JPG represent the same thing (an image in joint photo group format), the difference is in how those files are referenced. Unix based systems (Linux/Mac/etc.) are case sensitive, '.jpg' != '.JPG' while Windows ignores case when looking up files, '.jpg' == '.JPG'.

Standardize your file format or update your upload script to accept any case file extension- lower case is best for most conventions.

答案 3 :(得分:0)

There's no difference as far as the file format goes, they are exactly the same. The only difference comes when referencing the said file.
For instance, if you were trying to access image.JPG and linked it with src="image.jpg" you will most likely get a broken link.

Only difference = When referencing.