Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Java Program to generate magento product image directories
 
sanumala
Jr. Member
 
Total Posts:  1
Joined:  2009-09-18
 

Hi

I have written a small java program which will create all required sub directories and copy the image file into them so that we can directly upload those images to media/catalog/product

This program is useful for everyone who whats to import products using csv or excel. Before importing any products Run this java program and it will give output like this

/6/4/64studio.jpg
/a/b/absolutelinux.jpg
/a/d/adios.gif
/a/l/alinux.png

After running this program you can ftp target dir’s to sever

/**
 * 
 */
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author sanumala
 * 
 */
public class MagentoProductImageManager {

    
private String iSourceDir;
    private 
String iDestinationDir;

    public 
MagentoProductImageManager(String pSourceDirString pDestDir{
        setSourceDir
(pSourceDir);
        
setDestinationDir(pDestDir);
    
}

    
public String getSourceDir() {
        
return iSourceDir;
    
}

    
public void setSourceDir(String pSourceDir{
        this
.iSourceDir pSourceDir;
    
}

    
public String getDestinationDir() {
        
return iDestinationDir;
    
}

    
public void setDestinationDir(String pDestinationDir{
        this
.iDestinationDir pDestinationDir;
    
}

    
public void doMaintanance() {
        String mkdirCmd 
null;
        
String dirName null;
        
String tempFileName null;
        
String copyCmd null;
        
File sourceFolder = new File(getSourceDir());
        
File destFolder = new File(getDestinationDir());
        if (!
destFolder.exists()) {
            executeSystemCmd(
"mkdir -p " getDestinationDir());
        
}
        
for (File file sourceFolder.listFiles()) {
            
if (file.isFile(){
                tempFileName 
file.getName();
                if (!
tempFileName.equalsIgnoreCase(".ds_store")) {
                    dirName 
getDestinationDir() + "/"
                            
tempFileName.substring(01) + "/"
                            
tempFileName.substring(12);
                    
mkdirCmd "mkdir -p " dirName;
                
}

                
if (dirName != null && mkdirCmd != null
                        
&& !new File(dirName).exists()) {
                    executeSystemCmd(mkdirCmd)
;
                
}
                
// Now Copy file to destination directory
                
copyCmd "cp " getSourceDir() + "/" tempFileName " "
                        
dirName "/";
                
executeSystemCmd(copyCmd);
            
}
        }
    }

    
private List<FilegetFileListingWithoutSort(File pStartingDir)
            
throws FileNotFoundException {
        
List<Fileresult = new ArrayList<File>();
        
File[] filesAndDirs pStartingDir.listFiles();
        List<
FilefilesDirs Arrays.asList(filesAndDirs);
        for (
File file filesDirs{
            
// always add, even if directory
            
result.add(file);
            if (!
file.isFile(){
                
// must be a directory
                // recursive call!
                
List<FiledeeperList getFileListingWithoutSort(file);
                
result.addAll(deeperList);
            
}
        }
        
return result;
    
}

    
public void executeSystemCmd(String pCmd) {
        
try {
            Process process 
Runtime.getRuntime().exec(pCmd);
            
process.waitFor();
        
catch (IOException e{
            
// TODO Auto-generated catch block
            
e.printStackTrace();
        
catch (InterruptedException ie{
            ie
.printStackTrace();
        
}
    }

    
private void printFileNames() {
        
try {
            
List<FilereturnList getFileListingWithoutSort(new File(
                    getDestinationDir
()));
            
Collections.sort(returnList);
            for (
File tempFile returnList{
                
if (tempFile.isFile()
                        
&& !tempFile.getName().equalsIgnoreCase(".ds_store")) {
                    System
.out.println(tempFile.toString().substring(
                            
getDestinationDir().length(),
                            
tempFile.toString().length()));
                
}
            }
        } 
catch (FileNotFoundException e{
            e
.printStackTrace();
        
}
    }

    
// main method
    
public static void main(String[] args{
        MagentoProductImageManager manager 
= new MagentoProductImageManager(
                
"/Users/sanumala/Desktop/logos",
                
"/Users/sanumala/Desktop/templogos");
        
manager.doMaintanance();
        
manager.printFileNames();
    
}
}

Note Change /Users/sanumala/Desktop/logos and /Users/sanumala/Desktop/templogos values as per your needs and put all your images into one directory without sub directories.
This java class will automatically create destination directory.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top