반응형

파일 다운로드 창을 하나 만들어서 그 창에 있는 파일 여러개를 버튼 하나로 동시에 다운로드할 수 있는 기능을

만들려고 했다.


하지만 실패.. 구글링에 이것저것 찾아봤는데 http 뭐...response 이런거 통신할때는 파일 하나밖에 전송 안된다고  얼핏 본것같다. (아님말고..)

만약에 그렇게 구현하려면 아마 setTime으로 시간을 조정해서 해야할듯?


아무튼..  잘안되서 포기하고 파일들을 zip파일로 묶어 바로 전송하도록 했다.


참고로 ZipOutputStream은 한글지원이 안된다고 한다..



 

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
import java.util.zip.ZipOutputStream;
 
int bufferSize = 1024 * 2;
String ouputName = "test";
            
ZipOutputStream zos = null;
            
try {
                
    if (request.getHeader("User-Agent").indexOf("MSIE 5.5"> -1) {
        response.setHeader("Content-Disposition""filename=" ouputName + ".zip" + ";");
    } else {
        response.setHeader("Content-Disposition""attachment; filename=" + ouputName + ".zip" + ";");
    }
    response.setHeader("Content-Transfer-Encoding""binary");
    
                
    OutputStream os = response.getOutputStream();
    zos = new ZipOutputStream(os); // ZipOutputStream
    zos.setLevel(8); // 압축 레벨 - 최대 압축률은 9, 디폴트 8
    BufferedInputStream bis = null;
                
    
    String[] filePaths = {"filePath1","filePath2","filePath3"};
    String[] fileNames = {"fileName1","fileName2","fileName3"};
    int    i = 0;
    for(String filePath : filePaths){
        File sourceFile = new File(filePath);
                        
        bis = new BufferedInputStream(new FileInputStream(sourceFile));
        ZipEntry zentry = new ZipEntry(fileNames[i]);
        zentry.setTime(sourceFile.lastModified());
        zos.putNextEntry(zentry);
        
        byte[] buffer = new byte[bufferSize];
        int cnt = 0;
        while ((cnt = bis.read(buffer, 0, bufferSize)) != -1) {
            zos.write(buffer, 0, cnt);
        }
        zos.closeEntry();
 
        i++;
    }
               
    zos.close();
    bis.close();
                
                
catch(Exception e){
    e.printStackTrace();
}
 
 
cs



출처 : https://m.blog.naver.com/PostView.nhn?blogId=92211hyeon&logNo=220337059932&proxyReferer=https%3A%2F%2Fwww.google.co.kr%2F

반응형

+ Recent posts