本篇文章将介绍如何使junit在springBoot中测试文件的上传,首先先阅读如何在springBoot中进行接口测试.

文件上传操作测试代码

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date; @RunWith(SpringRunner.class)
@SpringBootTest
public class BootStarterSecurityDemoApplicationTests { @Test
public void contextLoads() {
} @Autowired
private WebApplicationContext wac; private MockMvc mockMvc; /**
* 在每次测试执行前构建mvc环境
*/
@Before
public void setup() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
/**
* 测试上传文件
*/
@Test
public void whenUploadFileSuccess() {
try {
String result = mockMvc.perform(
MockMvcRequestBuilders
.fileUpload("/file")
.file(
new MockMultipartFile("file", "test.txt", ",multipart/form-data", "hello upload".getBytes("UTF-8"))
)
).andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
}
}

 编写文件下载接口

package com.micro.fast.security.demo.controller;

import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*; @RestController
@RequestMapping("/file")
public class FileController {
/**
* 处理文件上传
* @param file
*/
@PostMapping
public String upload(MultipartFile file){
File localfile = new File("/file/name");
try {
//将文件上传到本地路径
file.transferTo(localfile);
} catch (IOException e) {
e.printStackTrace();
}
String fileInfo = "";
fileInfo += file.getName()+file.getOriginalFilename()+file.getContentType();
return fileInfo;
} /**
* 文件的下载
* @param id
* @param request
* @param response
*/
@GetMapping("/{id}")
public void download(@PathVariable String id , HttpServletRequest request, HttpServletResponse response){
try (InputStream inputStream = new FileInputStream(new File("/root/file/name"));
OutputStream outputStream = response.getOutputStream();
){
response.setContentType("application/x-download");
//指定文件的名称
response.addHeader("Content-Disposition","attachment;filename=test.txt");
IOUtils.copy(inputStream,outputStream);
outputStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

原文地址:https://blog.csdn.net/c_intmian/article/details/79543292

springBoot中使用使用junit测试文件上传,以及文件下载接口编写的更多相关文章

  1. springCloud 微服务通过minio实现文件上传和文件下载接口

    直接上代码吧,好多文章的下载都写的不明不白的,让人理解错,气死了!! 文件上传功能 文件上传很简单,首先你得部署好minio,然后写好配置信息,我的是动态读取nacos上配置的yml @Autowir ...

  2. 分享知识-快乐自己:SpringMvc中的单多文件上传及文件下载

    摘要:SpringMvc中的单多文件上传及文件下载:(以下是核心代码(拿过去直接能用)不谢) <!--设置文件上传需要的jar--> <dependency> <grou ...

  3. postman测试文件上传接口教程

    postman是一个很好的接口测试软件,有时候接口是Get请求方式的,肯定在浏览器都可以测了,不过对于比较规范的RestFul接口,限定了只能post请求的,那你只能通过工具来测了,浏览器只能支持ge ...

  4. struts2的文件上传和文件下载

    实现使用Struts2文件上传和文件下载: 注意点: (1)对应表单的file1和私有成员变量的名称必须一致 <input type="file" name="fi ...

  5. JavaEE开发之SpringMVC中的自定义消息转换器与文件上传

    上篇博客我们详细的聊了<JavaEE开发之SpringMVC中的静态资源映射及服务器推送技术>,本篇博客依然是JavaEE开发中的内容,我们就来聊一下SpringMVC中的自定义消息转发器 ...

  6. soapUI 之 测试文件上传 [6]

    在接口测试中会遇到需要上传文件的操作,比如头像修改等.那么soapui是怎么实现这部分测试的呢.以下以文件上传接口为例. 一.获取文件上传接口 可以通过开发直接提供的接口文档,或者自己抓包获取接口信息 ...

  7. springboot 整合 tobato 的 fastdfs 实现文件上传和下载

    添加项目所需要的依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId ...

  8. struts2中的文件上传,文件下载

    文件上传: Servlet中的文件上传回顾 前台页面 1.提交方式post 2.表单类型 multipart/form-data 3.input type=file 表单输入项 后台 apache提交 ...

  9. SpringBoot完美配置阿里云的文件上传

    新建一个config类 AliyunOSS.java @Configuration @Data public class AliyunOSS { private OSSClient ossClient ...

随机推荐

  1. 【JZOJ4899】【NOIP2016提高A组集训第17场11.16】雪之国度

    题目描述 雪之国度有N座城市,依次编号为1到N,又有M条道路连接了其中的城市,每一条道路都连接了不同的2个城市,任何两座不同的城市之间可能不止一条道路.雪之女王赋予了每一座城市不同的能量,其中第i座城 ...

  2. Linux安装mongoDB步骤和方法

    Linux安装mongoDB步骤和方法 下载mongoDB数据库 mongodb-linux-x86_64-3.0.15.tgz 存放到linux文件夹中 ftp软件直接拖上去 解压文件夹(解压后,会 ...

  3. thinkphp php审核后返回信息给html

    1.die("<script>alert('至少选择一个收款方式!');history.back(-1);</script>");

  4. 【JZOJ4841】【NOIP2016提高A组集训第4场11.1】平衡的子集

    题目描述 夏令营有N个人,每个人的力气为M(i).请大家从这N个人中选出若干人,如果这些人可以分成两组且两组力气之和完全相等,则称为一个合法的选法,问有多少种合法的选法? 数据范围 40%的数据满足: ...

  5. Java练习 SDUT-2561_九九乘法表

    九九乘法表 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 九九乘法表是数学学习的基础,今天我们就来看看乘法表的相关问题 ...

  6. VelocityTracker监控速度!!!

    用来追踪触摸事件(flinging事件和其他手势事件)的速率.用obtain()函数来获得类的实例,用addMovement(MotionEvent)函数将motion event加入到Velocit ...

  7. 快速删除项目中的输出日志console.log

    项目开发时,控制台往往有许多忘记删除或注释掉的输出日志.但是上线后总不能一个一个删吧,最近总结出几个解决思路 重写console.log方法,使其失去 输出能力 这个最直接有效,用vue框架的话放在m ...

  8. axios细节之绑定到原型和axios的defaults的配置属性

    把axios绑定到原型 vue开发者一套很好用的实践,一般来说,实践如果能够让大部分人都接受,会逐渐成为一个默认的标准. // 把axios配置到原型上 Vue.prototype.$axios = ...

  9. init()方法必须使用super.init(config)的原因--Servlet

    原 因: 一个servlet在它的init()方法中传递它的ServletConfig实例,在其他的方法中却不可以.当一个servlet在 init()方法外需要调用config对象时就会产生问题.使 ...

  10. UIWebView 真机iOS 8.x系统上报错

    crush发生后最终定位到: dyld`dyld_fatal_error: -> >: brk #0x3 控制台打印信息如下: dyld: Symbol not found: _OBJC_ ...