I'm trying to upload 3 image to Cloudinary and insert their link in SQL Server, but my code still add 1 link because I set same link. So my question is what solution to set each image link for each image file. My code below.
<body>
<form:form action="addR" method="post" enctype="multipart/form-data" modelAttribute="room">
<div>
<form:label path="roomTypeID">Room type :</form:label> <select style="width:100px; height: 30px;">
<c:forEach items="${dataRT}" var="rt">
<option value="${rt.idRoomType}">${rt.nameRoomType}</option>
</c:forEach>
</select>
</div>
<table>
<tr>
<td><form:label path="roomName">Room Name: </form:label></td>
<td><form:input path="roomName"/></td>
</tr>
<tr>
<td><form:label path="price">Price</form:label></td>
<td><form:input path="price"/></td>
</tr>
<tr>
<td><form:label path="description">Description</form:label></td>
<td><form:input path="description"/></td>
</tr>
<tr>
<td><form:label path="benefits">Benefits</form:label></td>
<td><form:input path="benefits"/></td>
</tr>
<tr>
<td><form:label path="area">Area</form:label></td>
<td><form:input path="area"/></td>
</tr>
<tr>
<td><form:label path="bed">Beds</form:label></td>
<td><form:input path="bed"/></td>
</tr>
<tr>
<td><form:label path="adults">Adults</form:label></td>
<td><form:input path="adults"/></td>
</tr>
<tr>
<td><form:label path="children">Children</form:label></td>
<td><form:input path="children"/></td>
</tr>
<tr>
<td><form:label path="roomUrl1">Image 1</form:label></td>
<td><form:input type="file" path="file"/></td>
</tr>
<tr>
<td><form:label path="roomUrl2">Image 2</form:label></td>
<td><form:input type="file" path="file"/></td>
</tr>
<tr>
<td><form:label path="roomUrl3">Image 3</form:label></td>
<td><form:input type="file" path="file"/></td>
</tr>
</table>
<div>
<form:button type="submit">Add</form:button>
</div>
</form:form>
@Table(name = "Room")
@Entity(name = "RoomEntity")
@NoArgsConstructor
@Getter
@Setter
@AllArgsConstructor
public class Room implements Serializable {
@Id
@GeneratedValue(generator = "my_generator2")
@GenericGenerator(name = "my_generator2", strategy = "Finalproject.spring.mvc.MyGenerator.GenerationRID")
@Column(name = "RoomID", columnDefinition = "varchar(10)")
private String id;
@Column(name = "RoomTypeID", columnDefinition = "varchar(10)")
private String roomTypeID;
@Column(name = "RoomName", columnDefinition = "nvarchar(50)", unique = true, nullable = false)
private String roomName;
@Column(name = "Price", columnDefinition = "float")
private float price;
@Column(name = "Description", columnDefinition = "nvarchar(max)")
private String description;
@Column(name = "Benefits", columnDefinition = "nvarchar(max)")
private String benefits;
@Column(name = "Area", columnDefinition = "float")
private float area;
@Column(name = "Bed", columnDefinition = "int")
private int bed;
@Column(name = "Adults", columnDefinition = "int")
private int adults;
@Column(name = "Children", columnDefinition = "int")
private int children;
@Column(name = "RoomUrl", columnDefinition = "nvarchar(max)")
private String roomUrl1;
@Column(name = "RoomUr2", columnDefinition = "nvarchar(max)")
private String roomUrl2;
@Column(name = "RoomUr3", columnDefinition = "nvarchar(max)")
private String roomUrl3;
@Column(name = "IsRent", columnDefinition = "bit")
private boolean isRent;
@ManyToOne()
@JoinColumn(name = "RoomTypeID"/* , referencedColumnName = "" */, insertable = false, updatable = false)
private RoomType roomTypeId;
@Transient
private MultipartFile file;
}
public class RoomServiceImpl implements RoomService {
@Autowired
private RoomRepository roomRepository;
@Autowired
private Cloudinary cloudinary;
@Override
public boolean addRoom(Room room) {
try {
Map r = this.cloudinary.uploader().upload(room.getFile().getBytes(),
ObjectUtils.asMap("resource_type", "auto"));
room.setRoomUrl1((String) r.get("secure_url"));
room.setRoomUrl2((String) r.get("secure_url"));
room.setRoomUrl3((String) r.get("secure_url"));
room.setRent(false);
return this.roomRepository.addRoom(room);
} catch (IOException e) {
System.err.println("add image" e.getMessage());
}
return false;
}
CodePudding user response:
The Cloudinary SDKs only accept one file at a time for upload. In your case, you have to call the upload method thrice to obtain three different URLs.
Sample code:
public class RoomServiceImpl implements RoomService {
@Autowired
private RoomRepository roomRepository;
@Autowired
private Cloudinary cloudinary;
@Override
public boolean addRoom(Room room) {
try {
room.setRoomUrl1((String) upload(room).get("secure_url"));
room.setRoomUrl2((String) upload(room).get("secure_url"));
room.setRoomUrl3((String) upload(room).get("secure_url"));
room.setRent(false);
return this.roomRepository.addRoom(room);
} catch (IOException e) {
System.err.println("add image" e.getMessage());
}
return false;
}
private Map upload(Room room){
Map r = this.cloudinary.uploader().upload(room.getFile().getBytes(),
ObjectUtils.asMap("resource_type", "auto"));
return r;
}
}