`

struts1 文件下载

    博客分类:
  • J2EE
OS 
阅读更多
//实现文件下载
	public ActionForward downLoad(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		 String fname  = request.getParameter("fname") ;
		 String path = request.getSession().getServletContext().getRealPath("/upload")+"/"+fname;
		 InputStream is  = new FileInputStream(new File(path)) ;
		 OutputStream os  = response.getOutputStream() ;
		 response.setHeader("Content-disposition", "attachment;filename="+URLEncoder.encode(path,"utf-8"));
		 int len = 0 ;
		 byte[] b = new byte[1024] ;
		 
		 while((len=is.read(b))!=-1){
			 os.write(b,0,len) ;
		 }
		 
		 os.flush() ;
		 os.close() ;
		 is.close() ;
		 
		 System.out.println("下载完毕 ");
		 
		return null;
	}



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics