서브렛에서 파일을 다운로드하는 쓰기

	public String execute() throws Exception {
		super.execute();
		ServiceResponse response = getResponse();
		if (!response.isSuccess()) {
			return assemblesResponse(response);
		}
		//       
		String fileName = response.getString("__fileName__");
		if (fileName == null || "".equals(fileName)) {
			fileName = "download";
		}
		fileName = new String(fileName.getBytes(), "iso8859-1");
		//      
		byte[] bs = (byte[]) response.get("__data__");
		if (bs == null) {
			throw new Exception("         ");
		}
		writeToStream(ServletActionContext.getResponse(), bs, fileName);
		return null;
	}

	public void writeToStream(ServletResponse resp, byte[] bytes, String fileName) throws IOException {
		HttpServletResponse response = (HttpServletResponse) resp;
		response.reset();
		response.setContentType("application/octet-stream");
		response.setContentLength(bytes.length);
		response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
		ServletOutputStream ouputStream = response.getOutputStream();
		ouputStream.write(bytes, 0, bytes.length);
		ouputStream.flush();
		ouputStream.close();
	}

주로 HttpServlet Response의 서브렛 OutputStream을 통해 바이트를 직접 출력합니다.
 
 
 

좋은 웹페이지 즐겨찾기