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");
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Java课程实验 Thymeleaf 模板应用
共72个文件
java:16个
class:16个
xml:13个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 64 浏览量
2023-07-07
10:43:35
上传
评论
收藏 295KB ZIP 举报
温馨提示
Thymeleaf是一种用于Java和Spring应用程序的现代化服务器端Java模板引擎。它允许在模板中嵌入动态内容,并能够与后端Java代码进行交互,生成动态的HTML页面。 以下是Thymeleaf模板的一些基本特点和语法: 1. 声明Thymeleaf命名空间: - 在HTML页面的根元素上添加`xmlns:th="http://www.thymeleaf.org"`命名空间声明。 2. 表达式语法: - 使用`${}`来显示表达式的值。例如:`${user.name}`将显示名为"user"对象中的"name"属性的值。 - 使用`*{}`来设定属性的值。例如:`<input type="text" th:field="*{username}">`将把"name"字段绑定到名为"username"的表达式。 Thymeleaf提供了丰富的表达式和标签属性,使开发人员能够更容易地构建动态的HTML页面。它与Spring框架完美集成,可用于构建动态的Web应用程序。
资源推荐
资源详情
资源评论
收起资源包目录
Java课程实验 Thymeleaf 模板应用.zip (72个子文件)
Java课程实验 Thymeleaf 模板应用
Thymeleaf 模板应用
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币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功