Java 在Spring Boot中上载时读取CSV文件
我在spring boot中有上传文件的代码。已成功上载并将文件放入文件夹中。我已经从运行批处理服务中获得了代码。但我仍然混淆了从上传数据读取CSV数据 我要修改。当我上传文件“特别是csv”时,系统将在csv文件中显示数据 你能帮我吗,我上传时如何从CSV获取数据 控制器Java 在Spring Boot中上载时读取CSV文件,java,spring,csv,spring-boot,Java,Spring,Csv,Spring Boot,我在spring boot中有上传文件的代码。已成功上载并将文件放入文件夹中。我已经从运行批处理服务中获得了代码。但我仍然混淆了从上传数据读取CSV数据 我要修改。当我上传文件“特别是csv”时,系统将在csv文件中显示数据 你能帮我吗,我上传时如何从CSV获取数据 控制器 package com.mkyong.controller; import org.springframework.stereotype.Controller; import org.springframework.web
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>
Spring启动文件上载示例
我在youtube和教程网站上被搜索到,但我找不到方法 您可以尝试以下方法:
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();
1.Model class=
Listing.java
.csv
文件位于resources/static/listing.csv
private void createListingFromCsvRecord(){
String path=“classpath:static/listings.csv”;
ResultSet rs=new Csv().read(路径,null,null);
列表=新的ArrayList();
while(rs.next()){
Listing=新列表();
setListingId(Long.parseLong(rs.getString(“listingId”));
listing.setSiteArea(rs.getString(“siteArea”);
列表。添加(列表);
}
myrepo.saveAll(列表);
}
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);
}