springboot 기반 파일 업로드
4433 단어 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 테스트 업로드파일 을 볼 수 있 고 로 컬 에 성공 적 으로 업로드 되 었 습 니 다!
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Kotlin Springboot -- 파트 14 사용 사례 REST로 전환하여 POST로 JSON으로 전환前回 前回 前回 記事 の は は で で で で で で を 使っ 使っ 使っ て て て て て リクエスト を を 受け取り 、 reqeustbody で 、 その リクエスト の ボディ ボディ を を 受け取り 、 関数 内部 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.