-1

Uploading a file scenario, i am mentioning the file path as like "D:\Workspace\MyProject\InputData\file1.jpg". Mentioning full system file path even the upload file is inside my project source folder.

StringSelection ss = new StringSelection("D:\Workspace\MyProject\InputData\file1.jpg"); 
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); 

But while I run my code in another machine - I need to update file path.

I tried following code because upload file is inside the source folder, but it hasn't worked

StringSelection ss = new StringSelection("InputData\file1.jpg"); 
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); 

Is it any other method to mention the file path (as like only mention source folder file path) without mentioning the system whole path?

Vanjithkumar
  • 69
  • 1
  • 1
  • 8

4 Answers4

0

You can specify the relative path for the file. For example, if you are writing above code in a class A which is under package X of your src directory, then you can specify path in following way:

StringSelection ss = new StringSelection("././InputData/file1.jpg"); 
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null); 

For more details about how to specify relative paths in Java, refer to How to define a relative path in java.

Community
  • 1
  • 1
Mahipal
  • 900
  • 1
  • 5
  • 7
0

you can find your current path first

System.out.println(System.getProperty("user.dir"));

then find the file path base on your current path

Alex Bruce
  • 533
  • 2
  • 10
  • 23
0

You can use:-

StringSelection ss = new StringSelection(System.getProperty("user.dir")+"<apend your path for the file>");
Paras
  • 3,197
  • 2
  • 20
  • 30
0

Thanks for your response.

Finally I found a solution to the above question. I have used following code to fix it..

   //This will return the build absolute path in ss variable
   StringSelection ss=new StringSelection(uploadfile().getAbsolutePath()+"\\"+"TestJpegFile.jpeg");
   Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, null);


//Call this function while setup your path 
public File uploadfile()
    {
        File datadir=new File("InputData\\images\\");
           try
           {
               if(!datadir.exists())
               {
                   datadir.mkdir();
               }
           }catch(Exception e)
           {
               e.printStackTrace();
           }
           return datadir;
    }
Vanjithkumar
  • 69
  • 1
  • 1
  • 8