티스토리 뷰

728x90
바로 아래 파일 다운로드 도우미 내용을 참고해서 사용해도 
계속 깨져서 결국 보내는 쪽에서도 인코딩을 해주니까 안깨지고 잘 나온다

[보내는 JSP]
<a href="/fileDownload.jsp?file_path=<%=boardFileData[i][4]%>

      &file_name=<%=java.net.URLEncoder.encode(boardFileData[i][3], "UTF-8")%>
      &down_name=<%=java.net.URLEncoder.encode(boardFileData[i][2], "UTF-8")%>">
      다운로드
</a>



[받는 JSP] - fileDownload.jsp

<%@ page language="java" import="java.io.*" %>
<%
if(request.getParameter("down_name") == null || request.getParameter("file_name") == null) return ;

String downloadName = java.net.URLDecoder.decode(request.getParameter("down_name"), "8859_1");
String fileName = java.net.URLDecoder.decode(request.getParameter("file_name"), "8859_1");
String filePath = request.getRealPath(request.getParameter("file_path"));

File f = new File( filePath, fileName );

response.setContentType("application/octet-stream; name=" + downloadName);

if (request.getHeader("User-Agent").indexOf("MSIE 5.5") > -1){
response.setHeader("Content-Disposition", "filename=" + downloadName + ";");
}else {
response.setHeader("Content-Type", "application/octet-stream; charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode(downloadName, "UTF-8") + ";");
}
response.setHeader("Content-Transfer-Encoding", "binary");
response.setDateHeader("Expires", 0);
response.setHeader("Pragma", "no-cache");
response.setContentLength((int)f.length());
byte b[] = new byte[4 * 1024];  // 버퍼 4kb 

if(f.isFile()){
BufferedInputStream fin = new BufferedInputStream( new FileInputStream( f ) );
BufferedOutputStream outs = new BufferedOutputStream( response.getOutputStream() );

int read = 0;

try{
while((read = fin.read(b)) != -1 ){
outs.write(b, 0, read);
}
}catch(Exception e){
e.printStackTrace();
try { outs.flush(); } catch ( Exception ei ){ }
}finally{
if(fin != null) try{ fin.close(); } catch ( Exception ei ){ }
if(outs != null) try{ outs.close(); } catch ( Exception ei ){ }
}
}

return;
%>



왜 utf-8로 인코딩해서 보냈는데 8859_1로 디코딩 해야 하는지는 모르겠다
어쨌든 안깨지고 잘된다..