Java tutorial
/* * #%L * unidle * %% * Copyright (C) 2013 Martin Lau * %% * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * #L% */ package org.unidle.service; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import org.unidle.domain.Attachment; import org.unidle.repository.AttachmentRepository; import java.io.IOException; @Service public class AttachmentServiceImpl implements AttachmentService { private final AttachmentRepository attachmentRepository; @Autowired public AttachmentServiceImpl(final AttachmentRepository attachmentRepository) { this.attachmentRepository = attachmentRepository; } @Override @Transactional public Attachment createAttachment(final MultipartFile file) throws IOException { final Attachment attachment = new Attachment(); attachment.setContent(file.getBytes()); attachment.setContentType(file.getContentType()); attachment.setTitle(file.getName()); return attachmentRepository.save(attachment); } }