SpringBoot 결합commons-fileupload 파일 업로드
2963 단어 SpringBoot
commons-fileupload
commons-fileupload
1.3.1
commons-io
commons-io
2.4
세부 코드:
//
@Value("${filePath}")
private String filePath;
@RequestMapping(value ="/fileUp", method = RequestMethod.POST)
@ResponseBody
public void fileUploadOne(@RequestParam("file") MultipartFile file,@RequestParam("fileType") String fileType) {
// +
String fileName = file.getOriginalFilename();
//
String fname = fileName.substring(0, fileName.lastIndexOf("."));
//
String format = fileName.substring(fileName.lastIndexOf(".") + 1);
// ( )
long MS = System.currentTimeMillis();
String timeMS = String.valueOf(MS);
// +
String videoName = fname + "_" + timeMS + "." + format;
String filelocalPath = "";
char pathChar = upfilePath.charAt(upfilePath.length() - 1);
Date date = new Date();
String dateOne = new SimpleDateFormat("yyyy/MM/dd/").format(date);
if (pathChar == '/') {
filelocalPath = upfilePath + dateOne;
} else {
filelocalPath = upfilePath + "/" + dateOne;
}
File f = new File(filelocalPath);
if (!f.exists())
f.mkdirs();
if (!file.isEmpty()) {
try {
FileOutputStream fos = new FileOutputStream(filelocalPath + videoName);
InputStream in = file.getInputStream();
//InputStream in = request.getInputStream();
byte[] bytes = new byte[1024];
int len = 0;
while ((len = in.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fos.close();
in.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
업로드 파일의 크기를 Application 시작 클래스에서 설정해야 한다는 점도 잊지 마십시오.
//
@Value("${tempfilePath}")
private String tempfilePath;
@Bean
public MultipartConfigElement multipartConfigElement() {
MultipartConfigFactory factory = new MultipartConfigFactory();
//
factory.setMaxFileSize("100MB");
///
factory.setMaxRequestSize("1024MB");
// ,
File f = new File(tempfilePath);
if (!f.exists())
f.mkdirs();
factory.setLocation(tempfilePath);
return factory.createMultipartConfig();
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
【Java・SpringBoot・Thymeleaf】 에러 메세지를 구현(SpringBoot 어플리케이션 실천편 3)로그인하여 사용자 목록을 표시하는 응용 프로그램을 만들고, Spring에서의 개발에 대해 공부하겠습니다 🌟 마지막 데이터 바인딩에 계속 바인딩 실패 시 오류 메시지를 구현합니다. 마지막 기사🌟 src/main/res...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.