IDE : 이클립스
DB : mysql
SQL : ibatis
OS : centos 7
server : tomcat 6
jdk : 1.6
프레임워크 : 전자정부프레임워크 3.8
파일을 복사하는 방법은 나는 2가지를 안다
- InputStream/ OutputStream 이용
- Channel 이용
나는 2번을 포스팅 할 것이다
준비물
- Controller (비즈니스 로직 Class)
- properties (없어도됨 , 단지 예제에 들어가 있다)
1. 로직 만들기
public String pdfViewer() {
System.out.println("::: PDF 뷰어 Part1");
//properties 관련 사용해서는 아래에 페이지 링크를 걸었으니 참고해주세요~
// properties에 선언한 파일 경로 사용
Properties properties = new Properties();
String propFileName = "test.properties"; //가져올 properties 파일이름
InputStream IS = getClass().getClassLoader().getResourceAsStream(propFileName);
try {
properties.load(IS);
} catch (IOException e) {
e.printStackTrace();
System.out.println(e);
}
//2. 원본 파일이름과 경로 출력, 그리고 확장자 출력
String ori_file_name = "ori.pdf"; //원본 file_name
String ori_file_path = "home/file"; //원본 file_path 경로
String ori_ext = ori_file_name.substring(ori_file_name.lastIndexOf(".") + 1); //파일 확장자 출력
String ori_file = ori_file_path + "/" + ori_file_name; //원본파일
System.out.println("::: ori_file_path =>" +ori_file_path);
System.out.println("::: ori_file_name =>" + ori_file_name);
System.out.println("::: ori_ext =>" + ori_ext);
System.out.println("::: ori_file => "+ori_file);
System.out.println("-----------------------------------------------------------------------");
//3. 복사할 파일 이름및 위치 설정
String copy_file_name = "copy_file.pdf"; //복사할 파일 이름 (고유 시퀀스번호 + 확장자)
String copy_file_path = properties.getProperty("filepath.pdf"); //복사할 파일 경로 불러오기
String copy_file = copy_file_path + "/" + copy_file_name;
System.out.println("::: copy_file_name =>"+copy_file_name);
System.out.println("::: copy_file_path =>"+copy_file_path);
System.out.println("::: copy_file =>"+copy_file);
System.out.println("------------------------------------------------------------------------");
//4. 복사 시작
try {
//원본파일 File담기
File oriFile = new File(ori_file);
FileInputStream fis = new FileInputStream(oriFile);
//복사될 File 담기
File copyFile = new File(copy_file);
FileOutputStream fos = new FileOutputStream(copyFile);
//Channel을 이용하여
FileChannel fcin = fis.getChannel();
FileChannel fcout = fos.getChannel();
long size = fcin.size();
//transferTo로 원본파일을 복사할파일 위치로 전송
fcin.transferTo(0, size, fcout);
// fcout.transferFrom(fcin, 0, fcin.size());
//닫기 해줘야함
fcout.close();
fcin.close();
System.out.println("PDF 복사 성공");
return SUCCESS;
}catch(Exception e){
e.printStackTrace();
System.out.println(e);
return ERROR;
}
}
참고하기 좋은 페이지
2025.02.08 - [Java] - [java] properties 파일 사용해보기
[java] properties 파일 사용해보기
프로젝트 이름 : NIDE : 이클립스DB : mysqlSQL : ibatisOS : centos 7server : tomcat 6jdk : 1.6프레임워크 : 전자정부프레임워크 3.8(1) 파일 업로드나 다운로드를할때 경로를 기억할 수 없어서 미리 properties라는
antoroong.tistory.com
'언어 > Java' 카테고리의 다른 글
[java]logback 설정하기 part2 (0) | 2025.02.08 |
---|---|
[java] logback 설정하기 (0) | 2025.02.08 |
[java] properties 파일 사용해보기 (0) | 2025.02.08 |
[java]파일 다운로드 1 (0) | 2025.02.07 |
[java] 이클립스 개발환경 구축(Apache-Tomcat 9.x) (0) | 2025.02.07 |