1) java runtime.exec(command); 명령어 문자열
java에서 runtime.exec(command); 형태로 터미널 커맨드를 실행할 수 있는데 이때 터미널이나 CLI에서 해당 명령어를 인식하는데 문제가 있는 것 같다.
처음에는 자바 정규식 표현을 찾아보고 정확하게 내가 curl을 terminal에서 실행할 때와 똑같은 명령어를 전달하려고 아래와 같이 String command를 선언했는데 이렇게 할 경우 terminal에서 인식하지 못하는 듯하다.
(※ 자바 정규식 표현은 중요한 부분이니까 따로 한번 정리하자)
String command= new String("curl -X POST -H \"Content-Type: multipart/form-data\" -H \"Accept-Encoding: gzip\" -H \"Content-Disposition: attachment\" -H \"Content-Transfer-Encoding: binary\" -H \"Content-Type: text/csv\" -F \"file=/Users/inmobi/Documents/workspaceEclipse/final_A1_aos_b.csv.gz\" \"http://advertiser-content.inmobiapis.com/tpce/v1/upload/usersegment\"");
검색중에 https://stackoverflow.com/questions/30301212/unable-to-execute-curl-command-through-java-runtime-getruntime-exec 글을 찾았고 보아하니까 터미널에서 명령어를 인식하는 문제가 있기 때문에 String [] 로 명령어를 쪼개서 입력하라는 거였다.
It seems like the data shell/console is interpreting/changing characters. For example the following line:
-H "Content-Type: application/json"
... seems to be getting interpreted as three different arguments:
-H Content-Type:
and application
and /json
by the shell/console.
Try breaking the command string down into an array of components using the format:
exec(String[] cmdarray)
That way it will be clear to the shell/console which arguments are grouped together.
이렇게 시도했지만 여전히 안되었고 스페이스나 자바정규식 표현이 문제가 아닌 것 같았다.
간단한 GET 방식으로 여러차례 시도해보면 알 수 있었는데 옵션을 줄 때 스페이스나 "와 같은 특수문자를 자바 escape문자로 알려주지 않아도 runtime.exec의 자바 process에서 잘 인식하는 듯하다. 결론적으로 지정해준 명령어 문자배열은 다음과 같다.
String [] command=new String[]{"curl", "-POST", "-H", "Content-Type: multipart/form-data", "-H", "Accept-Encoding: gzip", "-H", "Content-Disposition: attachment", "-H", "Content-Transfer-Encoding: binary", "-H", "Content-Type: text/csv", "-F", "file=/Users/inmobi/Documents/workspaceEclipse/final_A1_aos_b1.csv.gz", "http://advertiser-content.inmobiapis.com/tpce/v1/upload/usersegment"};
'Programming > segment upload project' 카테고리의 다른 글
read and parse CSV file in Java (0) | 2017.06.05 |
---|