• <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>

    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
    • 閱讀 ( 814 )
    • 分類:其他類型

    你可能感興趣的文章

    相關問題

    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 問答工具
  • <noscript id="ecgc0"><kbd id="ecgc0"></kbd></noscript>
    <menu id="ecgc0"></menu>
  • <tt id="ecgc0"></tt>
    久久久久精品国产麻豆