springboot 외부 정적 자원 파일 처리
3418 단어 springspringboot
저장 은 간단 하지만 위 챗 임시 소 재 를 얻 고 저장 하 는 방법 을 붙 여 놓 았 다.
/**
* @
* @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 기본 값 은 클래스 경로 의 파일 에 접근 합 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
thymeleaf로 HTML 페이지를 동적으로 만듭니다 (spring + gradle)지난번에는 에서 화면에 HTML을 표시했습니다. 이번에는 화면을 동적으로 움직여보고 싶기 때문에 입력한 문자를 화면에 표시시키고 싶습니다. 초보자의 비망록이므로 이상한 점 등 있으면 지적 받을 수 있으면 기쁩니다! ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.