Springboot jar package run in Get resource file

Source: Internet
Author: User

1. This evening, a program was written that, based on a small spring boot site, found that using FileUtils.class.getResource (path) to get the resource files in the jar package was not successful and its path was strange

file:/users/lonecloud/documents/ideacode/git/export/target/export-0.0.1-snapshot.jar!/boot-inf/classes!/temp/ Temp.xls

After reviewing the data, and viewing the resource files in the jar package found! There are also classes! such words, Super strange. A compromise method was found in the back to solve the problem

public static File Getjarresourcefile (string path, String fileName) {//Get Container resource resolver resourcepatternresolver res        Olver = new Pathmatchingresourcepatternresolver ();            try {//Get all matching files resource[] resources = resolver.getresources (path + fileName); if (Resources.length > 1) {//get the file stream because in the jar file, you cannot get the file directly through the file resource path, but you can get the file stream in the jar package Inputstrea                M stream = Resources[0].getinputstream ();                if (logger.isinfoenabled ()) {logger.info ("read file stream [" + Stream + "]");                } String Targetfilepath = System.getproperty ("user.home") + File.separator + resources[0].getfilename ();                if (logger.isinfoenabled ()) {Logger.info ("drop position [" + Targetfilepath + "]");                } File Ttffile = new file (Targetfilepath);                Org.apache.commons.io.FileUtils.copyInputStreamToFile (stream, ttffile); return ttffile;           }} catch (IOException e) {if (logger.iswarnenabled ()) {Logger.warn ("read file stream failed , writing to the local library failed!            "+ e); }} throw new RuntimeException ("File not Found"); }

  

Springboot jar package run in Get resource file

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.