java - 使用 Springboot 在托管服务器上上传图像
问题描述
我需要一些帮助来上传图片。
我有两个不同的应用程序
- Springboot 应用程序 - 在我的专用服务器上运行
- Angular-6 应用程序 - 在我的云服务器上运行
我需要将我的 spring-boot 应用程序中的图像上传到我的 Angular-6 assets 文件夹(assets 文件夹就像我们 spring-boot 应用程序的资源文件夹)
在 application.properties 我声明了上传位置,例如 profile_path=D:/documents/profile/ (它的工作文件带有我的文件系统路径)
if (!profileImage.isEmpty()) {
String fileName = profileImage.getOriginalFilename();
String fileExtenstion = FilenameUtils.getExtension(fileName);
InputStream is = profileImage.getInputStream();
Files.copy(is, Paths.get(profile_path + userDetailId + "." + fileExtenstion),
StandardCopyOption.REPLACE_EXISTING);
profileImagePath = profile_path + userDetailId + "." + fileExtenstion;
}
现在,问题出在云服务器路径上我如何上传http://www.example.com/documents/profiles/或 //103.XXX.XX.XX/documents/profiles/
任何人都可以给我一个建议。
解决方案
最后我找到了以下解决方案。
解决方案1: 我们可以使用“D://Documents/”之类的路径将图像上传到我们的文件系统。我们只需要添加我们要上传图片的路径。
解决方案2: 我们可以使用“file:////103.XXX.XX.XX//Documents/”之类的IP将图像上传到服务器文件系统。我们需要提供带有 IP 地址的远程位置的路径。
解决方案 3: 我们可以使用 FTPClient (Java-Networking) 在我们的托管站点上上传图像。以下是参考资料:
- 对于文件上传: https ://www.codejava.net/java-se/networking/ftp/java-ftp-file-upload-tutorial-and-example
- 对于删除文件: https ://www.codejava.net/java-se/networking/ftp/delete-a-file-on-a-ftp-server
- 对于 checkDirectoryOrFileExistOrNot : https://www.codejava.net/java-se/networking/ftp/determine-if-a-directory-or-file-exists-on-ftp-server