简体   繁体   English

如何通过 Spring 将多个图像上传到 cloudinary?

[英]How to upload multiple image to cloudinary by Spring?

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.我正在尝试将 3 张图片上传到 Cloudinary 并将其链接插入 SQL 服务器,但我的代码仍然添加了 1 个链接,因为我设置了相同的链接。 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") @Table(name = "房间")

@Entity(name = "RoomEntity") @Entity(name = "RoomEntity")

@NoArgsConstructor @NoArgsConstructor

@Getter @Getter

@Setter @Setter

@AllArgsConstructor @AllArgsConstructor

public class Room implements Serializable {公共 class 房间实现可序列化 {

@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;
}

The Cloudinary SDKs only accept one file at a time for upload. Cloudinary SDK 一次只接受一个文件进行上传。 In your case, you have to call the upload method thrice to obtain three different URLs.在您的情况下,您必须调用 upload 方法三次以获取三个不同的 URL。

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;
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM