SpringBoot 項目搭建 打包 運行

    搭建一個很是簡單的springboot項目,只有圖片訪問的功能。springboot項目其實很靈活,能夠打成jar包運行,靜態資源的路徑也能夠隨意配置,也能夠打成war包配合tomcat運行。前端

    1. 新建maven項目,初始架構爲:java

    2. 引進springboot,補充pom文件:web

​
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.yue</groupId>
    <artifactId>springboot-picture</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    
</project>

​

    3. resources下新建文件application.propertiesspring

server.port=8080
server.tomcat.basedir=./tomcatTemp

設置tomcat臨時目錄的緣由是,我不想讓太多垃圾堆積在c盤。tomcat啓動時,會默認在C盤的一個位置生成臨時目錄,存儲大量的臨時文件。消耗系統物理內存,我但願把這些消耗掌握在更明顯的可控範圍內。apache

    4. java包下新建com.yue,yue包下新建Application.class,做爲springboot的啓動類tomcat

@SpringBootApplication(scanBasePackages={"com.yue"})
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class);
    }
}
scanBasePackages={"com.yue"}  這一句是配置啓動類的所在包路徑

    此時,最基本的springboot框架已經搭建,運行Application類就能夠啓動了springboot

控制檯輸出架構

2018-06-21 16:12:40.729  INFO 11780 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2018-06-21 16:12:40.738  INFO 11780 --- [           main] com.yue.Application                      : Started Application in 3.78 seconds (JVM running for 4.346)

說明啓動成功。app

    5. 增長圖片訪問功能,這個圖片訪問不是把圖片放到靜態資源路徑下,而後用 http://ip:port/圖片名 的方式訪問,而是直接在前端請求過來時,將本地的圖片轉化成二進制流寫入response,響應給前端頁面。框架

    新建相關類

@Service
public class ImageService {

    private final String IMAGE_PATH="F:/150094541085.jpg";

    public byte[] getImage() throws IOException {
        FileInputStream fileInputStream = null;
        byte[] data =null;
        try {
            fileInputStream = new FileInputStream(IMAGE_PATH);
            //適合小容量文件
            data = new byte[fileInputStream.available()];
            fileInputStream.read(data);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return data;
    }
}
@Controller
public class ImageController {

    @Autowired
    private ImageService imageService;

    @GetMapping("/getImage")
    public void getImage(HttpServletResponse response) throws IOException {
        response.setHeader("Pragma", "no-cache");
        response.setContentType("image/jpeg");
        byte[] image = imageService.getImage();
        response.getOutputStream().write(image);
        response.flushBuffer();
    }
}

此時,啓動項目,輸入 http://localhost:8081/getImage 便能訪問本地圖片了

 

    6. 可是這樣子還沒辦法打包運行,須要繼續修改pom文件,添加如下代碼

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>1.5.10.RELEASE</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

這樣子,用maven命令 clean  、package 打包就好了。

 

運行jar包的命令  

java -Dfile.encoding=UTF-8 -jar ./xxx.jar
相關文章
相關標籤/搜索