Java 上载到本地WebDav-spring启动

Java 上载到本地WebDav-spring启动,java,spring-boot,file-upload,inputstream,webdav,Java,Spring Boot,File Upload,Inputstream,Webdav,我无法将文件上载到本地WebDav。到目前为止,我已经: public interface IStorageService { URI SaveFile(String filename, InputStream inputStream); } 和控制器 private final DocumentService documentService; public DocumentController(DocumentService documentService) {

我无法将文件上载到本地WebDav。到目前为止,我已经:

public interface IStorageService {
    URI SaveFile(String filename, InputStream inputStream);
}
和控制器

    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());
    }
'@Autowired
'ServletContext context; 
它工作正常,文件上传到C:/temp。。。 现在我想做同样的事情,但上传文件到本地WebDav。当我将@Value“C:\temp改为”时http://localhost“(这是ma webdav的位置)我有:

如何编写代码将文件直接上传到WebDav。 无法更改SaveFile方法的参数,我需要使用名称String和InputStream进行更改。 我试过用沙丁鱼,但没用。有人能帮我一下,给我一些建议或是代码建议吗


您好!

您可以获得部署web应用程序/war/servlet/控制器的路径:

ServletContext=getContext(); 字符串fullPath=context.getRealPath(“/WEB-INF/test/foo.txt”)

对于Spring项目,在控制器中

    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());
    }
'@Autowired
'ServletContext context; 
在控制器方法中:

'String uploadPath = context.getRealPath("") + File.separator + UPLOAD_DIRECTORY;
和真实的文件名,但如果一个用户上传相同的文件两次或两个用户上传相同名称的文件怎么办

最好将用户id/用户名和日期时间或其他标识符(如TXN id+一些固定文本)放入子目录中

' String fileName = context.getRealPath("") + File.separator + userId + readlName + "xyz."
  • extnFromMimeType
并根据您的业务用例将此事务/用户的路径存储在数据库中

如果mime类型为image/PMG,则extnFromMimeType为“png”;如果为jpeg或jpg,则为“jpg”

如果每个用户/事务可以有多个图像,还可以使用createTemp文件获取唯一的文件名
也可以使用UUID…或大随机数

您可以获得部署web应用程序/war/servlet/控制器的路径:

ServletContext=getContext(); 字符串fullPath=context.getRealPath(“/WEB-INF/test/foo.txt”)

对于Spring项目,在控制器中

    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());
    }
'@Autowired
'ServletContext context; 
在控制器方法中:

'String uploadPath = context.getRealPath("") + File.separator + UPLOAD_DIRECTORY;
和真实的文件名,但如果一个用户上传相同的文件两次或两个用户上传相同名称的文件怎么办

最好将用户id/用户名和日期时间或其他标识符(如TXN id+一些固定文本)放入子目录中

' String fileName = context.getRealPath("") + File.separator + userId + readlName + "xyz."
  • extnFromMimeType
并根据您的业务用例将此事务/用户的路径存储在数据库中

如果mime类型为image/PMG,则extnFromMimeType为“png”;如果为jpeg或jpg,则为“jpg”

如果每个用户/事务可以有多个图像,还可以使用createTemp文件获取唯一的文件名
也可以使用UUID…或大量随机数

无理由调用
java.nio.Files
文件东西。这只适用于实际文件资源,而不适用于任意URL。您需要更好的WebDAV客户端来处理此问题。
java.nio.Files
东西在没有原因。这只适用于实际文件资源,而不适用于任意URL。您需要一个更好的WebDAV客户端来处理此问题。