121059457 发表于 2013-2-6 10:00:40

文件下载

String name = request.getParameter("name");if (name == null || "".equals(name)) {request.setAttribute("error", "");RequestDispatcher requestDispatcher = request.getRequestDispatcher("/error.jsp");requestDispatcher.forward(request, response);return;}// 应用的IdString 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;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();}}
页: [1]
查看完整版本: 文件下载