Spring boot file Upload (multiple file uploads) Learn spring boot from scratch

Source: Internet
Author: User
Tags bind file size file upload getmessage join prepare xmlns

"Video & Communication Platform"

Àspringboot Video

Http://study.163.com/course/introduction.htm?courseId=1004329008&utm_campaign=commission&utm_source= 400000000155061&utm_medium=share

Àspringcloud Video

Http://study.163.com/course/introduction.htm?courseId=1004638001&utm_campaign=commission&utm_source= 400000000155061&utm_medium=share

àspring Boot Source

Https://gitee.com/happyangellxq520/spring-boot

àspring Boot AC Platform

http://412887952-qq-com.iteye.com/blog/2321532




file uploads are mainly divided into the following steps :

(1) New Maven Java project;

(2) to join the corresponding dependence in pom.xml;

(3) Create a new form page (use thymeleaf here);

(4) Prepare controller;

(5) Testing;

(6) To make some restrictions on the uploaded documents;

(7) Multi-File upload implementation

(1) New Maven Java Project

Create a new name called Spring-boot-fileuploadmaven Java project;

(2) to join the corresponding dependence in pom.xml;

Add the appropriate MAVEN dependencies, as explained in the following examples:

<!--

Springboot Parent Node Dependency,

After introducing this, the relevant introduction does not need to add the version configuration,

Springboot will automatically select the most appropriate version to add.

-

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>1.3.3.RELEASE</version>

</parent>

<dependencies>

<!--Spring Boot Web support: Mvc,aop ...-

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!--thmleaf template dependency. -

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-thymeleaf</artifactId>

</dependency>

</dependencies>

<build>

<plugins>

<!--compiled version; -

<plugin>

<artifactId>maven-compiler-plugin</artifactId>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>

</plugin>

</plugins>

</build>

(3) Create a new form page (use thymeleaf here)

Create a new templates in Src/main/resouces (if you've seen a blogger before, you should know that templates is the path to the Spring Boot store template file) and create a new file.html under templates:

<! DOCTYPE html>

xmlns:sec= "Http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" >

<title>hello world!</title>

<body>

<form method= "POST" enctype= "Multipart/form-data" action= "/upload" >

<p> file: <input type= "file" name= "file"/></p>

<p><input type= "Submit" value= "Upload"/></p>

</form>

</body>

(4) Prepare controller;

To write a controller for testing, here are the main implementation of two methods: one is to provide access to the/file path, and the other is to provide post upload/upload method, in particular, see the code implementation:

Package com.kfit;

import Java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import Java.io.FileOutputStream;

import java.io.IOException;

importOrg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importOrg.springframework.web.bind.annotation.RequestParam;

importorg.springframework.web.bind.annotation.ResponseBody;

importorg.springframework.web.multipart.MultipartFile;

@Controller

Publicclass Fileuploadcontroller {

Access path is: Http://127.0.0.1:8080/file

@RequestMapping ("/file")

Public String file () {

return"/file";

}

/**

* File upload specific implementation method;

*@param file

*@return

*/

@RequestMapping ("/upload")

@ResponseBody

Public Stringhandlefileupload (@RequestParam ("file") multipartfilefile) {

if (!file.isempty ()) {

Try {

/*

* After the execution of this code, the image is uploaded to the path of the project;

* Everyone's own thoughts, if we want to upload the picture to d:/files people can achieve it.

And so on

* Here is a simple example, please refer to yourself, into the actual may require you to do some thinking, such as:

* 1, file path;

* 2, file name;

* 3, file format;

* 4, the file size limit;

*/

Bufferedoutputstreamout =newbufferedoutputstream ( new FileOutputStream ( File.getoriginalfilename ())));

Out.write (File.getbytes ());

Out.flush ();

Out.close ();

}catch(Filenotfoundexceptione) {

E.printstacktrace ();

return "Upload failed," +e.getmessage ();

}catch (Ioexceptione) {

E.printstacktrace ();

return "Upload failed," +e.getmessage ();

&nbs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.