달력

52024  이전 다음

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

import java.io.*;
import java.net.URLEncoder;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

 

@Controller
public class Test {

 @RequestMapping("/fileView.do")
 public void fileDownLoad(HttpServletRequest request ,HttpServletResponse response) throws IOException {

  System.out.println(request.getParameter("file_id"));
  String getfile = "C:/DATA001/attachFiles/201610/Koala[20161018010740425].jpg";
  //String path = "C:/DATA001/attachFiles/201610/"; //paramMap.get("filePath"); //full경로
     //String fileName =  "Koala[20161018010740425].jpg"; //paramMap.get("fileName"); //파일명
     File f = new File("C:/DATA001/attachFiles/201610/Koala[20161018010740425].jpg");
     String path = f.getParent().toString();
     String fileName = f.getName();
     System.out.println("파일크기 ?" + f.length());
     FilenameUtils.getExtension(getfile);
     System.out.println("path : " + path);
     System.out.println("fileName : " + fileName);
     File file = new File(path+"/"+fileName);
     FileInputStream fileInputStream = null;
     ServletOutputStream servletOutputStream = null;

     try{
         String downName = null;
         String browser = request.getHeader("User-Agent");
         //파일 인코딩
         if(browser.contains("MSIE") || browser.contains("Trident") || browser.contains("Chrome")){//브라우저 확인 파일명 encode

             downName = URLEncoder.encode(fileName,"UTF-8").replaceAll("\\+", "%20");

         }else{

             downName = new String(fileName.getBytes("UTF-8"), "ISO-8859-1");

         }

         response.setHeader("Content-Disposition","attachment;filename=\"" + downName+"\"");
         response.setContentType("application/octer-stream");
         response.setHeader("Content-Transfer-Encoding", "binary;");

         fileInputStream = new FileInputStream(file);
         servletOutputStream = response.getOutputStream();

         byte b [] = new byte[1024];
         int data = 0;

         while((data=(fileInputStream.read(b, 0, b.length))) != -1){

             servletOutputStream.write(b, 0, data);

         }

         servletOutputStream.flush();//출력

     }catch (Exception e) {
         e.printStackTrace();
     }finally{
         if(servletOutputStream!=null){
             try{
                 servletOutputStream.close();
             }catch (IOException e){
                 e.printStackTrace();
             }
         }
         if(fileInputStream!=null){
             try{
                 fileInputStream.close();
             }catch (IOException e){
                 e.printStackTrace();
             }
         }
     }
 }
}

 

Posted by 한설림
|