springboot 기반 파일 업로드

본 논문 의 사례 는 springboot 기반 파일 업로드 의 구체 적 인 코드 를 공유 하여 참고 하 시기 바 랍 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
첫 번 째 단계:vo 패키지 에서 업로드 전단 응답 클래스 만 들 기

import com.alibaba.druid.filter.AutoLoad;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 *       
 * @param <E>
 */
//   lombok    
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Resp<E> {
 //        200 403
 private String code;
 //    
 private String Msg;
 //      Object body           
 private E body;//    

 /**
  *       
  * @param body
  * @param <E>
  * @return
  */
 public static<E> Resp<E> success(E body){
  return new Resp<E>("200","    !",body);
 }

 /**
  *         
  * @param code
  * @param msg
  * @param <E>
  * @return
  */
 public static<E> Resp<E> fail(String code,String msg){
  return new Resp<E>(code,msg,null);
 }
}
두 번 째 단계:controller 층 에서 전단 에 올 린 파일 을 받 습 니 다.

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
 //                       
 public Resp<String> upload(@RequestParam("file")MultipartFile file){

  return uploadService.upload(file);
 }

}
세 번 째 단계:servcie 패키지 에서 upload 인터페이스 구축 및 클래스 처리 업무 실현

import com.qf.springboot_ssm_day02.vo.Resp;
import org.springframework.web.multipart.MultipartFile;
/**
*     
*/
public interface UploadService {
 //    
 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;

/**
 *        
 */
@Service
public class UploadServiceImpl implements UploadService {

 @Override
 public Resp<String> upload(MultipartFile file) {
  //           
  if (file.isEmpty()){
   return Resp.fail("400","    !");
  }
  //        
  //       (         )      
  //           
  String OriginalFilename=file.getOriginalFilename();
  //      +   =      
  //         (           )
  String fileName= System.currentTimeMillis()+"."+OriginalFilename.substring(OriginalFilename.lastIndexOf(".")+1);
  //           
  System.out.println(fileName);
  //        
  //                             
  String filePath="F:\\Test\\";
  //       (          )
  File dest=new File(filePath+fileName);
  //  dest        
  if(dest.getParentFile().exists())
   dest.getParentFile().mkdirs();

  try {
    //             
    file.transferTo(dest);
   }catch (Exception e){
    e.printStackTrace();
    return Resp.fail("500",OriginalFilename+"    !");

   }
   //                
   return Resp.success(fileName);


 }
}
STEP 4:postman 테스트 업로드


파일 을 볼 수 있 고 로 컬 에 성공 적 으로 업로드 되 었 습 니 다!
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기