336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

안녕하세요.

출처 : https://winmargo.tistory.com/103


진행하기 위에선 DB에 파일 정보가 담겨져 있어야 합니다.


pom.xml

1
2
3
4
5
<dependency>
          <groupId>commons-fileupload</groupId>
          <artifactId>commons-fileupload</artifactId>
          <version>1.3.2</version>
        </dependency>
cs


FileController.java

1
2
3
4
5
6
7
8
9
10
11
12
13
@RequestMapping("/fileDown.do")
    public ModelAndView download(@RequestParam("filecol")String filecol){
        
        logger.info("@@@@@@@@@@@@@@@FileDown(1) START@@@@@@@@@@@@@@@");
        
        String fullPath = filecol;
         
        File file = new File(fullPath);
        
        logger.info("@@@@@@@@@@@@@@@FileDown(1) END@@@@@@@@@@@@@@@");
         
        return new ModelAndView("download""downloadFile", file);
    }
cs


servlet-context.xml

1
2
3
4
5
<bean class="org.springframework.web.servlet.view.BeanNameViewResolver">
              <property name="order" value="0"/>
    </bean>
    
    <bean id="download" class="com.java.spring.util.FileDownload"/>
cs



FileDownload.java

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
public class FileDownload extends AbstractView {
    public void Download(){
        
        setContentType("application/download; utf-8");
         
    }
 
    @Override
    protected void renderMergedOutputModel(Map<String, Object> model,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        // TODO Auto-generated method stub
        
        File file = (File)model.get("downloadFile");
        System.out.println("DownloadView --> file.getPath() : " + file.getPath());
        System.out.println("DownloadView --> file.getName() : " + file.getName());
         
        response.setContentType(getContentType());
        response.setContentLength((int)file.length());
         
        //String userAgent = request.getHeader("User-Agent");
         
        //boolean ie = userAgent.indexOf("MSIE") > -1;
         
        String fileName = null;
         
        //if(ie){
        //브라우저 정보에 따라 utf-8변경
        if(request.getHeader("User-Agent").indexOf("MSIE"> -1){
             
            fileName = URLEncoder.encode(file.getName(), "utf-8");
                         
        } else {
             
            fileName = new String(file.getName().getBytes("utf-8"));
        }// end if;
        
        response.setHeader("Content-Disposition""attachment; filename=\"" + fileName + "\";");
        response.setHeader("Content-Transfer-Encoding""binary");
        OutputStream out = response.getOutputStream();
        FileInputStream fis = null;
        
        //파일 카피 후 마무리
        try {
            fis = new FileInputStream(file);
            FileCopyUtils.copy(fis, out);
        } catch(Exception e){
            e.printStackTrace();
        }finally{
            if(fis != null){
                try{
                    fis.close();
                }catch(Exception e){}
            }
        }// try end;
        out.flush();
        
    }
}
cs


home.jsp 

해당 파일이 db로 뿌려지고 있다면..

1
2
3
4
5
function fileDown(file){
            location.href = "http://localhost:8080/spring/fileDown.do?filecol="+encodeURI(file);
        }
 
<td onclick="fileDown('${list.FILECOL }')">${list.FILECOL }</td>
cs


감사합니다.


'SPRING' 카테고리의 다른 글

SPRING PROCEDURE 사용하기 1  (0) 2019.04.01
SPIRNG EXCEL UPLOAD 하기  (1) 2019.04.01
Spring FileUpload 하기  (0) 2019.03.28
Spring Excel down하기  (0) 2019.03.28
SPRING JSTL 이용해서 Spring 값 뿌리기  (0) 2019.03.27

+ Recent posts