【问题标题】:Read CSV File While Upload in Spring Boot在 Spring Boot 中上传时读取 CSV 文件
【发布时间】:2019-05-05 22:07:49
【问题描述】:

我在 spring boot 中有用于上传文件的代码。已经成功上传并将文件放入文件夹中。我已经从批处理服务中获得了代码。但我仍然对从上传数据读取数据 CSV 感到困惑。

我想修改。当我上传文件“特别是 csv”时,系统将在 CSV 文件中显示数据。

您能帮我吗,我如何在上传时从 CSV 获取数据?

控制器

package com.mkyong.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Controller
public class UploadController {

    //Save the uploaded file to this folder
    private static String UPLOADED_FOLDER = "D:\\spring\\video\\";

    @GetMapping("/")
    public String index() {
        return "upload";
    }

    @PostMapping("/upload") // //new annotation since 4.3
    public String singleFileUpload(@RequestParam("file") MultipartFile file,
                                   RedirectAttributes redirectAttributes) {

        if (file.isEmpty()) {
            redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
            return "redirect:uploadStatus";
        }

        try {

            // Get the file and save it somewhere
            byte[] bytes = file.getBytes();
            Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
            Files.write(path, bytes);
            redirectAttributes.addFlashAttribute("message",
                    "You successfully uploaded '" + file.getOriginalFilename() + "'");

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "redirect:/uploadStatus";
    }

    @GetMapping("/uploadStatus")
    public String uploadStatus() {
        return "uploadStatus";
    }

}

型号

package com.mkyong.model;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="article")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @Column(name="title")
    private String title;

    @Column(name="category")
    private String category;

    public long getId() {
        return id;
    }
    public void setId(long id) {
        this.id = id;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getCategory() {
        return category;
    }
    public void setCategory(String category) {
        this.category = category;
    }


}

查看

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<body>

<h1>Spring Boot file upload example</h1>

<form method="POST" action="/upload" enctype="multipart/form-data">
    <input type="file" name="file" /><br/><br/>
    <input type="submit" value="Submit" />
</form>
</body>
</html>

我在 youtube 和教程网站上进行了搜索,但找不到方法。

【问题讨论】:

    标签: java spring csv spring-boot


    【解决方案1】:

    你可以试试这个:

    byte[] bytes = file.getBytes();
    ByteArrayInputStream inputFilestream = new ByteArrayInputStream(bytes);
    BufferedReader br = new BufferedReader(new InputStreamReader(inputFilestream ));
    String line = "";
    while ((line = br.readLine()) != null) {
    System.out.println(line);
    }
    br.close();
    

    【讨论】:

      【解决方案2】:

      1.型号 class= Listing.java

      1. 资源.csv 文件存在于resources/static/listing.csv
      private void createListingFromCsvRecord() {
            
                  String path = "classpath:static/listings.csv";
                  ResultSet rs = new Csv().read(path, null, null);
                  List<Listing> list = new ArrayList<>();
                  while (rs.next()) {
                      Listing listing = new Listing();
                      listing.setListingId(Long.parseLong(rs.getString("listingId")));      
                      listing.setSiteArea(rs.getString("siteArea"));   
                      list.add(listing);
                  }
                 myrepo.saveAll(list);
      
      }
      

      【讨论】:

        猜你喜欢
        • 2020-02-23
        • 2021-08-20
        • 2021-03-01
        • 2012-01-04
        • 1970-01-01
        • 1970-01-01
        • 2022-11-11
        • 1970-01-01
        • 2017-06-18
        相关资源
        最近更新 更多