http路径jsp直接下载文件

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="java.io.*"%>
<%@ page import="java.net.*"%>
<%
 try {
  String filePath = request.getParameter("filepath");  
  if (filePath != null) {
   URL url = new URL(filePath);
   URLConnection uc = url.openConnection();
   //http://adboximages.wemo.cn/2011-03-11/11550000012ea4664e24001d.xls
   String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
   byte by[] = new byte[2048];
   response.reset();
   response.setContentType("application/octet-stream; charset=UTF-8");
   response.setHeader("content-disposition", "attachment; filename=" + fileName);
   InputStream in = uc.getInputStream();// 打开的连接读取的输入流   
   ServletOutputStream sos = response.getOutputStream();
   int n;
   while ((n = in.read(by)) != -1) {
    sos.write(by, 0, n);
   }
   in.close();
   sos.flush();
   sos.close();
   
   out.clear();
   out = pageContext.pushBody();
  } 
 } catch (Exception e) {
  System.out.println("error." + e.getMessage());
 }
%>

猜你喜欢

转载自blog.csdn.net/langliu/article/details/6248299
今日推荐