Common JAVA2011. 5. 23. 21:43


<%@ page contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.io.*"%>
<%
 OutputStream outStream = null;
 FileInputStream fileStream = null;
 try {

  request.setCharacterEncoding("euc-kr");
  String fileName = request.getParameter("file");

  //또는 아래 사용
  //String filePath = request.getRealPath("/")+"uploadFile/";
  //request.getParameter("filepath");
  //추후 파라미터로 데이터를 받을 예정
  String filePath = "D:\\FileManage\\uploads\\";  

   response.setContentType("application/x-msdownload");
   //위 세팅으로 안될 경우에 사용.
   //response.setContentType("application/octet-stream");

   String convName1 =
   java.net.URLEncoder.encode(fileName,"UTF-8");

   response.setHeader("Content-Disposition", "attachment;filename=" + convName1 + ";");

  //위 세팅으로 안될 경우에 사용.
  //response.setHeader("Content-Disposition","attachment;fileName=\""+Convfilename+"\";");

   String convName2 = fileName;
   File file = new File(filePath+convName2);
 
   byte[] byteStream = new byte[(int)file.length()];
   fileStream = new FileInputStream(file);
   int i=0;
   int j=0;
   while( (i=fileStream.read()) != -1 ){
    byteStream[j] = (byte)i;
    j++;
   }
  out.clear(); //out--> jsp자체 객체
  out=pageContext.pushBody(); //out--> jsp자체 객체
  outStream = response.getOutputStream();
  outStream.write(byteStream);


 }catch(Exception e) {
  System.out.println(e);
 }finally {
  if(fileStream != null) fileStream.close();
  if(outStream != null) outStream.close();
 }

 


%>

Posted by penguindori