Java Struts (file download)

Source: Internet
Author: User

1. Jump to the User details page from the Registration Success page (jump to Userlistaction)

2.UserListAction calls the service to get a list of users and delivers the data to userlist.jsp, userlist.jsp the data for presentation

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;}

  

 

3. In the userlist there is a jump tag, this tag can be downloaded user picture, when clicked on this page, jump to downloadfileaction, this action to download

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);//download file//1. Get the absolute path to the download file first string filePath = This.getservlet (). Getservletcontext (). Getrealpath ("/file"); String Userphoto = User.getphoto (); Response.setcontenttype ("Text/html;charset=utf-8");//If the file has Chinese, URL encoding is required// Set a header that tells the browser to have the 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 {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). Obtain the user name transmitted from the previous page, and then retrieve the user's data from the database based on the user name.

2). Get the absolute path to the downloaded file, response set the encoding format and header; If the file has Chinese, URL encoding is required

3). Create an input stream to read the file; get an output stream from response for output

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

Java Struts (file download)

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.