zoukankan      html  css  js  c++  java
  • Java Struts(文件下载)

    1.从注册成功页面跳转至用户详情页面(跳转至UserListAction)

    2.UserListAction调用service获得用户列表,并将这些数据传送到UserList.jsp中,UserList.jsp将这些数据进行展示

    	public ActionForward execute(ActionMapping mapping, ActionForm form,
    			HttpServletRequest request, HttpServletResponse response) {
    		
    		UsersService service=new UsersService();
    		ArrayList<Users> userList = service.getUserList();
    		request.setAttribute("userList", userList);
    		return mapping.findForward("showuser");
    	}
    

      

    public ArrayList<Users> getUserList()
    	{
    		String sql="select * from _users";
    		List<Object[]> al = SqlHelper.executeQuery(sql, null);
    		ArrayList<Users> userList=new ArrayList<Users>();
    		Users user=new Users();
    		for(Object[] obj:al)
    		{
    			user.setUsername((String) obj[0]);
    			user.setPhoto((String) obj[1]);
    			user.setPhoto2((String) obj[2]);
    			userList.add(user);
    		}
    		return userList;
    	}
    

      

     <h1>用户列表</h1>
       		<c:forEach items="${userList}" var="user">
       		${user.username} <img src="/StrutsFileUpAndDown/file/${user.photo}" width=50px/><a href="/StrutsFileUpAndDown/downloadFile.do?filename=${user.username}">点击下载</a><br/>
       		  </c:forEach>
    

     

    3.在UserList中存在一个跳转标签,这个标签可以进行下载用户头像,当点击这个页面后,跳转至DownloadFileAction,这个Action进行下载操作

    	public ActionForward execute(ActionMapping mapping, ActionForm form,
    			HttpServletRequest request, HttpServletResponse response) {
    		FileInputStream fis=null;
    		OutputStream os=null;
    		String fileName = request.getParameter("filename");
    		UsersService usersService=new UsersService();
    		Users user = usersService.getUser(fileName);
    			
    		//下载文件
    		//1.先获取到下载文件的绝对路径
    		String filePath = this.getServlet().getServletContext().getRealPath("/file");
    		String userPhoto = user.getPhoto();
    		
    		response.setContentType("text/html;charset=utf-8");
    		//如果文件具有中文,则需要进行Url编码
    		//设置一个头,告诉浏览器有文件要下载
    		try {
    			response.setHeader("Content-Disposition","attachement; filename="+java.net.URLEncoder.encode(userPhoto,"utf-8"));
    		} catch (UnsupportedEncodingException e1) {
    			e1.printStackTrace();
    		}
    		String fileAllPath=filePath+"\"+userPhoto;
    		
    		byte []buff=new byte[1024];
    		int len=0;
    		try {
    			fis=new FileInputStream(fileAllPath);
    			os=response.getOutputStream();
    			while((len=fis.read(buff))!=-1)
    			{
    				os.write(buff,0,len);
    			}
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    		finally{
    			try {
    				fis.close();
    				os.close();
    			} catch (IOException e) {
    				e.printStackTrace();
    			}
    		}
    		return mapping.findForward("goback");
    	}
    

     

    	public Users getUser(String username)
    	{
    		String sql="select * from _users where username=?";
    		String[] parameter={username};
    		ArrayList<Object[]> al = SqlHelper.executeQuery(sql, parameter);
    		
    		Object[] objects = al.get(0);
    		
    		Users user=new Users();
    		user.setUsername((String) objects[0]);
    		user.setPhoto((String) objects[1]);
    		user.setPhoto2((String) objects[2]);
    		
    		return user;
    	}
    

      

     

      1).先获取上个页面传输过来的用户名,然后再根据这个用户名从数据库中获取这个用户的相关数据。

      2).获取下载文件的绝对路径,response设置编码格式和头;如果文件具有中文,则需要进行url编码

      3).创建一个输入流,用于读取文件;从response中获取一个输出流,用于输出

      4).通过输入输出流,将文件进行输出

  • 相关阅读:
    POJ 1703 Find them, Catch them
    POJ 2236 Wireless Network
    POJ 2010 Moo University
    POJ 2184 Cow Exhibition
    POJ 3280 Cheapest Palindrome
    POJ 3009 Curling 2.0
    POJ 3669 Meteor Shower
    POJ 2718 Smallest Difference
    POJ 3187 Backward Digit Sums
    POJ 3050 Hopscotch
  • 原文地址:https://www.cnblogs.com/callyblog/p/7440260.html
Copyright © 2011-2022 走看看