A simple example of Struts2 file upload and download

Source: Internet
Author: User

A simple example of Struts2 file upload and download

I have previously reposted an upload and download article. I have sorted it out by myself and verified it.

Package com.jereh.edu. entity; import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. IOException; import java. io. inputStream; import java. io. outputStream; import java. io. unsupportedEncodingException; import org. apache. struts2.ServletActionContext; import com. opensymphony. xwork2.Action; public class UpLoadAction {private String uname; // user name submitted at the front end; private File f Ile; // upload the file private String fileFileName; // the file name to be uploaded is automatically submitted at the front end without additional input private String fileContentType; /the file type of the uploaded file is automatically submitted at the front end without additional input private InputStream inputStream; // The input stream used for upload can define the local variable private String fileName; // The front-end hides the downloaded file name private String downLoadPath; // download path public String getFileName () {return fileName;} public void setFileName (String fileName) throws UnsupportedEncodingException {byte [] B = fileN Ame. getBytes ("UTF-8"); String str = new String (B, "iso-8859-1"); // re-decodes the encoding upon retrieval to solve the problem of garbled characters and not displaying Chinese characters. this. fileName = str;} public String getDownLoadPath () {return downLoadPath;} public void setDownLoadPath (String downLoadPath) {this. downLoadPath = downLoadPath;} public InputStream getInputStream () {return inputStream;} public void setInputStream (InputStream inputStream) {this. inputStream = inputStream;} public Str Ing getUname () {return uname;} public void setUname (String uname) {this. uname = uname;} public File getFile () {return file;} public void setFile (File file) {this. file = file;} public String getFileFileName () {return fileFileName;} public void setFileFileName (String fileContentName) {this. fileFileName = fileContentName;} public String getFileContentType () {return fileContentType;} public void setFileCont EntType (String fileContentType) {this. fileContentType = fileContentType;} public String upFile () throws IOException {// File Upload path; String path = ServletActionContext. getServletContext (). getRealPath ("/upload/"); // File upFile = new File (path + File. separatorChar + file. getName (); File upFile = new File (path + File. separatorChar + fileFileName); upFile. getParentFile (). mkdir (); // create the path System. out. println ("file name + +++ "+ File. getName () + "---" + file. getPath (); // the uploaded file is a temporary file, Struts2, Which is saved and imported to the desired path System. out. println ("uploaded file name +++" + fileFileName + "---" + fileContentType); inputStream = new FileInputStream (file ); // read OutputStream ops = new FileOutputStream (upFile) from a temporary file; // output to the specified file byte [] B = new byte [1024]; while (inputStream. read (B, 0, B. length ))! =-1) {ops. write (B);} ops. close (); inputStream. close (); return Action. SUCCESS;} public InputStream getDownLoad () {return ServletActionContext. getServletContext (). getResourceAsStream (downLoadPath);} public String downLoadFile () {return Action. SUCCESS ;}}
Once you have configured Struts, you will understand it.

                             
  
                    /success.jsp                
                                      
              
   attachment;filename=${fileName}            
   downLoad            
                          
 
 

 attachment;filename=${fileName}
ContentDisposition is configured. The front-end is downloaded in the pop-up download box. Otherwise, filename is the name of the downloaded file displayed on the page.

Front-end jsp form

<% @ Page language = "java" import = "java. util. * "pageEncoding =" UTF-8 "%> <% String path = request. getContextPath (); String basePath = request. getScheme () + ": //" + request. getServerName () + ":" + request. getServerPort () + path + "/"; %>              UpFile               
Download


The

enctype="multipart/form-data"

Attributes must be added and the method = "post" method is submitted for data integrity.

Jsp hides the file path and file name project. You can change it to js Code.



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.