package as.lwl.control;
import org.apache.commons.io.FileUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.net.URLEncoder;
import java.util.UUID;
@Controller
public class FileController {
String pathParent = "G:/File";
//向文件上传页面跳转
@GetMapping("/toUpload")
public String toUpload(){
return "file/upload";
}
//文件上传管理
@PostMapping("/uploadFile")
public String uploadFile(MultipartFile[] fileUpload, Model model) {
//上传成功返回状态信息
model.addAttribute("uploadStatus", "上传成功!");
for (MultipartFile file : fileUpload) {
//获取文件名以及后缀名
String fileName = file.getOriginalFilename();
//重新生成文件名
fileName = UUID.randomUUID()+"_"+fileName;
//指定上传文件本地存储目录,不存在则需创建
String dirPath = pathParent+"/upload/";
File filePath = new File(dirPath);
if(!filePath.exists()){
filePath.mkdirs();
}
try {
file.transferTo(new File(dirPath+fileName));
} catch (Exception e) {
e.printStackTrace();
//上传失败返回失败信息
model.addAttribute("uploadStatus","上传失败: "+e.getMessage());
}
}
//携带上传状态信息回调到文件上传页面
return "file/upload";
}
//向文件下载页面跳转
@GetMapping("/toDownload")
public String toDownload(){
return "file/download";
}
@GetMapping("/download")
//所有类型文件下载管理
public ResponseEntity<byte[]> fileDownload(HttpServletRequest request, String filename) throws Exception{
//指定要下载的文件根路径
String dirPath = pathParent+"/download/";
//创建该文件对象
File file = new File(dirPath + File.separator + filename);
//设置响应头
HttpHeaders headers = new HttpHeaders();
//通知浏览器已下载方式打开(下载前对文件名进行转码)
filename=getFilename(request,filename);
headers.setContentDispositionFormData("attachment",filename);
//定义以流的方式下载返回文件数据
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
try {
return new ResponseEntity<>(FileUtils.readFileToByteArray(file), headers, HttpStatus.OK);
} catch (Exception e) {
e.printStackTrace();
return new ResponseEntity<byte[]>(e.getMessage().getBytes(),HttpStatus.EXPECTATION_FAILED);
}
}
//根据浏览器的不同编码设置,返回编码的文件名
private String getFilename(HttpServletRequest request, String filename)
throws Exception {
//IE不同版本User-Agent中出现的关键字
String[] IEBrowserKeyWords = {"MSIE", "Trident", "Edge"};
//获取请求头代理信息
String userAgent = request.getHeader("User-Agent");
for (String keyWord : IEBrowserKeyWords) {
if (userAgent.contains(keyWord)) {
//IE内核浏览器,统一为UTF-8编码显示,并对转换的+进行更正
return URLEncoder.encode(filename, "UTF-8").replace("+"," ");
}
}
//火狐等其他浏览器统一为ISO-8859-1编码显示
return new String(filename.getBytes("UTF-8"), "ISO-8859-1");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
在Spring Boot中,你可以使用Spring框架提供的缓存管理来提高应用程序的性能。Spring Boot支持多种缓存实现,包括内存缓存和分布式缓存。 1.添加缓存依赖: 在项目的 pom.xml 文件中添加所需的缓存依赖。 2.配置缓存管理器: 在应用程序的配置类中添加@EnableCaching注解,启用缓存功能。 创建一个继承自org.springframework.cache.annotation.CachingConfigurerSupport的配置类,并重写其中的cacheManager()方法。 3.使用缓存注解: 在需要进行缓存的方法上添加缓存注解。例如,使用@Cacheable注解来指定一个方法的结果应该被缓存。 @Cacheable:将方法的结果缓存起来,下次调用该方法时,如果传入的参数相同,则直接返回缓存中的数据。 @CacheEvict:清除指定方法的缓存数据,可以配置在方法执行前或执行后进行缓存清除。 @CachePut:更新缓存数据,即使缓存已存在也会执行方法并更新缓存。 通过以上步骤,可以在Spring Boot中使用缓存管理器来提高应用程序的性能。
资源推荐
资源详情
资源评论
收起资源包目录
Java课程实验 Spring Boot 缓存管理.zip (72个子文件)
Java课程实验 Spring Boot 缓存管理
Spring Boot 缓存管理
pom.xml 3KB
src
test
java
main
resources
mapper
ArticleMapper.xml 542B
application.properties 727B
templates
file
upload.html 1KB
download.html 582B
article.html 582B
login.html 899B
static
login
js
jquery.min.js 84KB
img
login.jpg 12KB
css
bootstrap.min.css 141KB
signin.css 906B
java
as
lwl
mapper
ArticleMapper.java 288B
CommentMapper.java 196B
repository
ArticleRepository.java 379B
Chapter04MySQL.java 474B
pojo
Comment.java 981B
ArticleSerialize.java 1KB
Article.java 813B
service
ArticleServiceImpl.java 2KB
ArticleService.java 257B
servletComponent
MyFilter.java 2KB
MyListener.java 673B
MyServlet.java 818B
control
FileController.java 4KB
APiRedisCacheController.java 985B
ThymeleafController.java 1KB
LoginController.java 1KB
.idea
jarRepositories.xml 880B
dataSources
4fd8c099-2c4c-449e-9547-cf9c66bc09c3
storage_v2
_src_
schema
performance_schema.kIw0nw.meta 76B
information_schema.FNRwLQ.meta 76B
mysql.osA4Bg.meta 63B
4fd8c099-2c4c-449e-9547-cf9c66bc09c3.xml 29KB
dataSources.local.xml 1010B
uiDesigner.xml 9KB
runConfigurations.xml 346B
workspace.xml 7KB
misc.xml 541B
dataSources.xml 664B
compiler.xml 709B
.gitignore 184B
encodings.xml 191B
target
classes
mapper
ArticleMapper.xml 542B
application.properties 727B
templates
file
upload.html 1KB
download.html 582B
article.html 582B
login.html 899B
static
login
js
jquery.min.js 84KB
img
login.jpg 12KB
css
bootstrap.min.css 141KB
signin.css 906B
as
lwl
mapper
ArticleMapper.class 408B
CommentMapper.class 320B
repository
ArticleRepository.class 519B
pojo
Article.class 1KB
ArticleSerialize.class 2KB
Comment.class 2KB
Chapter04MySQL.class 860B
service
ArticleService.class 268B
ArticleServiceImpl.class 2KB
servletComponent
MyFilter.class 3KB
MyServlet.class 917B
MyListener.class 728B
control
ThymeleafController.class 2KB
LoginController.class 1KB
FileController.class 5KB
APiRedisCacheController.class 1KB
LWL-04-1.0-SNAPSHOT.jar.original 81KB
maven-status
maven-compiler-plugin
compile
default-compile
createdFiles.lst 496B
inputFiles.lst 1000B
testCompile
default-testCompile
inputFiles.lst 0B
maven-archiver
pom.properties 89B
generated-sources
annotations
共 72 条
- 1
资源评论
Meteor.792
- 粉丝: 971
- 资源: 64
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 二叉树7-1-1.cpp
- android 9.0 原生模拟器 签名文件
- 技术面试最后反问面试官的话 校招面试非技术问题有哪些 非技术问题如何回答.png
- NB-IOT-BC26全网通模块Altium+ CADENCE +PADS三种格式(原理图SCH+PCB封装库)文件.zip
- 基于微信小程序开发的校园失物招领系统源码毕业设计(优质项目源码).zip
- 词向量是一种将自然语言中的单词转换为数值向量的技术,它能够捕捉词义和上下文信息
- nmap与masscan的简单使用
- MyBatis动态SQL.pdf
- 基于stm32单片机protues仿真的温湿度控制系统设计(仿真图、源代码)
- 词向量:自然语言处理的基石
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功