-2

So I m trying to create a method through which I will upload a File.But while creating a new file in this method I need to use getBytes() method.But I get error saying.

The method getBytes() is undefined for the type File

How can I overcome this??.Below is my method.

public String SomeFile(File file, String basePath, UrlInfoKey urlInfoKey) {
        LOG.info("basePath for back up is {}", basePath);
        String uploadedPath = null;
        String fileDetails = file.getName();
        String locationDetails = fileDetails;
        String locationFolderPath = basePath+"/"+locationDetails;
        File directory = new File(locationFolderPath);
        if(!directory.isDirectory()) {
            LOG.info("creating the directory at {}", locationFolderPath);
            directory.mkdir();
        }
        String filepath = locationFolderPath+"/"+file.getName().trim();
        LOG.info("file path to back up is {}",filepath);
        File creatingFile = new File(filepath);
        try {
            creatingFile.createNewFile();
            FileOutputStream fos = new FileOutputStream(creatingFile);
            fos.write(file.getBytes());
            fos.close();
            uploadedPath = filepath;
        } catch (Exception e) {
            LOG.info("could not create file ", e.getMessage());
        }
        uploadedPath= updateURLWithDBURL(urlInfoKey,uploadedPath);
        LOG.info("file uploaded at location {}",uploadedPath);
        return uploadedPath;
    }
Line:fos.write(file.getBytes());

Is where I m getting error.Is there any other alternative I could use.Thanks in advance.

Lara
  • 1
  • 2
  • Hi Lara, This may help you https://stackoverflow.com/questions/2520305/java-io-to-copy-one-file-to-another – Pankaj Kant Patel Dec 16 '19 at 05:53
  • Hi Pankaj,I saw the solution that you shared but I dont think so that's what i excatly want.If you have any other alternative for the problem in my code please do share it.Thanks – Lara Dec 16 '19 at 06:28
  • Is there a specific reason you are using the old file io classes of Java instead of its modern API NIO revolving around `Files` and `Paths`? It is much simpler to use. – Zabuzard Dec 16 '19 at 06:39
  • 1
    There is no such method as `File.getBytes()`. See the Javadoc. Unclear what you're asking. – user207421 Dec 16 '19 at 06:59

1 Answers1

-1

If you want to create a new file from the file uploaded you can use createTempFile of java.io.File class like:

*File newFile = File.createTempFile("desiredFileName"+ ":", "extension",
                            oldFile);*

Here oldFile is the uploaded file object.

Seshidhar G
  • 265
  • 1
  • 9