Struts2 file upload and download supplement question, struts2 File Upload

Source: Internet
Author: User

Struts2 file upload and download supplement question, struts2 File Upload

The previous book mentioned the upload and download of the strust2 file. The last time the file was uploaded, it was silly to store an absolute path in the database. It was helpful to test the image retrieval on the local machine, A little stunned, I think the file upload and download is not the case, but today, when I package the project into a WAR package and run it today, I cannot find any images. I am so depressed, at first, I thought it was a problem during packaging, but it was still not feasible to re-package and run it. Later I went to the database to find out that it was a problem with the upload path. The local tomcat was installed on the d disk, however, the environment where the war package runs tomcat is installed on the C disk, so the path cannot be found, so there must be a problem in the display:

When uploading a file, you need to provide the upload path. In this case, the path is an absolute path, but the relative path needs to be stored in the database:

  

// Obtain the path String root = ServletActionContext for saving the image of the project. getServletContext (). getRealPath ("/ima"); File destFile = new File (root); // create if (! DestFile. exists () {destFile. mkdirs ();} InputStream is = new FileInputStream (ima ); // write the image to the path set above OutputStream OS = new FileOutputStream (destFile + "\" + imaFileName); try {byte [] B = new byte [1024]; // The size of each write. int j = 0; while (j = is. read (B)> 0) {OS. write (B, 0, j) ;}} catch (Exception e) {e. printStackTrace ();} finally {is. close (); OS. close ();}

In the above Code, root is an absolute path. What does absolute path mean? For example, "D: \ Tomcat 6.0 \ webapps" is an absolute path. What is a relative path? Tomcat 6.0 \ webapps is a relative path. Remember that there is no diagonal line at the beginning of the relative path. When uploading files, it is a relative path, but it is saved to a relative path when stored in the database. What should I do? As follows:

 String fileUrl="ima"+"\\"+imaFileName;         sysUser.setImgUrl(fileUrl);

In this way, a relative path is obtained, and the \ in the middle is for translation \.

The relative path stored in the database is also troublesome when downloading files. At least it is troublesome for me because the relative path is found from the database during the download, you still need to restore the relative path to the absolute path. Otherwise, you will not be able to get this file because this guy does not take care of you.

Request is used before getting the file and path. getRealPath (path); but this buddy is not doing well now, and it cannot be said that it is necessary to take a rest, so the new method is:

Request. getSession (). getServletContext (). getRealPath (path );

You only need to replace the path with your relative path to find the file, and then download it is an I \ O Stream.

String path = request. getSession (). getServletContext (). getRealPath ("ima \" + imgName); File file = new File (path); // download an object as a stream. InputStream FCM = new BufferedInputStream (new FileInputStream (path); byte [] buffer = new byte [FCM. available ()]; FCM. read (buffer); FCM. close (); // clear response. reset (); // set the response Header response. setContentType ("application/x-download"); response. addHeader ("Content-Disposition", "attachment; filename =" + URLEncoder. encode (imgName, "UTF-8"); response. addHeader ("Content-Length", "" + file. length (); OutputStream toClient = response. getOutputStream (); toClient. write (buffer); toClient. flush (); toClient. close ();

Well, this is now.

 

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.