package example.u2ware.springfield.part4.step3; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id; import javax.persistence.Transient; import lombok.Getter; import lombok.Setter; import org.springframework.web.multipart.MultipartFile; import com.u2ware.springfield.config.Springfield; import com.u2ware.springfield.config.Springfield.Strategy; import com.u2ware.springfield.support.multipart.UploadFile; @Springfield( strategy=Strategy.JPA, methodLevelMapping={"*","read.stream","read.download"} ) @Entity public class AttachedFile implements UploadFile{ @Transient private @Getter @Setter MultipartFile multipartFile; @Id @GeneratedValue private @Getter @Setter Integer id; private @Getter @Setter String contentFile; private @Getter @Setter String contentName; private @Getter @Setter String contentType; private @Getter @Setter long contentSize; }
package example.u2ware.springfield.part4.step3; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.u2ware.springfield.repository.EntityRepository; import com.u2ware.springfield.service.EntityServiceImpl; import com.u2ware.springfield.support.multipart.UploadFileRepository; @Service public class AttachedFileService extends EntityServiceImpl<AttachedFile, Integer>{ protected final Log logger = LogFactory.getLog(getClass()); @Autowired protected UploadFileRepository uploadFileRepository; @Autowired public AttachedFileService( @Qualifier("attachedFileRepository") EntityRepository<AttachedFile, ?> r) { super("attachedFileRepository", r); } @Transactional public AttachedFile create(AttachedFile entity) { uploadFileRepository.saveFile(entity); return super.create(entity); } @Transactional public AttachedFile delete(AttachedFile entity) { AttachedFile newEntity = super.read(entity); uploadFileRepository.deleteFile(newEntity); return super.delete(newEntity); } }
Springfield 에서 제공하는 com.u2ware.springfield.support.multipart.UploadFileRepository 은 com.u2ware.springfield.support.multipart.UploadFile 를 이용하여 업로드 파일을 관리한다.
public interface UploadFileRepository { public void saveFile(UploadFile bean) ; public void deleteFile(UploadFile bean) ; }
public interface UploadFile extends DownloadFile{ public MultipartFile getMultipartFile(); public void setContentFile(String contentFile); public void setContentName(String contentName); public void setContentType(String contentType); public void setContentSize(long contentSize); }
public interface DownloadFile { public String getContentFile(); public String getContentName(); public String getContentType(); public long getContentSize(); }
@Springfield 에 의해 download 와 stream 경로가 추가 되고, view resolver 설정 에 의해 파일 다운로드(download)와 미리 보기(stream)가 동작한다.
매핑 경로 | HTTP Method | Controller Method 이름 | 예제 Command 객체 | 예제 매핑 경로 |
/{topLevelMapping}/{id} | GET | read | AttachedFile | /part4/step3/7 /part4/step3/7.download /part4/step3/7.stream |