징그럽게 내 발목을 잡던 스트럿츠 다운로드 해결... 거즘....2주간 붙잡고 있었던 것 같다.
한글 파일은 다운이 안되던 문제를 겨우 해결...
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class downloadAction extends Action {
@Override
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {
try {
String filePath = "D:\\workspace\\DKIEX\\WebContent\\upload";
String fileName = (String) request.getParameter( "fileName" );
String originFileName = (String) request.getParameter( "originFileName" );
fileName = new String(fileName.getBytes("8859_1"), "euc-kr");
String orgFileName = java.net.URLEncoder.encode(new String(originFileName.getBytes("8859_1"), "euc-kr"),"UTF-8");
if (request.getHeader( "User-Agent" ).indexOf( "MSIE 5.5" ) > -1){
response.setHeader( "Content-Type", "doesn/matter;" );
response.setHeader( "Content-Disposition", "attachment; filename=" + orgFileName + ";" );
}
else{
response.setHeader( "Content-Type", "application/octet-stream;" );
response.setHeader( "Content-Disposition", "attachment; filename=" + orgFileName + ";" );
}
File file = new File( filePath +"\\"+new String(originFileName.getBytes("8859_1"),"euc-kr"));
byte b[] = new byte[(int) file.length()]; // file.length()
response.setHeader( "Content-Transfer-Encoding", "binary;" );
response.setHeader( "Content-Length", "" + file.length() );
response.setHeader( "Pragma", "no-cache;" );
response.setHeader( "Expires", "-1;" );
BufferedInputStream fin = new BufferedInputStream( new FileInputStream( file ) );
BufferedOutputStream outs = new BufferedOutputStream( response.getOutputStream() );
int read = 0;
while ((read = fin.read( b )) != -1){
outs.write( b, 0, read );
}
outs.flush();
outs.close();
fin.close();
}catch (Exception e){
e.printStackTrace();
System.out.println("Exception");
}
return null; // 리턴정보가 필요 없기 때문임
// 여기에 리턴정보를 맵핑시키면 화면상에는 에러가 뜨지 않지만 로그상에는 오류로 기록되기때문에
// null 처리 함
}
}