Mam pytanie bo chce aby do postów były dodawane zdjęcia zrobiłam coś takiego i nie chce mi to zadziałać może ktoś coś podpowiedzieć?
Model Post:
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "post")
private List<Comment> comments= new ArrayList<>();
@Lob
@Column(name="images")
private List<MultipartFile> images;
public List<MultipartFile> getImages() {
return images;
}
public void setImages(List<MultipartFile> images) {
this.images = images;
}
PostController:
@RequestMapping(value = "/savePost", method = RequestMethod.POST)
public ModelAndView savePost(@ModelAttribute("post") Post post,
BindingResult result, HttpServletRequest request) {
List<MultipartFile> files = post.getImages();
List<String> fileNames = new ArrayList<String>();
if (null != files && files.size() > 0)
{
for (MultipartFile multipartFile : files) {
String fileName = multipartFile.getOriginalFilename();
fileNames.add(fileName);
File imageFile = new File(request.getServletContext().getRealPath("/images"), fileName);
try
{
multipartFile.transferTo(imageFile);
} catch (IOException e)
{
e.printStackTrace();
}
}}
postService.saveOrUpdate(post);
return new ModelAndView("redirect:/posts");
}
}
NewPost .jsp
<form:form action="savePost" method="post" commandName="post" enctype="multipart/form-data">
<form:hidden path="postid"/>
<div class="form-group row">
<div class="col-xs-4">
<label for="ex3"></label>
<input class="form-control" name="autor" id="ex3" type="string" placeholder="Autor">
</div>
</div>
<div class="form-group row">
<div class="col-xs-4">
<label for="ex3"></label>
<input class="form-control" name="tytul" id="ex3" type="string" placeholder="Tytuł">
</div>
</div>
HibernateConfiguration
@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.pack.configuration" })
@PropertySource(value = { "classpath:application.properties" })
public class HibernateConfiguration {
@Autowired
private Environment environment;
@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(dataSource());
sessionFactory.setPackagesToScan(new String[] { "com.pack.model" });
sessionFactory.setHibernateProperties(hibernateProperties());
return sessionFactory;
}
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
return dataSource;
}
private Properties hibernateProperties() {
Properties properties = new Properties();
properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
return properties;
}
@Bean
@Autowired
public HibernateTransactionManager transactionManager(SessionFactory s) {
HibernateTransactionManager txManager = new HibernateTransactionManager();
txManager.setSessionFactory(s);
return txManager;
}
@Bean
public MultipartResolver multipartResolver() {
CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
multipartResolver.setMaxUploadSize(10485760); // 10MB
multipartResolver.setMaxUploadSizePerFile(1048576); // 1MB
return multipartResolver;
}
}
sql
CREATE TABLE `post` (
`postid` int(11) NOT NULL AUTO_INCREMENT,
`autor` varchar(45) NOT NULL,
`tytul` varchar(512) NOT NULL,
`opis` varchar(5000) NOT NULL,
`images` blob NOT NULL,
PRIMARY KEY (`postid`)
);