简体   繁体   中英

Upload excel file content to mysql using Spring boot and UI

I'm trying to upload excel file content to mysql using Spring boot with the help of upload file UI. Need help with that.

But i'm facing Whitelabel Error Page. I've tried couple of things but no luck yet. Project View

ReadFileApplication.java

package com.springboot.file.parsefiles;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
@EnableAutoConfiguration
@ComponentScan(basePackages= {"com.springboot.file.parsefiles.controller"})
@SpringBootApplication
@Component

public class ReadFileApplication {

    public static void main(String[] args) {
        SpringApplication.run(ReadFileApplication.class, args);

    }
}

ReadFileConrtroller.java

package com.springboot.file.parsefiles.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import com.springboot.file.parsefiles.service.ReadFileService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.springboot.file.parsefiles.model.User;

@RestController
public class ReadFileController 
{
    @Autowired private ReadFileService readFileService;

    @GetMapping(value="/ ")
    public String home(Model model)
    {
        model.addAttribute("user", new User());
        List<User> users = ReadFileService.findAll();
        model.addAttribute("users", users);
        return "view/users";
    }


    @PostMapping(value="/fileupload")
    public String uploadFile(@ModelAttribute User user, RedirectAttributes redirectAttributes)
    {
        @SuppressWarnings("unused")
        boolean isFlag = readFileService.saveDataFromUploadFile(user.getFile());
        return "redirect:/";
    }
}

ReadFileRepository.java

package com.springboot.file.parsefiles.repository;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import com.springboot.file.parsefiles.model.User;

@Repository
public interface ReadFileRepository extends CrudRepository<User, Long>
{

}

ReadFileService.java

package com.springboot.file.parsefiles.service;

import java.util.List;
import org.springframework.web.multipart.MultipartFile;
import com.springboot.file.parsefiles.model.User;

public interface ReadFileService 
{
    List<User> findAll = null;
    static List<User> findAll() 
    {
        return null;
    }
    boolean saveDataFromUploadFile(MultipartFile file);

}

ReadFileServiceImpl.java

package com.springboot.file.parsefiles.service;

import java.util.List;

import com.springboot.file.parsefiles.service.ReadFileService;
import org.apache.commons.io.FilenameUtils;
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 com.springboot.file.parsefiles.model.User;
import com.springboot.file.parsefiles.repository.ReadFileRepository;

@Service
@Transactional
public class ReadFileServiceImpl implements ReadFileService
{
    @Autowired private ReadFileRepository readFileRepository;


    public List<User> findAll()
    {
        return (List<User>) readFileRepository.findAll();
    }


    @Override
    public boolean saveDataFromUploadFile(MultipartFile file) {
        @SuppressWarnings("unused")
        boolean isFlag = false;
        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
        if(extension.equalsIgnoreCase("json"))
        {
            isFlag = realDataFromJson(file);
        }else if(extension.equalsIgnoreCase("csv"))
        {
            isFlag = realDataFromCsv(file);
        }
        return false;
    }


    private boolean realDataFromCsv(MultipartFile file) 
    {
        return false;
    }


    private boolean realDataFromJson(MultipartFile file) 
    {
        return false;
    }

}

Applicatiopn.properties

spring.datasource.url = jdbc:mysql://localhost:3306/sampledatabase
spring.datasource.username = root
spring.datasource.password = root
spring.datasource.testWhileIdle = true
spring.datasource.validationQuery = SELECT 1

spring.jpa.show-sql = true

spring.jpa.hibernate.ddl-auto = update

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect

spring.servlet.multipart.max-file-size=100MB
spring.servlet.multipart.max-request-size=500MB

Edit:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field readFileService in com.springboot.file.parsefiles.controller.ReadFileController required a bean of type 'com.springboot.file.parsefiles.service.ReadFileService' that could not be found.

The injection point has the following annotations:
    - @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.springboot.file.parsefiles.service.ReadFileService' in your configuration.

Update your controller as below

@PostMapping(value="/fileupload")
    public String uploadFile(@RequestParam MultipartFile file, RedirectAttributes redirectAttributes)
    {
        @SuppressWarnings("unused")
        boolean isFlag = readFileService.saveDataFromUploadFile(file);
        return "redirect:/";
    }

Next i used apache-poi library to convert excel file to list of model object in service class. So my service method is as below

@Autowired
private IPoiji poijiImpl;

@Override
    public boolean saveDataFromUploadFile(MultipartFile file) {
        List<UserDTO> uploadedUserList = = poijiImpl.fromExcel(file.getOriginalFilename(), file.getInputStream(),UserDTO.class);
        // here i can call repository methods to save data in database
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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