搭建一个很是简单的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