Q.CAST QSP API 호출 파일 다운로드 수정

This commit is contained in:
LEEYONGJAE 2024-10-17 16:35:49 +09:00
parent ffc6be8752
commit 1275b8a8c0
2 changed files with 88 additions and 8 deletions

View File

@ -4,6 +4,8 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpMethod;
@ -146,16 +148,21 @@ public class BoardService {
String url = QSP_API_URL + "/api/file/downloadFile" + "?encodeFileNo=" + encodeFileNo;
/* [2]. QSP API CALL -> Response */
String strResponse = interfaceQsp.callApi(HttpMethod.GET, url, null);
Map<String, String> result = new HashMap<String, String>();
byte[] byteResponse = interfaceQsp.callApi(HttpMethod.GET, url, null, result);
if (!"".equals(strResponse)) {
if (byteResponse != null && byteResponse.length > 0) {
try {
/* [3]. API 응답 파일 처리 */
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;");
response.setContentType(
!"".equals(result.get("type")) && result.get("type") != null ? result.get("type")
: "application/octet-stream");
response.setHeader("Content-Disposition",
!"".equals(result.get("disposition")) && result.get("disposition") != null
? result.get("disposition")
: "attachment;");
InputStream inputStream =
new ByteArrayInputStream(strResponse.getBytes(StandardCharsets.UTF_8));
InputStream inputStream = new ByteArrayInputStream(byteResponse);
StreamUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
@ -169,8 +176,7 @@ public class BoardService {
} else {
// [msg] File does not exist.
throw new QcastException(ErrorCode.NOT_FOUND,
message.getMessage("common.message.file.download.exists"));
throw new QcastException(null, message.getMessage("common.message.file.download.exists"));
}
}
}

View File

@ -1,10 +1,13 @@
package com.interplug.qcast.util;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Map;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.databind.ObjectMapper;
@ -79,4 +82,75 @@ public class InterfaceQsp {
return sb != null ? sb.toString() : null;
}
/**
* API Call - byte return, file down 사용
*
* @param apiPath
* @param requestObject
* @return String
* @throws Exception
*/
public byte[] callApi(HttpMethod httpMethod, String apiPath, Object requestObject,
Map<String, String> result) throws Exception {
URL url = null;
HttpURLConnection con = null;
OutputStreamWriter osw = null;
BufferedReader br = null;
byte[] bt = null;
try {
url = new URL(apiPath);
con = (HttpURLConnection) url.openConnection();
con.setConnectTimeout(30000); // 서버에 연결되는 Timeout 시간 설정
con.setReadTimeout(30000); // InputStream 읽어 오는 Timeout 시간 설정
con.setRequestMethod(httpMethod.toString());
con.setRequestProperty("Content-Type", "application/json");
con.setDoInput(true);
con.setUseCaches(false);
con.setDefaultUseCaches(false);
if (HttpMethod.GET.equals(httpMethod)) {
con.setDoOutput(false);
} else {
con.setDoOutput(true); // POST 데이터를 OutputStream으로 넘겨 주겠다는 설정
if (requestObject != null) {
ObjectMapper om = new ObjectMapper();
osw = new OutputStreamWriter(con.getOutputStream());
osw.write(om.writeValueAsString(requestObject)); // json 형식의 message 전달
osw.flush();
}
}
if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
// response 헤더 결과 셋팅
result.put("type", con.getHeaderField("Content-Type"));
result.put("disposition", con.getHeaderField("Content-Disposition"));
InputStream inputStream = con.getInputStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
bt = outputStream.toByteArray(); // 파일 데이터 반환
}
} catch (Exception e) {
throw e;
} finally {
try {
if (osw != null)
osw.close();
if (br != null)
br.close();
} catch (Exception e) {
throw e;
}
}
return bt != null ? bt : null;
}
}