springboot实现图片上传

      Spring Boot是由Pivotal团队供给的全新框架,其设计目标是用来简化新Spring应用的初始搭建以及开辟过程。该框架利用了特心猿意马的体例来进行设置装备摆设,从而使开辟人员不再需要界说样板化的设置装备摆设。经由过程这种体例,Spring Boot致力于在蓬勃成长的快速应用开辟范畴(rapid application development)当作为带领者。

东西/原料

  • 电脑
  • intellij IDEA

方式/步调

  1. 1

    第一步:搭建springboot开辟情况。

    1、file --》new --》 project.. 打开建立选择项目类型框

    2、选择建立springboot类型的项目--》进入项目建立框 “upload”项目名称不克不及大写会报错。

    3、选择依靠web下一步完当作。

  2. 2

    第二步:在pom.xm文件中添加HTML视图解析依靠。

    <!--添加HTML视图解析依靠--><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

  3. 3

    第三步:pom.xm确认。

    <modelVersion>4.0.0</modelVersion>    <parent>        <groupId>org.springframework.boot</groupId>        <artifactId>spring-boot-starter-parent</artifactId>        <version>2.1.3.RELEASE</version>        <relativePath/> <!-- lookup parent from repository -->    </parent>    <groupId>com.example</groupId>    <artifactId>upload</artifactId>    <version>0.0.1-SNAPSHOT</version>    <name>upload</name>    <description>Demo project for Spring Boot</description>    <properties>        <java.version>1.8</java.version>    </properties>    <dependencies>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-web</artifactId>        </dependency>        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-test</artifactId>            <scope>test</scope>        </dependency>        <!--添加HTML视图解析依靠-->        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactId>spring-boot-starter-thymeleaf</artifactId>        </dependency>    </dependencies>    <build>        <plugins>            <plugin>                <groupId>org.springframework.boot</groupId>                <artifactId>spring-boot-maven-plugin</artifactId>            </plugin>        </plugins>    </build></project>

  4. 4

    第四步:编写代码。

    1、前端代码

    <!DOCTYPE html>

    <html>


    <head>

    <title>图片上传</title>

    <meta name="keywords" content="keyword1,keyword2,keyword3"></meta>

    <meta name="description" content="this is my page"></meta>

    <meta name="content-type" content="text/html; charset=UTF-8"></meta>

    </head>


    <body>

    <form enctype="multipart/form-data" method="post" action="/testUploadimg"> 

    图片:<input type="file" name="file" /><br/> 

    <input type="submit" value="上传" />.

    </form>

    </body>


    </html>

  5. 5

    第五步:节制器UploadController实现。

    UploadController 本家儿要分为3部门

    2.1 调整页面请求goUploadImg


    2.2 上传请求方式uploadImg


    2.3 存储图片方式uploadFile


    @Controllerpublic class UploadController {    

    //跳转到上传文件的页面    

    @RequestMapping(value = "/gouploadimg", method = RequestMethod.GET)    

    public String goUploadImg() {        

    //跳转到 templates 目次下的 uploadimg.html        

    return "uploadimg";    

    }    

    //处置文件上传    

    @ResponseBody //返回json数据    

    @RequestMapping(value = "/testUploadimg", method = RequestMethod.POST)    

    public String uploadImg(@RequestParam("file") MultipartFile file,                            

    HttpServletRequest request) {        

    tring contentType = file.getContentType();        

    String fileName = file.getOriginalFilename();        

    String filePath = "D:/img";        

    if (file.isEmpty()) {            

    return "文件为空!";        

    }        

    try {            

    uploadFile(file.getBytes(), filePath, fileName);        

    } catch (Exception e) {            

    // TODO: handle exception        

    }        

    //返回json        

    return "上传当作功";    

    }    

    public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {        

    File targetFile = new File(filePath);        

    if (!targetFile.exists()) {            

    targetFile.mkdirs();        

    }        

    FileOutputStream out = new FileOutputStream(filePath +"/"+ fileName);        

    out.write(file);        

    out.flush();        

    out.close();    

    }

    }

  6. 6

    第六步:测试。

    打开页面地址如下图所示:

注重事项

  • intellij IDEA 版本2018 2.2
  • jdk 1.8
  • apache-maven-3.5.4也可以利用IDEA自带的maven
  • 发表于 2019-06-08 20:11
  • 阅读 ( 810 )
  • 分类:其他类型

你可能感兴趣的文章

相关问题

0 条评论

请先 登录 后评论
admin
admin

0 篇文章

作家榜 »

  1. xiaonan123 189 文章
  2. 汤依妹儿 97 文章
  3. luogf229 46 文章
  4. jy02406749 45 文章
  5. 小凡 34 文章
  6. Daisy萌 32 文章
  7. 我的QQ3117863681 24 文章
  8. 华志健 23 文章

联系我们:uytrv@hotmail.com 问答工具