`
quanxi40402
  • 浏览: 0 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论
收藏列表
标题 标签 来源
文件下载代码 download
public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		String url=HttpConfig.getInstance().getHost("wsp");
		String id=request.getParameter("id");
		id=StringUtil.decodeString(id);
		System.out.println("---------id:"+id);
		String reportName = new String(request.getParameter("reportName").getBytes("ISO-8859-1"),"utf-8") ;
		String start=request.getParameter("start");
		String end=request.getParameter("end");
		reportName=(reportName)+(start+"至"+end)+".doc";
		System.out.println("---------reportName:"+reportName);
		response.setContentType("application/x-msdownload");
		response.setHeader("Content-Disposition", "attachment; filename="+new String(reportName.getBytes("gb2312"), "ISO8859-1"));
		OutputStream out = response.getOutputStream();
		HttpClient client = new HttpClient();
		PostMethod postMethod = null;
		try {
			postMethod = new PostMethod(url+"/DownReportServlet?id="+id);
         	postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET,"utf-8"); 
			int i = client.executeMethod(postMethod);
			System.out.println("-=--status----->"+i);
			if (200 == i) {
				InputStream in = postMethod.getResponseBodyAsStream();
				if(in!=null)
				{
					try {
						byte[] buf = new byte[1024];
						int iret = 0;
						while ((iret = in.read(buf)) > 0)
						{
							out.write(buf, 0, iret);
						}
					} catch (Exception ex) {
//						ex.printStackTrace();

					} finally {
						in.close();
						if (out != null)
							out.flush();
					}
				}
			} 
		} catch (Exception e) {
//			e.printStackTrace();
		} finally {
			postMethod.releaseConnection();
			client.getHttpConnectionManager().closeIdleConnections(0);
		}
		return;
	}


public void downLoad(String filePath, HttpServletResponse response,
			boolean isOnLine) throws Exception {
		File f = new File(filePath);
		if (!f.exists()) {
			response.sendError(404, "File not found!");
			return;
		}
		BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
		byte[] buf = new byte[1024];
		int len = 0;

		response.reset(); // 非常重要
		if (isOnLine) { // 在线打开方式
			URL u = new URL("file:///" + filePath);
			response.setContentType(u.openConnection().getContentType());
			response.setHeader("Content-Disposition", "inline; filename="
					+ f.getName());
			// 文件名应该编码成UTF-8
		} else { // 纯下载方式
			response.setContentType("application/x-msdownload");
			response.setHeader("Content-Disposition", "attachment; filename="
					+ f.getName());
		}
		OutputStream out = response.getOutputStream();
		while ((len = br.read(buf)) > 0)
		{
			
			out.write(buf, 0, len);
		}
		br.close();
		out.close();
	}
Global site tag (gtag.js) - Google Analytics