3

I am saving the Multipart file and I am using the Path class Of java.nio.file.Path.And in this Path I am getting the path C:\for\expample\ but I need the path like this C:/for/expample/. Here I am sharing my code where I have tried to do but unfortunately, I didn't get the true path with forwarding slashes.

public String saveFile(MultipartFile theFile, String rootPath, String filePath , String fileNme) throws Exception {
        try {

            Path fPath = null;
            if(theFile != null) {

                Path path = Paths.get(rootPath, filePath);
                if(Files.notExists(path)) {
                    //Create directory if one does not exists
                    Files.createDirectories(path);
                }
                String fileName;
                //Create a new file at that location
                if(fileNme == "") {
                    fileName = theFile.getOriginalFilename();
                }else {
                    fileName = fileNme;
                }

                fPath = Paths.get(rootPath, filePath, fileName);
                if(Files.isRegularFile(fPath) && Files.exists(fPath)) {
                    Files.delete(fPath);
                }

                StringWriter writer = new StringWriter();
                IOUtils.copy(theFile.getInputStream(), writer, StandardCharsets.UTF_8);

                File newFile = new File(fPath.toString());
                newFile.createNewFile();

                try (OutputStream os = Files.newOutputStream(fPath)) {
                    os.write(theFile.getBytes());
                }
            }
            return this.replaceBackslashes(fPath == null ? "" :fPath.normalize().toString());

        }catch (IOException e) {
            e.printStackTrace();
            throw new Exception("Error while storing the file");
        }
    }
Rajan
  • 369
  • 3
  • 18
  • 1
    Why do you need the path like that? It's not a correctly formed Windows path. – daniu Oct 16 '19 at 11:04
  • There are two files i.e. `system file` and `application file` that I have to save in a particular folder in a client's pc. In my local server it's running well all files have been saved successfully in the particular folder. But while running on the AWS server I have ended up with the error saying `Error while storing the file`. I don't know why it happened ?! – Rajan Oct 16 '19 at 11:15
  • For AWS, I doubt the backslashes are your only problem. I haven't worked on it a lot, but I doubt it provides a `C:` drive. – daniu Oct 16 '19 at 11:21
  • `if(fileNme == "")` will not work. See https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java. – VGR Oct 16 '19 at 11:46

3 Answers3

3

try

return fPath == null ? "" : fPath.normalize().toString().replace("\\","/");

Kishita Variya
  • 810
  • 9
  • 19
  • Thanks for your help !! I have tried this but it didn't work. – Rajan Oct 16 '19 at 12:38
  • can you please post a sample string of your fPath ? – Kishita Variya Oct 16 '19 at 12:44
  • `fPath :D:\test-root\SLT\env_files\16-10-2019` . **16-10-2019** is my environment name where both files have been saved . – Rajan Oct 16 '19 at 12:47
  • ideally, if it's a string, it should have been something like `D:\\test-root\\SLT\\env_files\\16-10-2019` because `\t` etc has different meaning – Kishita Variya Oct 16 '19 at 12:50
  • but it would not convert the backslash into forwarding slashes. I have to run on AWS that is Linux based and in AWS Linux it only reads the forward slash. – Rajan Oct 16 '19 at 12:56
0

Convert the full path to a string and use the regular expression like

String str = fPath.toString();
str = str.replace("\\", "/");
Arif Khan
  • 25
  • 1
  • 8
  • Thanks for your help, mate. I have to send the `Path` class of `java.nio.file.Path`, not the string. – Rajan Oct 16 '19 at 12:41
0

Given a Path object having C:\\aaaa\\bbbb, simply replace all double-blackslashes with a forward slash

path.toString().replaceAll("\\\\", "/");

Output: C:/aaaa/bbbb

Sebastian Nielsen
  • 3,835
  • 5
  • 27
  • 43