前提・実現したいこと
Springboot+JPA+thymleafで画像をアップロードするサイトを作り、画像は10KBほどのJPGを使用してテストしています。
画像をbyte配列に変換して、DB(MYSQL)に登録しようとすると、データが長すぎるといったエラーが出ます。
(文字数制限のためエラーコード割愛しています)
この場合、MYSQL側ではblob型として登録してくれるわけではないのでしょうか?
正常に登録できる方法を教えていただきたいです。
発生している問題・エラーメッセージ
ERROR 4952 --- [nio-8080-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper : Data truncation: Data too long for column 'img' at row 1 org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:280) at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:233) at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:551) at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:152) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:174) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:215) at com.sun.proxy.$Proxy76.save(Unknown Source) at myPortfolio.service.WorksService.save(WorksService.java:31) at myPortfolio.controller.SettingController.registWorks(SettingController.java:37) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.base/java.lang.reflect.Method.invoke(Method.java:566) at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150) at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895) at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
該当のソースコード
Entity
1package myPortfolio.entity; 2import java.io.Serializable; 3 4import javax.persistence.Entity; 5import javax.persistence.GeneratedValue; 6import javax.persistence.GenerationType; 7import javax.persistence.Id; 8 9import lombok.Data; 10 11@Data 12@Entity 13public class WorksEntity implements Serializable{ 14 15 private static final long serialVersionUID = 8222254821577665211L; 16 @Id 17 @GeneratedValue(strategy = GenerationType.IDENTITY) 18 private int id; 19 private byte[] img; 20 private String imgName; 21 22}
Service
1package myPortfolio.service; 2 3import java.io.IOException; 4import java.util.List; 5 6import org.springframework.beans.factory.annotation.Autowired; 7import org.springframework.stereotype.Service; 8import org.springframework.web.multipart.MultipartFile; 9 10import myPortfolio.entity.WorksEntity; 11import myPortfolio.repository.WorksRepository; 12 13@Service 14public class WorksService { 15 @Autowired 16 WorksRepository worksRepository; 17 18 19 //画像一覧を取得する 20 public List<WorksEntity> findAll(){ 21 return worksRepository.findAll(); 22 } 23 24 public void save(WorksEntity worksEntity, MultipartFile multipartfile) throws IOException { 25 try { 26 //multipartfile型をバイト型に変換 27 if(multipartfile != null && !multipartfile.isEmpty()) { 28 worksEntity.setImgName(multipartfile.getOriginalFilename()); 29 worksEntity.setImg(multipartfile.getBytes()); 30 worksRepository.save(worksEntity); 31 } 32 } catch(Exception e) { 33 e.printStackTrace(); 34 } 35 } 36} 37
Repository
1package myPortfolio.repository; 2 3import org.springframework.data.jpa.repository.JpaRepository; 4import org.springframework.stereotype.Repository; 5 6import myPortfolio.entity.WorksEntity; 7 8@Repository 9public interface WorksRepository extends JpaRepository<WorksEntity, Integer>{ 10}
Controller
1package myPortfolio.controller; 2 3import java.io.IOException; 4 5import org.springframework.beans.factory.annotation.Autowired; 6import org.springframework.stereotype.Controller; 7import org.springframework.ui.Model; 8import org.springframework.web.bind.annotation.GetMapping; 9import org.springframework.web.bind.annotation.PostMapping; 10import org.springframework.web.bind.annotation.RequestMapping; 11import org.springframework.web.bind.annotation.RequestParam; 12import org.springframework.web.multipart.MultipartFile; 13 14import myPortfolio.entity.WorksEntity; 15import myPortfolio.service.WorksService; 16 17@Controller 18@RequestMapping("MyPortfolio") 19public class SettingController { 20 @Autowired 21 WorksService worksService; 22 23 @GetMapping("/setting_page") 24 public String setting(Model model) { 25 return "setting"; 26 } 27 28 @PostMapping("/setting_page") 29 public String registWorks(@RequestParam("multipartfile") MultipartFile multipartfile, Model model) 30 throws IOException { 31 32 worksService.save(new WorksEntity(), multipartfile); 33 return "redirect:/MyPortfolio/setting_page"; 34 } 35}
HTML
1<!DOCTYPE html> 2<html xmlns:th="https://www.thymeleaf.org"> 3<head> 4 5<meta charset="UTF-8"> 6<title>Setting</title> 7</head> 8<body> 9 <h1>Setting</h1> 10 <form th:action="@{./setting_page}" method="post" enctype="multipart/form-data"> 11 <div class="works"> 12 <h2>Works</h2> 13 <div> 14 <p><input type="file" name="multipartfile"></p> 15 </div> 16 <p><input type="submit" value="画像登録"></p> 17 </div> 18 </form> 19</body> 20</html>
補足情報(FW/ツールのバージョンなど)
Java11
Eclipse
Gradle
Spring Boot
JPA
MYSQL 8.0.25
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/10/16 12:03