Java Image Downloader
Whenever your company gets new software, you’re bound to work overtime as a programmer. The staff figured out that you can load images into the POS software and the sales staff can see the image for each part. Hooray…for them.
“This shouldn’t be a problem for you, right Dino? We absolutely need this to work. This would improve sales drastically!” …Right.
So I contact the company and ask them if there’s any way to import the images in bulk. They tell me that there is. Apparently you can upload an entire folder into the system. As long as each image is named exactly like the part number, the system will link the image to that particular part. Seems like an inefficient use of resources, but ok. We only have 17,000 part numbers. We wholesale wheels and tires and there are a lot of items.
I have all of the images on the website. The way they’re named is brand/model_finish.jpg/png for wheels and brand/model.jpg/png for tires. Each wheel has a brand, a model, and a finish, so the XXR 575 Phantom Black would be stored as xxr/575_phantom_black.jpg for each part number matching those three criteria. A tire has no finish (just rubber), so a Delinte DS8 would be stored as delinte/ds8.jpg.
The database stores all of the parts. The brand, model, wheel finish, etc, are separated into different tables for normalization purposes. When a person goes to a particular page on the site, the data for that page is retrieved from the table(s), the image src is constructed, and the image is displayed next to each part number.
Great! I just need to go through my database, extract the data, construct the image src for each item and download the image. My programming language of choice? Java.
Since I need the MySQL connector, I decide to make this a maven project. In my pom.xml file, I include the mysql-connector-java dependency.
Next it’s time to create the DBConnection class. This will just connect to the backed-up database on my local server.
Inside the Main class, I specify whether I want to retrieve data for wheels or tires. The main method then calls either the getWheelQuery() or getTireQuery(). Each one of these methods just returns a String (SQL statement). The query is then executed and the data is stored in the resultSet.
The resultSet and the type (wheel or tire) arguments are passed to the Item.getItems() method. The getItem() method calls either the downloadWheels() or downloadTires() method. For each item inside the database, the columns are sanitized to remove any characters that are not used in image creation, and the image is saved (hopefully). Since the website does allow for both jpg and png files, both are constructed and tested for.
After only a few minutes, the wheels and tires folders were filled with images whose names matched the part numbers they represented. I dragged the folder into the POS software and watched the magic happen. After only a couple of hours since they first approached me to do this, 17,000 images were loaded into the system and the entire company was happy. I’m going to ask the sales staff daily if they’re selling more since the images are now loaded…I mean it was of paramount importance after all.
Dino Cajic is currently the Head of IT at LSBio (LifeSpan BioSciences, Inc.), Absolute Antibody, Kerafast, Everest BioTech, Nordic MUbio and Exalpha. He also serves as the CEO at MyAutoSystem. He has over a decade of software engineering experience. He has a B.S. in Computer Science and a minor in Biology. His background consists of creating enterprise level e-commerce applications, performing research based software development, and facilitating the spread of knowledge through writing.
You can connect with him on LinkedIn, follow him on Instagram, or subscribe to his Medium publication.