【发布时间】:2020-09-15 10:39:06
【问题描述】:
public void storeEmployeeImageDetails(String folderPath, List<Employee> listEmp) {
try {
for (Employee emp : listEmp) {
Blob image;
String imageType;
image = emp.getImageBlob();
imageType = emp.getImageType();
String empId = emp.getEmployeeID();
if (!"".equals(imageType) && image != null) {
String type = imageType.substring(1);
int blobLength = (int) image.length();
byte[] blobAsBytes = image.getBytes(1, blobLength);
BufferedImage imageData = ImageIO.read(new ByteArrayInputStream(blobAsBytes));
ImageIO.write(imageData, type, new File(folderPath + empId + imageType));
}
}
} catch (SQLException | IOException e) {
e.printStackTrace();
logger.error("Exception Occured While Storing Employee Image In Server Folder: " + e);
}
}
我出错了: java.io.FileNotFoundException: https:\example.com\hr\uploads\profilepics\100.jpg(文件名、目录名或卷标语法不正确)
注意: 这里 folderPath 是
https:\\example.com\\hr\\uploads\\profilepics\\ 我是从 application.properties 文件中获取的。
https:\\example.com 是一个godaddy 服务器,它是一个PHP 项目。我正在尝试将图像从 java 应用程序存储到该服务器。
【问题讨论】:
-
从您提供的小解释来看,您似乎想将文件存储在另一台服务器上的文件系统中的某个位置。使用 http 应该是不可能的,因为如果是这样,那将是一个安全漏洞。因此,即使您能够管理它,也不应该依赖它。查看该服务器的 API 及其提供的内容(它可能接受一些路径作为查询/发布参数)。
-
那个 URL 不是一个 REST API。这是他们服务器中的一个文件夹。有写权限。
-
这不是关于权限,而是关于网络。
-
这与 REST 没有任何关系。如果你想使用 HTTP,服务器需要提供一个端点/http 处理程序,它可能能够将任意 url 转换为本地路径。但是,ImageIO 无法做到这一点 - 您需要使用 url 连接(理想情况下使用 HttpClient,例如 Apache Commons Http 提供的)并将图像发布到该 url。
-
谢谢你的想法,我会试试这个。
标签: java spring spring-boot hibernate file-io