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("  ");
}

좋은 웹페이지 즐겨찾기