springboot는 excel 파일의 업로드와 다운로드를 실현합니다
4326 단어 처음
먼저pom 파일을 다음과 같이 가져옵니다.
org.apache.poi
poi
3.16
org.apache.poi
poi-ooxml
3.16
commons-io
commons-io
2.6
이어서 쓰기 시작했습니다. 저는 페이지를 테스트로 사용했습니다. 페이지 코드는 다음과 같습니다.
Title
。。。
파일 업로드다운로드
이어서 응용 프로그램.java 이 시작 파일은 다음과 같습니다.
public static String tempPath="D://images/";
@Bean
MultipartConfigElement multipartConfigElement(){
MultipartConfigFactory factory = new MultipartConfigFactory();
factory.setLocation(tempPath);
return factory.createMultipartConfig();
}
다음은 컨트롤러의 작성입니다. 업로드와 다운로드 코드는 다음과 같습니다.
업로드
@ResponseBody
@RequestMapping("file/upup")
public TaotaoResult upup(@RequestParam("file")MultipartFile file,
HttpServletRequest request){
if(!file.isEmpty())
{
String pa="D:/images/";
path=request.getServletContext().getRealPath("");
//
String filename=file.getOriginalFilename();
File filepath=new File(path,filename);
// ,
if (!filepath.getParentFile().exists()){
filepath.getParentFile().mkdirs();
}
//
try {
file.transferTo(new File(pa+File.separator+filename));
return TaotaoResult.ok(" ");
} catch (IOException e) {
e.printStackTrace();
}
}else {
return TaotaoResult.ok(" , ");
}
return TaotaoResult.ok(" ");
}
다운로드:
@ResponseBody
@RequestMapping("/download")
public TaotaoResult downloadFile(HttpServletRequest request,HttpServletResponse response) throws UnsupportedEncodingException {
//
File scFileDir = new File("D://images");
File TrxFiles[] = scFileDir.listFiles();
System.out.println(TrxFiles[0]);
String fileName = TrxFiles[0].getName(); //
// ,
if (fileName != null) {
//
String realPath = "D://images/";
File file = new File(realPath, fileName);
// ,
if (file.exists()) {
//
response.setHeader("content-type", "application/octet-stream");
response.setContentType("application/octet-stream");
//
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
//
byte[] buffer = new byte[1024];
FileInputStream fis = null;
BufferedInputStream bis = null;
try {
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
OutputStream os = response.getOutputStream();
int i = bis.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bis.read(buffer);
}
System.out.println("Download the song successfully!");
}
catch (Exception e) {
System.out.println("Download the song failed!");
}
finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
return TaotaoResult.ok(" ");
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Vue 페이지 수동 새로 고침, 탐색 표시줄 활성화 항목을 초기 상태로 복원장면 설명: 페이지에 맨 위 네비게이션과 왼쪽 네비게이션이 존재하고 왼쪽 네비게이션과 오른쪽 내용 구역은 명명 보기를 사용하여 실현된다. 왼쪽 네비게이션의 링크를 클릭하면 오른쪽 내용 구역은 서로 다른 구성 요소의 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.