안녕하세요.
JAVA 소스로 음성 REST API 사용시
newtonenYQbndJ2SUvi2iaS
Content-Type: application/json; charset=UTF-8
{“type”:“errorCalled”,“value”:“Error 4 Received Nack - no result”}
------newtonenYQbndJ2SUvi2iaS–
다음과 같이 에러메세지를 호출해주고 있습니다
문제해결 부탁드립니다.
REST API 요청을 구현한 JAVA 소스이며 voiceApiRequestP 의 매개변수인 String fileString은
heykakao.wmv를 file 객체로 받아와 base64로 변환하여 문자열로 변경시킨 값입니다
public String voiceApiRequestP(String fileString) throws Exception {
String receiverURL = "https://kakaoi-newtone-openapi.kakao.com/v1/recognize";
StringBuffer receiveMsg = new StringBuffer();
HttpURLConnection uc = null;
BufferedReader in = null;
try {
int errorCode = 0;
//receive servlet connect
URL servletUrl = new URL(receiverURL);
uc = (HttpURLConnection)servletUrl.openConnection();
uc.setReadTimeout(20000);
uc.setDoOutput(true);
uc.setDoInput(true);
uc.setUseCaches(false);
uc.setDefaultUseCaches(false);
uc.setRequestProperty("Transfer-Encoding","chunked"); //
uc.setRequestProperty("Content-Type", "application/octet-strean"); //
uc.setRequestProperty("X-DSS-Service", "DICTATION"); //
uc.setRequestProperty("Authorization", "KakaoAK da62bcaec5f2e24e8fd11fcbc4e50e33"); //
uc.setRequestMethod("POST");
OutputStreamWriter wr = new OutputStreamWriter(uc.getOutputStream(),"UTF-8");
wr.write(fileString.trim());
wr.flush();
wr.close();
// init
errorCode = 0;
if(uc.getResponseCode() == HttpURLConnection.HTTP_OK) {
String currLine = new String();
in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "UTF-8"));
while((currLine = in.readLine()) != null) {
receiveMsg.append(currLine).append("\r\n");
}
in.close();
}else {
errorCode = uc.getResponseCode();
System.out.println("errorCode : "+errorCode);
return receiveMsg.toString();
}
uc.disconnect();
} catch (Exception e) {
if(in != null) in.close();
if(uc != null) uc.disconnect();
// TODO: handle exception
}
return receiveMsg.toString();
}
파일 변경 소스입니다
//----------------------------------------------- S: 호출 메소드
File f = new File(“C:\fileupload\” + file.getOriginalFilename());
//file binary -> string 변환
String fileString = fileToBinary(f);
//-------------------------------------------------E : 호출 메소드
//파일 base64 변경메소드
public static String fileToBinary(File file) {
String out = new String();
FileInputStream fis = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
fis = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.out.println("Exception position : FileUtil - fileToString(File file)");
}
int len = 0;
byte[] buf = new byte[1024];
try {
while ((len = fis.read(buf)) != -1) {
baos.write(buf, 0, len);
}
byte[] fileArray = baos.toByteArray();
out = new String(base64Enc(fileArray));
fis.close();
baos.close();
} catch (IOException e) {
System.out.println("Exception position : FileUtil - fileToString(File file)");
}
return out;
}
public static byte[] base64Enc(byte[] buffer) {
return Base64.encodeBase64(buffer);
}