package com.mask.fileserver.controller;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import com.mask.fileserver.domain.File;
import com.mask.fileserver.service.FileService;
import com.mask.fileserver.util.MD5Util;
@CrossOrigin(origins = "*", maxAge = 3600) // 允许所有域名访问
@Controller
public class FileController {
@Autowired
private FileService fileService;
@RequestMapping(value = "/")
public String index(Model model) {
model.addAttribute("files", fileService.listFiles());
return "index";
}
/**
* 获取文件片信息
* @param id
* @return
*/
@GetMapping("/{id}")
@ResponseBody
public ResponseEntity serveFile(@PathVariable String id) {
File file = fileService.getFileById(id);
if (file != null) {
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; fileName=\"" + file.getName() + "\"")
.header(HttpHeaders.CONTENT_TYPE, "application/octet-stream" )
.header(HttpHeaders.CONTENT_LENGTH, file.getSize()+"")
.header("Connection", "close")
.body( file.getContent());
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File was not fount");
}
}
/**
* 在线显示文件
* @param id
* @return
*/
@GetMapping("/view/{id}")
@ResponseBody
public ResponseEntity serveFileOnline(@PathVariable String id) {
File file = fileService.getFileById(id);
if (file != null) {
return ResponseEntity
.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "fileName=\"" + file.getName() + "\"")
.header(HttpHeaders.CONTENT_TYPE, file.getContentType() )
.header(HttpHeaders.CONTENT_LENGTH, file.getSize()+"")
.header("Connection", "close")
.body( file.getContent());
} else {
return ResponseEntity.status(HttpStatus.NOT_FOUND).body("File was not fount");
}
}
/**
* 上传
* @param file
* @param redirectAttributes
* @return
*/
@PostMapping("/")
public String handleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
try {
File f = new File(file.getOriginalFilename(), file.getContentType(), file.getSize(), file.getBytes());
f.setMd5( MD5Util.getMD5(file.getInputStream()) );
fileService.saveFile(f);
} catch (IOException | NoSuchAlgorithmException ex) {
ex.printStackTrace();
redirectAttributes.addFlashAttribute("message",
"Your " + file.getOriginalFilename() + " is wrong!");
return "redirect:/";
}
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded " + file.getOriginalFilename() + "!");
return "redirect:/";
}
@GetMapping("/del/{id}")
public String deleteFile(@PathVariable String id, RedirectAttributes redirectAttributes) {
try {
fileService.removeFile(id);
redirectAttributes.addFlashAttribute("message",
"You successfully delete " + id + "!");
} catch (Exception e) {
redirectAttributes.addFlashAttribute("message",
"fail to delete " + id + "!");
}
return "redirect:/";
}
@PostMapping("/upload")
@ResponseBody
public ResponseEntity<String> handleFileUpload(@RequestParam("file") MultipartFile file) {
File returnFile = null;
try {
File f = new File(file.getOriginalFilename(), file.getContentType(), file.getSize(),file.getBytes());
f.setMd5( MD5Util.getMD5(file.getInputStream()) );
returnFile = fileService.saveFile(f);
returnFile.setPath("http://localhost:8081/view/"+f.getId());
returnFile.setContent(null) ;
return ResponseEntity.status(HttpStatus.OK).body("http://localhost:8081/view/"+f.getId());
} catch (IOException | NoSuchAlgorithmException ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(ex.getMessage());
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,适合用作毕业设计、课程设计作业等,项目均经过测试,可快速部署运行! 基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,适合用作毕业设计、课程设计作业等,项目均经过测试,可快速部署运行! 基于node.js、vue、mongodb等技术构建的web系统,界面美观,功能齐全,适合用作毕业设计、课程设计作业等,项目均经过测试,可快速部署运行!
资源推荐
资源详情
资源评论
收起资源包目录
基于SpringBoot和MongoDB的文件管理系统.zip (37个子文件)
project_demo
.classpath 1KB
.settings
org.eclipse.jdt.core.prefs 291B
org.eclipse.core.resources.prefs 250B
org.springframework.ide.eclipse.prefs 65B
org.eclipse.wst.common.project.facet.core.xml 145B
org.eclipse.m2e.core.prefs 90B
pom.xml 2KB
src
main
resources
application.properties 277B
templates
index.html 1KB
fragments
header.html 228B
footer.html 182B
static
favicon.ico 4KB
java
com
mask
fileserver
Application.java 309B
repository
FileRepository.java 231B
controller
FileController.java 5KB
service
FileServiceImpl.java 772B
FileService.java 522B
util
MD5Util.java 1KB
domain
File.java 3KB
server
config
SecurityConfig.java 553B
target
classes
application.properties 277B
templates
index.html 1KB
fragments
header.html 228B
footer.html 182B
static
favicon.ico 4KB
META-INF
MANIFEST.MF 335B
maven
com.mask
file-server
pom.properties 227B
pom.xml 2KB
com
mask
fileserver
repository
FileRepository.class 362B
Application.class 698B
controller
FileController.class 6KB
service
FileServiceImpl.class 2KB
FileService.class 461B
util
MD5Util.class 1KB
domain
File.class 4KB
server
config
SecurityConfig.class 1KB
.project 1KB
共 37 条
- 1
资源评论
白话机器学习
- 粉丝: 1w+
- 资源: 7671
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功