您的位置:首页 > 编程语言 > Java开发

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
就可以将之前模拟上传的文件下载下来,其中链接后面的数字是文件的上传后的名称。

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐