<> 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,
c Language implementation 《 Student management system 》 No hole is the future of mobile phone ? There are still many problems to be solved Junior , A little sense , Just for mutual encouragement How to use Vue Used in Echarts Visual Library The 11th Blue Bridge Cup Java The second provincial competition B Group part Python- be based on OpenCV Contour fill for flooding algorithm hole filling 【C#】 The realization of student achievement information management system List Common interview questions in the collection and simple ideas China Mobile Science Popularization : Why do mobile networks call “ Cellular mobile network ”【Golang Basic series 10 】Go language On conditional sentences if