1

I have problem with upload file to local WebDav. So far i have:

public interface IStorageService {
    URI SaveFile(String filename, InputStream inputStream);
}
@Component
public class LocalStorageService implements IStorageService {
    @Value( "C:\temp" )
    private String filestorePath;
    
    public URI SaveFile(String filename, InputStream inputStream) {
        var rootLocation = Paths.get(filestorePath);
        var filePath = rootLocation.resolve(filename);
        try {
            Files.copy(inputStream, filePath);
        } catch (IOException e) {
            throw new RuntimeException("Failure save file to " + filename + " in " + filestorePath + "." + e.getMessage(), e);
        }
        return filePath.toUri();
    }
}

and controller

    private final DocumentService documentService;

    public DocumentController(DocumentService documentService) {
        this.documentService = documentService;
    }

    @RequestMapping(method = RequestMethod.POST)
    public DocumentModel handleFileUpload(@RequestParam("file") MultipartFile file) throws IOException {        
        return documentService.handleFileUpload(file.getOriginalFilename(), file.getInputStream());
    }

And it is works correctly, the file is uploaded to C:/temp... Now I would like to do the same but upload file to local WebDav. When i change in @Value "C:\temp to "http://localhost" (this is ma webdav location) i have:

invalidpathexception: illegal char <:> at index 4: http://localhost

or when I declare http//localhost without <:>

nosuchfileexception: http\localhost

How can I write my code to upload file directly to WebDav. Parameters of SaveFile method cannot be changed, I need do it with Name as String and InputStream. I tried with Sardine but to no avail. Could someone help me, give any tips or maybe suggestion of code ?

Greetings !

mat373
  • 171
  • 4
  • 13
  • The `java.nio.Files` stuff isn't called `Files` without a reason. That only works for actual file resources not for arbitrary URLs. You will need a better WebDAV client to work with this. – M. Deinum Dec 18 '20 at 09:50

1 Answers1

0

You can get the path where your web app/ war/ servlet / controllers are deployed :

ServletContext context = getContext(); String fullPath = context.getRealPath("/WEB-INF/test/foo.txt");

For a Spring project, in controller

'@Autowired
'ServletContext context; 

And in controller method :

'String uploadPath = context.getRealPath("") + File.separator + UPLOAD_DIRECTORY;

And the real file name, but what if a user uploads same file twice or 2 users upload file with same name?

Better to put in sub directory with user id/ user name and maybe date time or some other identifier like TXN id + some fixed text like

' String fileName = context.getRealPath("") + File.separator + userId + readlName + "xyz."
  • extnFromMimeType;

And store the path in data base for this transaction/ user as per your busibess use case.

if mime type is image/PMG then extnFromMimeType will be "png"; if jpeg or jpg then "jpg"

See

If can be many images per user/ transaction can also use createTemp File to get a unique file name using a UUID also possible ... https://stackoverflow.com/a/1293712/1643558 or a large random number

tgkprog
  • 4,493
  • 4
  • 41
  • 70