본문 바로가기
프로그램/Vue

vue + 자바 파일다운로드

by cbwstar 2021. 10. 19.
728x90
반응형
const form = new FormData()
      form.append('filepath', 'board')
      form.append('filename', 'test.pptx')
      form.append('originFileName', '1634543241310-1897687360')

      this.$axios({
        method: 'POST',
        url: 'com/systemmng/file/downloadAxiosFiles.do',
        responseType: 'blob',
        headers: this.headerWeblog,
        data: form,
        params: {
          param1: 1,
          parma2: '한글',
          parma3: 'eng!@'
        }
      })
        .then(response => {
          const fileName = 'test.pptx'
          const data = response.data
          if (!data) {
            return
          }
          console.log(response)
          const url = window.URL.createObjectURL(new Blob([data]))
          const a = document.createElement('a')
          a.style.display = 'none'
          a.href = url
          a.setAttribute('download', fileName)
          document.body.appendChild(a)
          a.click()
          document.body.removeChild(a)
          window.URL.revokeObjectURL(url)
        })
        .catch(response => {
          throw new Error(response)
        })


/* 자바 */

/**
*
*<pre>
*@descaxios파일다운로드
*@param
*@return
*@return
*@throws
*</pre>
*/
@RequestMapping(value="/com/systemmng/file/downloadAxiosFiles.do")
publicStringdownloadAxiosFiles(HttpServletRequestrequest,HttpServletResponseresponse){

log.debug("--------------vue파일다운로드-------------");

StringgetPath=EgovStringUtil.removedFileTraversal(request.getParameter("filepath"));
StringgetName=EgovStringUtil.removedFileTraversal(request.getParameter("filename"));
StringgetOrginFileName=EgovStringUtil.removedFileTraversal(request.getParameter("originFileName"));

log.debug("getOrginFileName:"+getOrginFileName);
log.debug("getName:"+getName);
log.debug("getPath:"+getPath);

StringfilePath=EgovProperties.getProperty("Globals.fileStorePath").trim()+getPath+SP+getOrginFileName;

/*보안필터링*/
/*
filePath=filePath.replaceAll("/","");//"/"필터링
filePath=filePath.replaceAll("\\\\","");//"\"필터링
filePath=filePath.replaceAll("\\.\\.","");//".."필터링
*/

Enumeration<String>param=request.getParameterNames();

System.out.println("----------------------------");
while(param.hasMoreElements()){
Stringname=(String)param.nextElement();
System.out.println(name+":"+request.getParameter(name));
}
System.out.println("----------------------------");


/*헤더정보가져오기*/

EnumerationheaderEnum=request.getHeaderNames();
StringheaderName="";
StringheaderValue="";
while(headerEnum.hasMoreElements()){
headerName=(String)headerEnum.nextElement();
headerValue=request.getHeader(headerName);
log.debug("headerName:"+headerName+"headerValue:"+headerValue);

}

Map<String,Object>reMap=newHashMap<>();
OutputStreamos=null;
InputStreamis=null;
BufferedInputStreambis=null;
BufferedOutputStreambos=null;
ServletOutputStreamsout=null;
Filef=newFile(filePath);

try{
response.reset();
response.setCharacterEncoding("utf-8");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename="+newString(getName.getBytes("UTF-8"),"ISO8859-1"));


is=newFileInputStream(f);
if(is==null){
reMap.put("msg","파일이존재하지않습니다.");
}
sout=response.getOutputStream();

bis=newBufferedInputStream(is);
bos=newBufferedOutputStream(sout);
byte[]buff=newbyte[2048];
intbytesRead;
while(-1!=(bytesRead=bis.read(buff,0,buff.length))){
bos.write(buff,0,bytesRead);
}
bos.flush();
bos.close();
bis.close();
is.close();
sout.close();
os.close();
}catch(IOExceptione){
reMap.put("msg","처리중오류가발생하였습니다.");
}catch(Exceptione){
reMap.put("msg","처리중오류가발생하였습니다.");
}finally
{
if(bis!=null)
{
try
{
bis.close();
}catch(RuntimeExceptione){
f=null;
}
catch(Exceptione)
{
f=null;
}
}
if(bos!=null)
{
try
{
bos.close();
}catch(RuntimeExceptione){
f=null;
}
catch(Exceptione)
{
f=null;
}
}
if(is!=null)
{
try
{
is.close();
}catch(RuntimeExceptione){
f=null;
}
catch(Exceptione)
{
f=null;
}
}
if(sout!=null)
{
try
{
sout.close();
}catch(RuntimeExceptione){
f=null;
}
catch(Exceptione)
{
f=null;
}
}
if(os!=null)
{
try
{
os.close();
}catch(RuntimeExceptione){
f=null;
}
catch(Exceptione)
{
f=null;
}
}
}

//Stringstr=JsonUtil.map2Json(reMap);
Stringstr="ok";
returnstr;
}
728x90
반응형

'프로그램 > Vue' 카테고리의 다른 글

vscode Sass Compiler 설치  (0) 2021.11.27
vue Event Bus  (0) 2021.10.18
material 아이콘 url  (0) 2021.10.14
vue를 이용한 프로젝트 생성  (0) 2021.09.02
Vs Code 기본 환경 셋팅  (0) 2021.09.02

댓글



"이 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."

loading