0

I am writing a java code to retrieve an attachments from specified location to a file system. The attachment information is stored into database table column, like this

Number        attachment_url                                                     
-----------  -----------------------------------------------------------------  
SAT-3        C:\test_attachments\SAT\SAT-368\thumbs\_thumb_63650.png  
SAT-10       C:\test_attachments\SAT\thumbs\_ts63650.xls
SAT-89       C:\test_attachments\SAT\thumbs\mydoc.pdf

The file extension can be any.

I am able to fetch the attachment url and also able to create a folders. But how I can write the attachment to particular folder ?

File files = new File("E:\\Directory1\\" + m_Number);
            if (!files.exists()) {
                if (files.mkdirs()) {
                    System.out.println("Multiple directories are created!");
                    if (files.exists()) {
                        System.out.println("Directory exists");
                        if (files.canWrite()) {
                            Blob blob = resultSet.getBlob("att_url");


                        } else {
                            System.out.println("Access denied !!!");
                        }
                    }
                } else {
                    System.out.println("Failed to create multiple directories!");
                }

How can I achive this

pnuts
  • 58,317
  • 11
  • 87
  • 139
Sukane
  • 2,632
  • 3
  • 18
  • 19

1 Answers1

1

The attachment url doesn't seem to be a BLOB. I think it's a string. So just get the string and create a File object. If the file exists, you can copy it to your folder.

File files = new File("E:\\Directory1\\" + m_jiraNumber);
    if (!files.exists()) {
        if (files.mkdirs()) {
            System.out.println("Multiple directories are created!");
            if (files.exists()) {
                System.out.println("Directory exists");
                if (files.canWrite()) {
                    String attachment = resultSet.getString("attachment_url").replace("\\", File.separator);
                    Path sourcePath = Paths.get(attachment);
                    Path destPath = Paths.get(files.getAbsolutePath() + File.separator + sourcePath.getFileName().toString());
                    Files.copy(sourcePath, destPath);
                } else {
                    System.out.println("Access denied !!!");
                }
            }
        } else {
            System.out.println("Failed to create multiple directories!");
        }
    }
Moh-Aw
  • 2,976
  • 2
  • 30
  • 44
  • ,Thank You for quick response.I tried copying file but folder is showing empty. I referred http://stackoverflow.com/questions/106770/standard-concise-way-to-copy-a-file-in-java link – Sukane Apr 07 '14 at 12:22
  • so for example the following file doesn't exist? C:\test_attachments\SAT\SAT-368\thumbs\_thumb_63650.png – Moh-Aw Apr 07 '14 at 12:29
  • can you provide example how to copy ? – Sukane Apr 07 '14 at 12:30
  • file exists but still folder is empty – Sukane Apr 07 '14 at 12:31
  • I tried above code but it throws FileAleadyExists Exception. If I use standard copy option with replace existing then there is no error but sub-folders are erased and files are copied without extension – Sukane Apr 07 '14 at 16:34
  • 1
    edited the answer again. the output filename was your directory + jira number. Now it would put the file into that directory and give it the original name. – Moh-Aw Apr 08 '14 at 06:22