springboot 외부 정적 자원 파일 처리

3418 단어 springspringboot
spring boot 의 외부 자원 파일 에 대한 처 리 는 주로 2 부분 으로 나 뉘 어 저장 하고 취 합 니 다. 공식 파일 을 보고 블 로 그 를 보고 구 덩이 를 밟 은 후에 드디어 해결 되 었 습 니 다. 이 기록 입 니 다.
저장 은 간단 하지만 위 챗 임시 소 재 를 얻 고 저장 하 는 방법 을 붙 여 놓 았 다.
/**
     * @           
     * @param filePath          
     * @param method     ,  POST GET
     * @param url      
     * @return
     */

    public static String saveUrlAs(String url,String filePath,String method){
        //          
        File file=new File(filePath);
        //         
        if (!file.exists())
        {
            //        ,         
            file.mkdirs();
        }
        FileOutputStream fileOut = null;
        HttpURLConnection conn = null;
        InputStream inputStream = null;
        String savePath = null ;
        try
        {
            //     
            URL httpUrl=new URL(url);
            conn=(HttpURLConnection) httpUrl.openConnection();
            // Post      ,  get  
            conn.setRequestMethod(method);
            conn.setDoInput(true);
            conn.setDoOutput(true);
            // post        
            conn.setUseCaches(false);
            //       
            conn.connect();
            //       
            inputStream=conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(inputStream);
            //              /  
            if (!filePath.endsWith("/")) {

                filePath += "/";

            }
            String filePathDir =  DateUtil.getStringAllDate();
            //     (                     )
            savePath = filePath+filePathDir+".png";
            fileOut = new FileOutputStream(savePath);
            BufferedOutputStream bos = new BufferedOutputStream(fileOut);

            byte[] buf = new byte[4096];
            int length = bis.read(buf);
            //    
            while(length != -1)
            {
                bos.write(buf, 0, length);
                length = bis.read(buf);
            }
            bos.close();
            bis.close();
            conn.disconnect();
        } catch (Exception e)
        {
            e.printStackTrace();
            logger.error(">>>>>>>>>>>>>>>>             [{}]",e.getMessage());
        }

        return savePath;

    }

2. 취, spring boot 에 익숙 하지 않 아서 이 위 에 구 덩이 를 밟 았 습 니 다.
 정적 자원 에 대한 springboot 공식 문서 의 설명 을 살 펴 보 겠 습 니 다.
https://docs.spring.io/spring-boot/docs/1.5.19.RELEASE/reference/htmlsingle/#boot-features-spring-mvc-static-content
이 두 설정 에 주로 사 용 됩 니 다.
spring.mvc.static-path-pattern=/resources/**    //url 접근 경로 spring. resources. static - locations = 설정                      //대응 하 는 파일 경로 설정
프로젝트 루트 디 렉 터 리 와 같은 등급 의 static 폴 더 에 정적 자원 을 저장 하고 싶 어서 설정 하 였 습 니 다.
spring.resources.static-locations=  static/
spring.mvc.static-path-pattern=/static/** 
이후 접근 파일 404
그 후에 인터넷 에서 찾 아 보 니 이 편 을 보 았 다.https://blog.csdn.net/kilua_way/article/details/54601195
발견 하 다.
spring.resources.static-locations= file:xxx 
file: + 경로 라 는 설정 방법 을 사용 하고 시도 해 보 았 습 니 다. 이렇게 되 었 습 니 다.
spring:
  resources:
    static-locations: file:${my.config.static-location}
my:
  config:
    static-location: /static/

파일 접근 이 성 공 했 습 니 다!
그래서 실제 외부 파일 은 file: 설정 이 필요 합 니 다. static - locations 기본 값 은 클래스 경로 의 파일 에 접근 합 니 다.

좋은 웹페이지 즐겨찾기