용테크
[JAVA] URL호출을 이용한 File Download 본문
화면단에서 a태그를 만들어 Download Rule을 호출하는 방식으로 File을 다운로드하는 방법을 만들어 기록해놓는다.
Javascript
fileDownload : function(fileID,fileName,filePath){
var obj = document.createElement("a");
if(fileName.includes('%')){
fileName = fileName.replace(/%/gi, '');
}
if(fileName.includes('#')){
fileName = fileName.replace(/#/gi, escape('#'));
}
if(window.location.href.includes('#')){
obj.href = window.location.href.slice(0,-1)+"do?r=downloadRule&file_id="+fileID+"&file_name="+fileName+"&file_path="+filePath;
}else{
obj.href = window.location.href+"do?r=downloadRule&file_id="+fileID+"&file_name="+fileName+"&file_path="+filePath;
}
obj.click();
}
Java
public class downloadRule extends DatasetRule{
@Override
public void doEvent() throws Throwable {
IMessage msg = this.getMessage().get();
HttpSession session = msg.getSession();
// http://localhost:10040/do?r=downloadRule&mes_doc_id=aa&file_name=bb&userId=cc&token=dd
String file_id = session.getParameter("file_id");
String file_name = session.getParameter("file_name");
String file_path = session.getParameter("file_path");
String userId = session.getParameter("userId");
String token = session.getParameter("token");
String decFileName = URLDecoder.decode(file_name, "UTF-8");
HttpServletResponse response = session.getResponse();
String[] filePathConfirm = file_path.split("/");
String fc = filePathConfirm[filePathConfirm.length-2];
String mime = "application/octet-stream";
File file = new File(file_path+"//"+file_id);
InputStream is = null;
String decFileName2 = URLEncoder.encode(file_name, "UTF-8");
decFileName2 = decFileName2.replaceAll("\\+", "%20");
response.setContentType( "application/download; UTF-8" );
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition", String.format("inline; filename=\"%s\"", decFileName2));
response.setHeader("Content-Type", "application/octet-stream");
response.setHeader("Content-Transfer-Encoding", "binary;");
response.setHeader("Pragma", "no-cache;");
response.setHeader("Expires", "-1;");
BufferedInputStream fin = null;
BufferedOutputStream outs = null;
try {
is = new FileInputStream(file);
IOUtils.copy(is, response.getOutputStream());
msg.getParameter().set("U$EVENT_IGNOREREPLY", true);
} catch (Exception e) {
Log.error(FileDownloadToUrlForBMP.class, "", e);
e.printStackTrace();
} finally {
if(is !=null) {
is.close();
}
}
}
}
'JAVA > Etc' 카테고리의 다른 글
[JAVA] AES256 Cipher (0) | 2020.07.30 |
---|
Comments