Spring Security技术栈开发企业级认证与授权使用REST方式处理文件服务
2018-10-12 10:40
483 查看
Spring Security技术栈开发企业级认证与授权使用REST方式处理文件服务
2018年04月02日 17:46:40 lemon__jiang 阅读数:157 标签: Spring BootSpring MVCSpring Security文件上传文件下载 更多
个人分类: Spring Security
所属专栏: Spring Security技术栈开发企业级认证与授权
版权声明:本文为博主原创文章,转载请注明出处。 https://blog.csdn.net/Lammonpeter/article/details/79787861
Spring Boot实现文件的上传和下载十分便捷,之前已经写了一篇关于Spring Boot实现文件上传的博客,用的是浏览上传的,而本篇博客使用的是代码模拟上传,两种方式不一样,可以参考一下。
一、文件上传
通过RESTful API上传文件,代码如下:
[code]package com.lemon.security.web.controller; import cn.hutool.core.io.IoUtil; import com.lemon.security.web.dto.FileInfo; 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.*; /** * @author lemon * @date 2018/4/2 下午2:19 */ @RestController @RequestMapping("/file") public class FileController { private static String folder = "/Users/lemon/IdeaProjects/spring-security/lemon-security-demo"; @PostMapping public FileInfo upload(@RequestParam("file") MultipartFile file) throws IOException { System.out.println("上传文件的表单name值为:" + file.getName()); System.out.println("文件路径为:" + file.getOriginalFilename()); System.out.println("文件大小为:" + file.getSize()); File localFile = new File(folder, System.currentTimeMillis() + ".txt"); // 执行上传操作 file.transferTo(localFile); return new FileInfo(localFile.getAbsolutePath()); } }
上面的代码的FileInfo类就是一个包含文件路径的一个JavaBean。
[code]package com.lemon.security.web.dto; import lombok.Data; /** * @author lemon * @date 2018/4/2 下午2:24 */ @Data public class FileInfo { private String path; public FileInfo(String path) { this.path = path; } }
- 上传的代码为:
[code]@Test public void fileUpload() throws Exception { String result = mockMvc.perform(MockMvcRequestBuilders.fileUpload("/file") .file(new MockMultipartFile("file", "test.txt", "multipart/form-data", "hello world".getBytes("UTF-8")))) .andExpect(MockMvcResultMatchers.status().isOk()) .andReturn().getResponse().getContentAsString(); System.out.println(result); }
二、文件下载
文件下载的代码为:
[code]package com.lemon.security.web.controller; import cn.hutool.core.io.IoUtil; import com.lemon.security.web.dto.FileInfo; 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.*; /** * @author lemon * @date 2018/4/2 下午2:19 */ @RestController @RequestMapping("/file") public class FileController { private static String folder = "/Users/lemon/IdeaProjects/spring-security/lemon-security-demo"; @GetMapping("/{id}") public void download(@PathVariable String id, HttpServletRequest request, HttpServletResponse response) { System.out.println(folder); try ( // 这是JDK7的特性,关于流的操作,可以写在try后面的圆括号里,这样就无需手动关闭流 InputStream inputStream = new FileInputStream(new File(folder, id + ".txt")); OutputStream outputStream = response.getOutputStream() ) { // 设置下载的文件类型 response.setContentType("application/x-download"); // 设置下载后的文件名 response.setHeader("Content-Disposition", "attachment;filename=test.txt"); IoUtil.copy(inputStream, outputStream); // 刷新输出流 outputStream.flush(); } catch (IOException e) { e.printStackTrace(); } } }
从浏览器访问
http://localhost:8080/file/152445566433就可以将之前模拟上传的文件下载下来,其中链接后面的数字是文件的上传后的名称。 阅读更多
相关文章推荐
- Spring Security技术栈开发企业级认证与授权(七)使用Swagger自动生成API文档
- Spring Security技术栈开发企业级认证与授权
- Spring Security技术栈开发企业级认证与授权(四)RESTful API服务异常处理
- Spring Security技术栈开发企业级认证与授权
- Spring Security技术栈开发企业级认证与授权(一)环境搭建
- 使用J2ME技术开发RPG游戏(二)——按键处理机制
- 使用J2ME技术开发RPG游戏(二)——按键处理机制
- linux下使用文件来模拟硬盘进行ASM测试-开发技术
- 使用J2ME技术开发RPG游戏(二)——按键处理机制
- 使用J2ME技术开发RPG游戏(二)——按键处理机制
- 基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET - 服务定位器(SL)与AgileEAS.NET中的实现
- 基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET平台开发指南 - 处理报表
- WCF步步为营(三):使用配置文件改变使用服务的方式
- 基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET - 4.0 最新发布版本 -下载使用说明
- 基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET平台开发指南 - 配置文件
- .NET开发中的事务处理大比拼 之 企业级服务COM+事务
- 使用J2ME技术开发RPG游戏(二)——按键处理机制
- 使用J2ME技术开发RPG游戏(二)——按键处理机制
- 基于DotNet构件技术的企业级敏捷软件开发平台 - AgileEAS.NET平台开发指南 - 对象设计器使用帮助
- 使用J2ME技术开发RPG游戏(二)——按键处理机制