java入門 springboot上傳文件

一、 pom.xml

knife4j和springboot之間存在版本不兼容的問題,需要選對合適的版本

<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.sht</groupId><artifactId>test</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>test</name><url>http://maven.apache.org</url><properties><maven.compiler.source>1.8</maven.compiler.source><maven.compiler.target>1.8</maven.compiler.target><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.3.6.RELEASE</version></parent><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- springboot 依賴 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-logging</artifactId></exclusion></exclusions></dependency><!-- spring security --><!-- <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId></dependency><!-- 日志 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-log4j2</artifactId></dependency><!-- mybatis數據庫支持 --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.3.2</version></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><!-- 調試支持 --><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>2.0.8</version></dependency><!-- lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.12</version></dependency><!-- jwt --><!-- <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>3.4.0</version> </dependency> --><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.4</version></dependency></dependencies>
</project>

二、 App.java

app.java

package com.sht.test;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class App 
{public static void main( String[] args ){SpringApplication.run(App.class, args);}
}

三、 Controller

HelloController.java

package com.sht.test.controller;import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;@RestController
@Api(value = "文件上傳",tags = "文件上傳")
public class HelloController {//文件上傳@PostMapping("/file/upload")@ApiImplicitParams({@ApiImplicitParam(name = "name", value = "文件名"),@ApiImplicitParam(name = "file", value = "文件內容")})public String upload(@RequestParam(value = "name") String name,@RequestParam(value = "file") MultipartFile file) {try {File diskFile = new File("D:\\myfile\\project\\ttt\\oxygen\\test\\data\\" + name);if (!diskFile.exists()) {diskFile.createNewFile();}InputStream is = file.getInputStream();OutputStream os = new FileOutputStream(diskFile);IOUtils.copy(is, os);} catch (Exception e) {e.printStackTrace();}return "success";}
}

四、 Knife4j配置

Knife4jConfig.java

package com.sht.test.config;import java.util.ArrayList;
import java.util.List;import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;@SpringBootConfiguration
@EnableSwagger2WebMvc
public class Knife4jConfig {@Beanpublic Docket adminApiConfig(){List<Parameter> pars = new ArrayList<>();ParameterBuilder tokenPar = new ParameterBuilder();tokenPar.name("token").description("用戶token").defaultValue("").modelRef(new ModelRef("string")).parameterType("header").required(false).build();pars.add(tokenPar.build());//添加head參數endDocket adminApi = new Docket(DocumentationType.SWAGGER_2).groupName("adminApi").apiInfo(adminApiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.sht.test")).build().globalOperationParameters(pars);return adminApi;}private ApiInfo adminApiInfo(){return new ApiInfoBuilder().title("測試-API文檔").description("測試").version("1.0").contact(new Contact("張三", "", "xxxxxxx")).build();}
}

五、 測試截圖

在這里插入圖片描述

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/bicheng/17591.shtml
繁體地址,請注明出處:http://hk.pswp.cn/bicheng/17591.shtml
英文地址,請注明出處:http://en.pswp.cn/bicheng/17591.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

雜談|RestFul和http的區別

前言 今天和我一組的小伙伴&#xff0c;在對接一個接口時&#xff0c;客戶將DELETED請求設置了body參數&#xff0c;導致一個功能反復搞了半天&#xff0c;今天就來說下這兩者的區別 1.HTTP概述 HTTP&#xff08;HyperText Transfer Protocol&#xff09;是一種用于從WWW&…

操作系統5_虛擬存儲器

操作系統5_虛擬存儲器 文章目錄 操作系統5_虛擬存儲器1. 虛擬存儲器1.1 虛擬存儲器的引入1.2 虛擬存儲器的概念1.3 虛擬存儲器的特征1.4 虛擬存儲器的實現方法2. 請求分頁存儲管理2.1 請求分頁中的硬件支持2.2 內存分配策略和分配算法2.3 調頁策略2.4 頁面置換算法2.4.1 最佳置…

docker部署相關命令

docker部署相關操作 查看docker基本信息 docker info查看docker中所有鏡像 docker images查看docker中所有容器 docker ps # 已啟動的容器 docker ps -a # 所有容器 docker ps -a -s # 查看所有容器和大小從鏡像創建容器并運行 docker run -it 鏡像名 # 簡單命令 dock…

c++——模板初始識

1.函數模板 我們經常用到Swap函數交換兩個值。由于需要交換的數據的類型不同&#xff0c;我們就需要寫不同參數類型的同名函數&#xff0c;也就是函數重載&#xff1a; 然而這三個函數的邏輯是一樣的&#xff0c;寫這么多有些多此一舉&#xff0c;通過函數模版可以寫一個通用…

LabVIEW機器視覺在自動化生產線中的應用是什么?

LabVIEW機器視覺技術在自動化生產線中有廣泛的應用&#xff0c;主要包括以下幾個方面&#xff1a; 質量控制與檢測&#xff1a; 缺陷檢測&#xff1a;使用機器視覺系統實時檢測產品表面的缺陷&#xff0c;如劃痕、裂紋、污漬等&#xff0c;確保產品質量。尺寸測量&#xff1a;通…

【量算分析工具-水平面積】GeoServer改造Springboot番外系列五

【量算分析工具-概述】GeoServer改造Springboot番外系列三-CSDN博客 【量算分析工具-水平距離】GeoServer改造Springboot番外系列四-CSDN博客 【量算分析工具-水平面積】GeoServer改造Springboot番外系列五-CSDN博客 【量算分析工具-方位角】GeoServer改造Springboot番外系列…

GoldenEye-v1(vulnhub)靶機練習實踐報告

GoldenEye-v1****靶機練習實踐報告 一、安裝靶機 靶機是.ova文件&#xff0c;需要用VirtualBox打開&#xff0c;但我習慣于使用VMWare,因此修改靶機文件&#xff0c;使其適用于VMWare打開。 解壓ova文件&#xff0c;得到.ovf文件和.vmdk文件。 用記事本打開.ovf文件并修改“…

Element Plus 快速入門

Element Plus 快速入門 Element Plus 是一個基于 Vue 3.0 的桌面端組件庫&#xff0c;它包含了豐富的組件和實用的工具&#xff0c;可以幫助開發者快速構建 Vue 3.0 應用。 安裝 首先&#xff0c;我們需要在項目中安裝 Element Plus。在終端中運行以下命令&#xff1a; npm…

gmssl vs2010編譯

1、虛擬機win10 x64&#xff0c;離線安裝vs2010和2010sp1補丁&#xff1b; 2、安裝ActivePerl_v5.28.1.0000和nasm-2.16.03-installer-x64均是默認完整安裝&#xff1b; nasm官網下載&#xff1a; Index of /pub/nasm/releasebuilds/2.16.03/win64https://www.nasm.us/pub/nas…

Unity 之 Android 【獲取設備的序列號 (Serial Number)/Android_ID】功能的簡單封裝

Unity 之 Android 【獲取設備的序列號 (Serial Number)/Android_ID】功能的簡單封裝 目錄 Unity 之 Android 【獲取設備的序列號 (Serial Number)/Android_ID】功能的簡單封裝 一、簡單介紹 二、獲取設備的序列號 (Serial Number) 實現原理 1、Android 2、 Unity 三、注意…

九型人格介紹

協調型人格 作為“好好先生”的何炅是典型的協調型人格者&#xff0c;他總是將大家的利益放在第一位&#xff0c;很少顧及自己的感受;當他周圍的人產生沖突時&#xff0c;他總是力圖找到一個有利于雙方的解決方案;本著息事寧人的態度&#xff0c;他對利益的追逐和向往很低&…

gem5模擬器入門(一)——環境配置

什么是gem5&#xff1f; gem5是一個模塊化的離散事件驅動的計算機系統模擬器平臺。這意味著&#xff1a; GEM5 的組件可以輕松重新排列、參數化、擴展或更換&#xff0c;以滿足您的需求。它將時間的流逝模擬為一系列離散事件。它的預期用途是以各種方式模擬一個或多個計算機系…

掌握并發控制的“急剎車”藝術!

當一個線程運行時&#xff0c;另外一個線程可以直接通過interrupt方法對其設置中斷標志位。 判斷線程是否中斷的2個方法&#xff1a; // 判斷目標線程是否被中斷&#xff0c;不會清除中斷標記。 Thread.currentThread().isInterrupted() // 判斷目標線程是否被中斷&#xff0c;…

【職業教育培訓機構小程序】教培機構“招生+教學”有效解決方案

教培機構“招生教學”有效解決方案在數字化轉型的浪潮中&#xff0c;職業教育培訓機構面臨著提升教學效率、拓寬招生渠道、增強學員互動等多重挑戰。小程序作為一種新興的移動應用平臺&#xff0c;為解決這些痛點提供了有效途徑。 一、職業教育培訓機構小程序的核心功能 &…

Laravel 圖片添加水印

和這個配合使用 Laravel ThinkPhP 海報生成_laravel 制作海報-CSDN博客 代碼 //水印 $x_length $imageInfo[0]; $y_length $imageInfo[1];$color imagecolorallocatealpha($posterImage, 255, 255, 255, 70); // 增加透明度參數alpha$font_size 40; //字體大小 $angle …

HTML靜態網頁成品作業(HTML+CSS)——家鄉沅陵介紹網頁(1個頁面)

&#x1f389;不定期分享源碼&#xff0c;關注不丟失哦 文章目錄 一、作品介紹二、作品演示三、代碼目錄四、網站代碼HTML部分代碼 五、源碼獲取 一、作品介紹 &#x1f3f7;?本套采用HTMLCSS&#xff0c;未使用Javacsript代碼&#xff0c;共有1個頁面。 二、作品演示 三、代…

條款9:利用destructors避免泄露資源

對指針說拜拜。承認吧&#xff0c;你從未真正喜歡過它&#xff0c;對不&#xff1f; 好&#xff0c;你不需要對所有指針說拜拜&#xff0c;但是你真的得對那些用來操控局部性資源(local resources&#xff09;的指針說莎唷娜拉了。 舉個例子&#xff0c;你正在為“小動物收養…

Flutter 中的 CircularProgressIndicator 小部件:全面指南

Flutter 中的 CircularProgressIndicator 小部件&#xff1a;全面指南 在 Flutter 應用開發中&#xff0c;加載指示器是提供用戶反饋的重要組成部分&#xff0c;特別是在需要等待數據加載的場景中。CircularProgressIndicator 是 Flutter 提供的一個表現圓形加載動畫的小部件。…

Python進階:探索Python標準庫和第三方庫

在前兩篇文章中,我們介紹了Python的基本語法和面向對象編程。在這篇文章中,我們將深入探索Python的標準庫以及一些常用的第三方庫。Python的強大之處不僅在于其簡潔的語法,還在于豐富的庫生態系統。通過使用這些庫,你可以更高效地完成各種任務,從文件操作到數據分析、網絡…

godot4.2 + GDextension c++在 vs code 中斷點調試配置

游戲開發中如果做不到自己編寫的代碼做斷點調試&#xff0c;無不是瞎子摸象&#xff0c;特別是C這么底層的語言。這2天開始在VS studio中折騰&#xff0c;一直折騰不出結果&#xff0c;幾次想要放棄GODOT。最終今天在VS code中搞定了這斷點調試C代碼。 在上一篇文章我已經做好了…