<> 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,
data structure C Parking management of language 300 million people are working hard Java Share five face to face experience ,offer Taken ! Byte three side remote ,Java+Redis+ network + database + algorithm , Easy to kill interviewers ?C/C++ The 11th Blue Bridge Cup provincial competition B Group 2 Junior college background Java Post , Who says that there is no spring for college programmers ? new iPhone I won't support it 5G Will lead to further decline in shipment Not yet Spring? Ali P8 Summative 100 Analysis of Dao interview , Let you hammer the interviewer Upright code analysis __ Principle of two wheel balance car 【C language 】 Mine clearance game details Some suggestions for novice programmers to write practical business code in confusion