[JAVA] How to load a Spring upload file and view its contents

Since the necessary classes cannot be imported unless the library at stater-web is installed in build.gradle. Described first. Refresh Gradle when you can write.

build.gradle


dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	compile('org.springframework.boot:spring-boot-starter-thymeleaf')
	compile('org.springframework.boot:spring-boot-starter-data-jpa')
	compile ('org.springframework.boot:spring-boot-starter-web')
}

Add an instance of ** MultipartFile ** class to the argument of the post method of the controller. This is the file that was selected from HTML with \ and posted with \ Become an instance.

ModelAndViewController.java


	@RequestMapping(value="/",method=RequestMethod.POST)
	public ModelAndView post(
			@RequestParam("upload_file")MultipartFile uploadFile,
			ModelAndView mav) 
	{
		mav.setViewName("index");
		mav.addObject("file_contents", fileContents(uploadFile));
		mav.addObject("recordSet", list);
		return mav;
	}

A place to convert the contents of the read file into text format and extract it. When reading from ** MultiPartFile **, it is a byte stream, so to convert it to a text format such as a csv file, convert it to ** Reader **-> ** BufferedReader **. (I don't know if it is necessary to insert a Reader.) In the end, in this code, the contents of the file are contained in the buf variable in text format, so all you have to do is read it in the same way as a local application such as Swing.

ModelAndViewController.java


private List<String> fileContents(MultipartFile uploadFile) {
		List<String> lines = new ArrayList<String>();
		String line = null;
		try {
			InputStream stream = uploadFile.getInputStream();			
			Reader reader = new InputStreamReader(stream);
			BufferedReader buf= new BufferedReader(reader);
			while((line = buf.readLine()) != null) {
				lines.add(line);
			}
			line = buf.readLine();
			
		} catch (IOException e) {
			line = "Can't read contents.";
			lines.add(line);
			e.printStackTrace();
		}
		return lines;
	}

Uploading is not possible unless ** enctype = "multipart / form-data" ** is described in the html form tag.

index.html


<!--Implementation of file upload button-->
	<form method="post" action="/" enctype="multipart/form-data">
		<input type="submit" value="Upload file">
		<input type="file" name="upload_file">	
	</form>
	<p th:text="${file_name}"></p>
	<table>
<!--Below is the result display-->
	<tr th:each="line:${file_contents}">
		<td th:text=${line}></td>
	</tr>
	</table>

Recommended Posts

How to load a Spring upload file and view its contents
How to test file upload screen in Spring + Selenium
[Rails] How to load JavaScript in a specific view
How to load UI created by Xib file into View
I want to get a list of the contents of a zip file and its uncompressed size
How to realize huge file upload with TERASOLUNA 5.x (= Spring MVC)
How to read a file and treat it as standard input
How to realize huge file upload with Rest Template of Spring
[Xcode] How to add a README.md file
How to split Spring Boot message file
How to achieve file upload with Feign
How to record JFR (Java Flight Recorder) and output a dump file
How to create a server executable JAR and WAR with Spring gradle
Scraping and writing specific elements to a file
How to add a classpath in Spring Boot
How to bind to property file in Spring Boot
[Spring Boot] How to refer to the property file
How to ZIP a JAVA CSV file and manage it in a Byte array
[Spring sample code included] How to create a form and how to get multiple records
How to create an Excel form using a template file with Spring MVC
I tried to implement file upload with Spring MVC
How to jump from Eclipse Java to a SQL file
How to write a unit test for Spring Boot 2
How to create a Spring Boot project in IntelliJ
[Spring Boot] How to create a project (for beginners)
How to download a file (Servlet, HTML, Apache, Tomcat)
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
21 Load the script from a file and execute it
[Rails] How to edit and customize devise view and controller
How to request a CSV file as JSON with jMeter
[Ruby] How to split each GraphQL query into a file
How to use OpenCV 4 on Android and view camera live view
How to call and use API in Java (Spring Boot)
[Ruby] How to retrieve the contents of a double hash
How to develop and register a Sota app in Java
How to change the contents of the jar file without decompressing
How to join a table without using DBFlute and sql
How to create and launch a Dockerfile for Payara Micro
How to leave a comment
File upload with Spring Boot
How to insert a video
How to create a method
[Swift5] How to create a .gitignore file and the code that should be written by default
How to create a jar file or war file using the jar command
How to make a hinadan for a Spring Boot project using SPRING INITIALIZR
How to make a jar file with no dependencies in Maven
How to set and use profile in annotation-based Configuration in Spring framework
[Rails] How to upload images to AWS S3 using Carrierwave and fog-aws
How to make CsrfRequestDataValueProcessor and original RequestDataValueProcessor coexist on Spring Boot
How to open a script file from Ubuntu with VS code
[Rails] How to upload images to AWS S3 using refile and refile-s3
[Reading impression] "How to learn Rails, how to write a book, and how to teach"
[chown] How to change the owner of a file or directory
[Swift5] How to communicate from ViewController to Model and pass a value
How to check for the contents of a java fixed-length string
How to display characters entered in Spring Boot on a browser and reference links [Introduction to Spring Boot / For beginners]
How to use Lombok in Spring
How to use StringBurrer and Arrays.toString.
How to unit test Spring AOP
How to add columns to a table