论坛首页 入门技术论坛

文件下载

浏览 1578 次
锁定老帖子 主题:文件下载
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-01-04   最后修改:2010-01-04

String name = request.getParameter("name");
			if (name == null || "".equals(name)) {
				request.setAttribute("error", "");
				RequestDispatcher requestDispatcher = request
						.getRequestDispatcher("/error.jsp");
				requestDispatcher.forward(request, response);
				return;
			}

			// 应用的Id
			String path = request.getParameter("path");
			if (path == null || "".equals(path)) {
				request.setAttribute("error", "");
				RequestDispatcher requestDispatcher = request
						.getRequestDispatcher("/error.jsp");
				requestDispatcher.forward(request, response);
				return;
			}

		
			int i = 0;
			byte[] b = new byte[1024];
			OutputStream out1 = null;
			FileInputStream fis = null;
			String path1 = props.getProperty("fileStoragePath");
			response.setContentType("application/x-download");
			String url = path1 + "/" +path;
			String fileName = name;
			fileName = URLEncoder.encode(fileName, "UTF-8");
			response.addHeader("Content-Disposition", "attachment;filename="
					+ fileName);
			File file = new File(url);
			try {
				fis = new FileInputStream(file);
				out1 = response.getOutputStream();
				while ((i = fis.read(b)) > 0) {
					out1.write(b, 0, i);
				}
			} catch (Exception e) {
								e.printStackTrace();
			} finally {
				try {
					fis.close();
					out1.flush();
					out1.close();
				} catch (Exception e) {
										e.printStackTrace();
				}
				try {

				} catch (Exception e) {
									e.printStackTrace();
				}

			}
















论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics