<> be based on springboot File upload for

<> The first step : stay vo Create upload front-end response class under package
import com.alibaba.druid.filter.AutoLoad; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.NoArgsConstructor; /** * Upload response parameters * @param <E>
*/ // Here are lombok Plug in comments @Data @AllArgsConstructor @NoArgsConstructor public class
Resp<E> { // Return status code as 200 403 private String code; // Return information private String Msg;
// It can also be defined as Object body It means any kind of meaning private E body;// Template type /** * The way to success * @param body
* @param <E> * @return */ public static<E> Resp<E> success(E body){ return new
Resp<E>("200"," Upload successful !",body); } /** * Method for upload failure * @param code * @param msg *
@param <E> * @return */ public static<E> Resp<E> fail(String code,String msg){
return new Resp<E>(code,msg,null); } }
<> Step two : stay controller The layer receives the files uploaded by the front end
import com.qf.springboot_ssm_day02.service.UploadService; import com.qf.
springboot_ssm_day02.vo.Resp; import org.springframework.beans.factory.
annotation.Autowired; import org.springframework.stereotype.Controller; import
org.springframework.web.bind.annotation.RequestMapping; import org.
springframework.web.bind.annotation.RequestMethod; import org.springframework.
web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation
.ResponseBody; import org.springframework.web.multipart.MultipartFile;
@Controller public class uploadController { @Autowired private UploadService
uploadService; @RequestMapping(value = "upload",method = RequestMethod.POST)
@ResponseBody // The return type is based on the custom return type Not necessarily like me public Resp<String> upload(@RequestParam(
"file")MultipartFile file){ return uploadService.upload(file); } }
Step three : stay servcie Build under package upload Interface and its implementation class to handle business
import com.qf.springboot_ssm_day02.vo.Resp; import org.springframework.web.
multipart.MultipartFile; /** * Upload business class */ public interface UploadService { // Upload interface
Resp<String > upload(MultipartFile file); } import com.qf.springboot_ssm_day02.
service.UploadService; import com.qf.springboot_ssm_day02.vo.Resp; import org.
springframework.stereotype.Service; import org.springframework.web.multipart.
MultipartFile; import java.io.File; /** * Upload business implementation class */ @Service public class
UploadServiceImpl implements UploadService { @Override public Resp<String>
upload(MultipartFile file) { // Judge whether the uploaded file is empty if (file.isEmpty()){ return Resp.fail
("400"," The file is empty !"); } // The file is not empty // Get the original file name ( File name from the front end ) With extended name // There is a problem with the original file name String
OriginalFilename=file.getOriginalFilename(); // according to time stamp + Extended name = Server file name //
Determine the server file name ( After the character operation plus the extension name ) String fileName= System.currentTimeMillis()+"."+
OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1); // Console view server file name
System.out.println(fileName); // Determine file storage location // File save path Notice the double backslash at the end Escape character all double backslashes String
filePath="F:\\Test\\"; // Destination file path ( Files actually created on hard disk ) File dest=new File(filePath+
fileName); // judge dest Does the parent directory of exist if(dest.getParentFile().exists()) dest.getParentFile
().mkdirs(); try { // The files from the front end are copied locally file.transferTo(dest); }catch (Exception e){
e.printStackTrace(); return Resp.fail("500",OriginalFilename+" Upload failed !"); } // Upload successful
Returns the name of the file passed through the front end return Resp.success(fileName); } }
<> Step 4 :postman Test upload

Can see the file and successfully uploaded to the local !

©2019-2020 Toolsou All rights reserved,
【 Explain in detail 】 Interview must ask :SpringBoot Automatic configuration principle 【 Hyper detail 】Java Realization of student information management system 2020 The 11th National Blue Bridge Cup Python group JavaScript note ( five )---- Branch statement MYSQL database SQL Sentence practice experiment EXERCISES. 1 SIMPLE COMMANDSMySQL Basics Commonly used sentence ( Add / delete / modify query )C++ of string of compare usage vue limit input Only positive numbers can be entered C Language of a Gobang game implementation Programmer refused due to low salary offer,HR become shame , Netizens instantly blew up ..