使用Java将映像存储在其他服务器路径中 public void storeEmployeeImageDetails(字符串folderPath,列表listEmp){ 试一试{ 对于(员工emp:ListMP){ 斑点图像; 字符串图像类型; image=emp.getImageBlob(); imageType=emp.getImageType(); 字符串empId=emp.getEmployeeID(); 如果(!“”.equals(imageType)&&image!=null){ 字符串类型=imageType.子字符串(1); int blobLength=(int)image.length(); byte[]blobAsBytes=image.getBytes(1,blobLength); BuffereImage imageData=ImageIO.read(新的ByteArrayInputStream(blobAsBytes)); write(imageData,类型,新文件(folderPath+empId+imageType)); } } }catch(SQLException | IOException e){ e、 printStackTrace(); logger.error(“在服务器文件夹“+e”中存储员工映像时发生异常”); } }
我遇到错误: java.io.FileNotFoundException:https:\example.com\hr\uploads\profilepics\100.jpg(文件名、目录名或卷标语法不正确) 注意:此处文件夹路径是使用Java将映像存储在其他服务器路径中 public void storeEmployeeImageDetails(字符串folderPath,列表listEmp){ 试一试{ 对于(员工emp:ListMP){ 斑点图像; 字符串图像类型; image=emp.getImageBlob(); imageType=emp.getImageType(); 字符串empId=emp.getEmployeeID(); 如果(!“”.equals(imageType)&&image!=null){ 字符串类型=imageType.子字符串(1); int blobLength=(int)image.length(); byte[]blobAsBytes=image.getBytes(1,blobLength); BuffereImage imageData=ImageIO.read(新的ByteArrayInputStream(blobAsBytes)); write(imageData,类型,新文件(folderPath+empId+imageType)); } } }catch(SQLException | IOException e){ e、 printStackTrace(); logger.error(“在服务器文件夹“+e”中存储员工映像时发生异常”); } },java,spring,spring-boot,hibernate,file-io,Java,Spring,Spring Boot,Hibernate,File Io,我遇到错误: java.io.FileNotFoundException:https:\example.com\hr\uploads\profilepics\100.jpg(文件名、目录名或卷标语法不正确) 注意:此处文件夹路径是 https:\\example.com\\hr\\uploads\\profilepics\\我从application.properties文件获取 https:\\example.com是一个godaddy服务器,是一个PHP项目。我正在尝试将java应用程序中的
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。感谢您的想法,我将尝试此方法。
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);
}
}
public class FTPUploader {
FTPClient ftp = null;
public FTPUploader(String host, String user, String pwd) throws Exception{
ftp = new FTPClient();
ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out)));
int reply;
ftp.connect(host);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
throw new Exception("Exception in connecting to FTP Server");
}
ftp.login(user, pwd);
ftp.setFileType(FTP.BINARY_FILE_TYPE);
ftp.enterLocalPassiveMode();
}
public void uploadFile(byte[] localFileFullName, String fileName, String hostDir)
throws Exception {
try {
InputStream input = new ByteArrayInputStream(localFileFullName);
this.ftp.storeFile(hostDir + fileName, input);
}catch(Exception e) {
e.printStackTrace();
}
}
public void disconnect(){
if (this.ftp.isConnected()) {
try {
this.ftp.logout();
this.ftp.disconnect();
} catch (IOException f) {
// do nothing as file is already saved to server
}
}
}
}