Java Struts (File Download), Java Struts

Source: Internet
Author: User

Java Struts (File Download), Java Struts

1. jump from the registration successful page to the user details page (jump to UserListAction)

 

2. UserListAction calls service to obtain the user list and transmits the data to UserList. jsp. UserList. jsp displays the data.

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> user List 

 

3. There is a jump label in the UserList. This label can be used to download the user's profile picture. When you click this page, it will jump to DownloadFileAction. This Action will be downloaded.

Public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) {FileInputStream FS = null; OutputStream OS = null; String fileName = request. getParameter ("filename"); UsersService usersService = new UsersService (); Users user = usersService. getUser (fileName); // download the object // 1. first obtain the absolute path of the downloaded file String filePath = this. getServlet (). getServletContext (). getRealPa Th ("/file"); String userPhoto = user. getPhoto (); response. setContentType ("text/html; charset = UTF-8"); // if the file has Chinese characters, perform Url encoding. // set a header, tell the browser that there is a file to download 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 {FCM = new FileInputStream (fileAllPath); OS = response. getOutputStream (); while (len = Fi. read (buff ))! =-1) {OS. write (buff, 0, len) ;}} catch (Exception e) {e. printStackTrace ();} finally {try {FCM. 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) first obtain the user name transmitted from the previous page, and then obtain the user data from the database based on the user name.

2). Get the absolute path of the downloaded file, and set the encoding format and header for response. If the file has Chinese characters, url encoding is required.

3) create an input stream to read files. Obtain an output stream from response for output

4). Output the file through the input/output stream

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.