Spring 프로젝트 가 시 작 될 때 데이터베이스 에서 파일 경 로 를 가 져 오고 로 컬 로 다운로드 합 니 다.
2583 단어 javaWeb
@Component
public class InitAction {
@Autowired
private FilePathService filePathService;
/**
* , ,
*/
@PostConstruct
public void downFile() {
Properties props = System.getProperties(); //
String osName = props.getProperty("os.name"); //
if (osName.matches(".*Mac.*")) {
String basepath = "/tmp/";
// ,
List pathLists = new LinkedList();
String filePath1 = basepath + "file";
String filePath2 = basepath + "file2";
pathLists.add(filePath1);
pathLists.add(filePath2);
for (String string : pathLists) {
File file = new File(string);
if (!file.exists()) {
file.mkdirs();
}
}
//
List lists = this.filePathService.findAllData();
List downUrlLists = new LinkedList();
String filePath = basepath + "file"; //
for (FilePath filePath : lists) {
downUrlLists.add(filePath.getDown_url());
}
//
File file = new File(filePath);
//
if (!file.exists()) {
// ,
file.mkdirs();
}
FileOutputStream fileOut = null;
HttpURLConnection conn = null;
InputStream inputStream = null;
try {
for (int i = 0; i < downUrlLists.size(); i++) {
String downUrl = downUrlLists.get(i);
String fileName = downUrl.substring(downUrl.lastIndexOf("/")); //
//
URL httpUrl = new URL(downUrl);
conn = (HttpURLConnection) httpUrl.openConnection();
// Post , get
conn.setRequestMethod("GET");
conn.setDoInput(true);
conn.setDoOutput(true);
// post
conn.setUseCaches(false);
//
conn.connect();
//
inputStream = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(inputStream);
// /
if (!filePath.endsWith("/")) {
filePath += "/";
}
// ( )
fileOut = new FileOutputStream(filePath + fileName);
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();
}
}
}
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
학습 노트: 일반 공장텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.