List

월요일, 12월 14, 2015

Mybatis 환경에서 답변형 게시판 구축 - 파일 업로드/다운로드 처리 2


  • XML 처리
    • Bean download 클래스 추가
    • bnViewResolver 를추가 ???
    • 뭔지 아직 잘모르겟음

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
   <!-- file download bean 설정 -->
   <bean id="bnViewResolver" 
           class="org.springframework.web.servlet.view.BeanNameViewResolver">
      <property name="order" value="0"/>
      <!-- viewResolver우선1번째
         bean으로 등록되어 호출되는 view가 있으면 그 뷰를 호출하고,
         아니면, 2번째인 jsp View를 호출처리한다.
       -->
   </bean>    
   
     <bean id="viewResolver"
       class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="suffix" value=".jsp"/>
      <property name="order" value="1"/><!-- viewResolver우선2번째 -->
   </bean>
   
   <bean id="download" class="springboard.util.DownView"/>   
cs
  •  DownCtrl ( 다른 class )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Controller
public class DownCtrl {
   @RequestMapping("download.do")
   public ModelAndView down(RestoreVO rs){
      System.out.println("폴드명:"+rs.getFold());
      System.out.println("파일명:"+rs.getFname());
      
      File f = new File(rs.getFold(), rs.getFname());
      System.out.println("존재여부:"+f.exists());
      System.out.println("파일크기:"+f.length());
      // ModelAndView("bean이름","모델이름",파일);
      ModelAndView mav = new ModelAndView("download","downloadFile",f);
      //mav.setViewName("redirect:/upload.do");
         
      return mav;
   }
}
cs

  • DownView 단

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
32
33
34
35
    public class DownView extends AbstractView{
       public DownView(){
          setContentType("application/download;charset=utf-8;");
       }
       @Override
       protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response)
             throws Exception {
          // TODO Auto-generated method stub
          // 파일을 가져오기..controller에서 모델이름과 함께 넘겨온 파일을 로딩..
          File file = (File)model.get("downloadFile");
          
          // 서버에 있는 파일을 클라이언트에 넘겨주는 처리..
          // ==> response
          response.setContentType(getContentType());
          // 파일의 길이 설정..
          response.setContentLength((int)file.length());
          // 한글파일명을 utf방식을 encoding처리..공백은 %+ ==> " "전환
          String fname=URLEncoder.encode(file.getName(), "utf-8").replaceAll("\\+"" ");
          // 파일은 웹상에서 주로 header에서 정보를 지정해서 stream방식으로 전송.
          // 1. response.setHeader(파일관련정보);
          response.setHeader("Content-Disposition""attachment;filename=\""+fname+"\"");
          // attachment;filename="파일명"
          response.setHeader("Content-Transfer-Encoding""binary");
          // 2. response.getOutputStream();
          OutputStream out = response.getOutputStream();
          //    서버에서 전달해온 stream을 파일로 저장처리.
          FileInputStream fis=null;
          fis = new FileInputStream(file);
          FileCopyUtils.copy(fis, out);
          out.flush();
          
       }
}
cs




ㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴㄴ

댓글 없음:

댓글 쓰기