繁体   English   中英

使用 Spring 启动和 UI 将 excel 文件内容上传到 mysql

[英]Upload excel file content to mysql using Spring boot and UI

我正在尝试在上传文件 UI 的帮助下使用 Spring 引导将 excel 文件内容上传到 mysql。 需要帮助。

但我面临着 Whitelabel 错误页面。 我已经尝试了几件事,但还没有运气。 项目视图

读取文件应用程序.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);

    }
}

ReadFileConrroller.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>
{

}

读取文件服务.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;
    }

}

应用程序属性

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

编辑:

***************************
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.

如下更新您的 controller

@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. 所以我的服务方法如下

@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
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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