1. 程式人生 > >Java Struts(文件下載)

Java Struts(文件下載)

mage header int nbsp thead 輸出流 enc lose 註冊

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).通過輸入輸出流,將文件進行輸出

Java Struts(文件下載)