Search code examples
javaspringeclipsemavenintellij-idea

Using maven multi module, spring boot - issue with Creating Directory in IntelliJ vs. Eclipse


I have a Spring boot maven mutliple module project where I'm trying to create a directory in my code using the Files.createDirectories method. The project is structured as follows:

-GlobalProject :                                                                                                                                                                                                      
    CommonProject:                                                                                                                                                                                       
    WebParentPrject:
         backendPorject:                                                                                                                                                   
         frontendProoject: 

I want to create a directory named user-images in the backendPorject directory. The following code(in backendproject) works fine in Eclipse, but it's not behaving as expected in IntelliJ:

public class FileUploadUtil {

    public static void saveFile(String uploadDir, String fileName, MultipartFile multipartFile) throws IOException {
        Path uploadPath = Paths.get(uploadDir);
        if(!Files.exists(uploadPath))
        {
            Path directories = Files.createDirectories(uploadPath);
            System.out.println(directories);
        }
        try (InputStream inputStream = multipartFile.getInputStream()){
            Path filePath = uploadPath.resolve(fileName);
            Files.copy(inputStream,filePath, StandardCopyOption.REPLACE_EXISTING);
        }catch (IOException exception){
            throw new IOException("Could not save file : "+fileName,exception);
        }
    }

In Eclipse, the directory is created within the backendProject directory, but in IntelliJ, the directory is being created in the root directory of the entire project (MyProject). I want the directory to be created in the backendProject directory in both IDEs.

Could someone help me understand why the behavior is different between Eclipse and IntelliJ, and how I can modify my code to ensure consistent behavior in both IDEs?


Solution

  • File operations work relative to the current working directory, which you can set in IntelliJ IDEA Run configuration.

    Example for Spring Boot:

    Boot working directory