- public ActionForward down(ActionMapping mapping, ActionForm form,
- HttpServletRequest request, HttpServletResponse response)
- throws Exception {
- //用户登录 省略
- //得到传过来的参数
- //因为是用get方式传过来的,所以要编码
- String name = new String(request.getParameter("name").getBytes("iso-8859-1"),"utf-8");
- //服务器上的真实路径
- String path = request.getSession().getServletContext().getRealPath("/upload");
- //从服务器上下载,用new FileInputStream
- //下载用response,因为是要下载到客户端,所以要用response.getOutputStream()
- InputStream iStream = new FileInputStream(path+File.separator+name);
- OutputStream oStream = response.getOutputStream();
- //这句话是设置response的头信息
- //可以弹出一个对话框用来下载
- response.addHeader("content-disposition", "attachment;filename="+java.net.URLEncoder.encode(name, "utf-8"));
- byte[] b = new byte[1024];//一次读多少字节的数据从iStream中
- int size = iStream.read(b);
- while (size > 0) {
- oStream.write(b,0,size);//这里一次写多少
- size = iStream.read(b);
- }
- iStream.close();
- oStream.close();
- return null;
- }
struts1文件下载
最新推荐文章于 2014-03-15 00:50:05 发布