SpringCloud(第 023 篇)簡單文件上傳微服務采取curl或者頁面點擊實現(xiàn)文件上傳

SpringCloud(第 023 篇)簡單文件上傳微服務采取curl或者頁面點擊實現(xiàn)文件上傳

一、大致介紹

1、本章節(jié)主要搭建了一個簡單的頁面上傳Web控制器,主要為后序工作加入 zuul 微服務而做的準備;
2、不過在本章節(jié)用命令上傳文件的時候,在windows命令窗口有時候會出現(xiàn)中文亂碼什么的,請注意看本文 FileUploadController 是如何解決這個亂碼問題的;
3、至于使用 curl 命令需要下載什么安裝包之類的,這個就請大家找找度娘怎么弄吧。

二、實現(xiàn)步驟

2.1 添加 maven 引用包

<?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>

    <artifactId>springms-file-upload</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>
    
    <parent>
        <groupId>com.springms.cloud</groupId>
        <artifactId>springms-spring-cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    
    <dependencies>
        <!-- web模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- 客戶端發(fā)現(xiàn)模塊 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- 監(jiān)控和管理生產(chǎn)環(huán)境的模塊 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

</project>

2.2 添加應用配置文件(springms-file-upload\src\main\resources\application.yml)

server:
  port: 8190
eureka:
  client:
    serviceUrl:
      defaultZone: http://admin:admin@localhost:8761/eureka/
  instance:
    prefer-ip-address: true
spring:
  application:
    name: springms-file-upload
  http:
    multipart:
      max-file-size: 20Mb      # Max file size,默認1M
      max-request-size: 20Mb   # Max request size,默認10M



#####################################################################################################
# 打印日志
logging:
  level:
    root: INFO
    com.springms: DEBUG
    com.netflix: debug
#####################################################################################################

2.3 添加簡單的上傳文件頁面(springms-file-upload\src\main\resources\static\index.html)

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form method="POST" enctype="multipart/form-data" action="/upload">
        File to upload: 
        <input type="file" name="file">
        <input type="submit" value="Upload">
    </form>
</body>
</html>

2.4 添加上傳文件Web控制器(springms-file-upload\src\main\java\com\springms\cloud\controller\FileUploadController.java)

package com.springms.cloud.controller;

import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 上傳文件控制器。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/26
 *
 */
@RestController
public class FileUploadController {

    /**
     * 上傳文件。
     *
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping(value = "/upload", method = RequestMethod.POST)
    @ResponseBody
    public String uploadFile(@RequestParam(value = "file", required = true)MultipartFile file) throws IOException{
        byte[] bytes = file.getBytes();
        File fileToSave = new File(file.getOriginalFilename());
        FileCopyUtils.copy(bytes, fileToSave);
        return fileToSave.getAbsolutePath();
    }

//    解決 windows 的 curl 命令執(zhí)行后返回亂碼
//    chcp 65001 就是換成UTF-8代碼頁
//    chcp 936 可以換回默認的GBK
//    chcp 437 是美國英語
//    在命令行標題欄上點擊右鍵,選擇"屬性"->"字體",將字體修改為True Type字體"Lucida Console",然后點擊確定將屬性應用到當前窗口。
}

2.5 添加文件服務啟動類(springms-file-upload\src\main\java\com\springms\cloud\MsFileUploadApplication.java)

package com.springms.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

/**
 * 簡單文件上傳微服務采取curl或者頁面點擊實現(xiàn)文件上傳。
 *
 * @author hmilyylimh
 *
 * @version 0.0.1
 *
 * @date 2017/9/26
 *
 */
@SpringBootApplication
@EnableEurekaClient
public class MsFileUploadApplication {

    public static void main(String[] args) {
        SpringApplication.run(MsFileUploadApplication.class, args);
        System.out.println("【【【【【【 FileUpload微服務 】】】】】】已啟動.");
    }
}

三、測試

/****************************************************************************************
 一、簡單文件上傳微服務,并加入 zuul 微服務后用 zuul 微服務地址采取curl或者頁面點擊實現(xiàn)文件上傳(頁面上傳文件):

 1、編寫 FileUploadController 文件,添加應用程序的注解 EnableEurekaClient 配置;
 2、啟動 springms-discovery-eureka 模塊服務,啟動1個端口;
 3、啟動 springms-file-upload 模塊服務;

 4、新起網(wǎng)頁頁簽,輸入 http://localhost:8190/index.html 正常情況下是能看到選擇文件上傳的界面;
 5、選擇文件,然后點擊 upload 上傳文件,然后可以在該項目所在的根目錄可以看到剛剛上傳的那個文件,而且網(wǎng)頁也會將剛剛上傳完后的磁盤路徑呈現(xiàn)在頁面上;
 ****************************************************************************************/





/****************************************************************************************
 二、簡單文件上傳微服務,并加入 zuul 微服務后用 zuul 微服務地址采取curl或者頁面點擊實現(xiàn)文件上傳(命令上傳文件):

 1、編寫 FileUploadController 文件,添加應用程序的注解 EnableEurekaClient 配置;
 2、啟動 springms-discovery-eureka 模塊服務,啟動1個端口;
 3、啟動 springms-file-upload 模塊服務;

 4、進入 curl.exe 所在的目錄,嘗試 curl.exe www.baidu.com 看看是否正常,正常情況下會打印百度首頁的一堆信息;
 5、執(zhí)行命令:curl.exe -F GBK "file=@文件名稱" localhost:8190/upload
 6、正常情況下,第5步驟執(zhí)行后,直接返回上傳成功文件所在的磁盤全路徑;
 ****************************************************************************************/

四、下載地址

https://gitee.com/ylimhhmily/SpringCloudTutorial.git

SpringCloudTutorial交流QQ群: 235322432

SpringCloudTutorial交流微信群: 微信溝通群二維碼圖片鏈接

歡迎關注,您的肯定是對我最大的支持!!!

<上一篇????????首頁????????下一篇>

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容