response 파일 다운로드

1399 단어 OSExcel
오늘 프로젝트에서 다른 사람이 Response로 쓴 다운로드 기능을 보고 코드를 복사해 볼게요.
 
public String execute() throws Exception {
		ApArticleAttachEntity file = baseDataService.getArticleAttach(attachId);
		if (file == null)
			return NONE;
		String fileName = (file.getAttachName() == null
				|| "".equals(file.getAttachName())) ? "unknownFileName.tmp" : file
				.getAttachName();
		String suffixName = fileName.substring(fileName.lastIndexOf(".")+1, fileName.length());
		if("doc".equalsIgnoreCase(suffixName)){
			response.setContentType("application/msword");
		}else if("xls".equalsIgnoreCase(suffixName)){
			response.setContentType("application/vnd.ms-excel");
		}else if("pdf".equalsIgnoreCase(suffixName)){
			response.setContentType("application/pdf");
		}else{
			response.setContentType("application/download");
		}
		response.setHeader("Content-Disposition", "attachment; filename=\""
				+ new String(fileName.getBytes("GBK"), "iso8859-1") + "\"");

		OutputStream os = response.getOutputStream();
		os.write(file.getAttachContent());
		os.flush();
		os.close();

		return NONE;
	}

 
 
 
 

좋은 웹페이지 즐겨찾기