emmm… 刚刚放假休息完回来
由于要做一个文件管理系统 肯定就要弄文件上传和下载
这两天看了下博客 然后自己在弄 写个demo试一下 (加了一个简单的html页面来进行测试 暂时只实现了单文件和多文件上传 下载还没弄好。。然后也没有存入数据库 URL只用log.info打印出来了 并没有存入数据库里去 后面一两天准备做这些)
很简单的一层代码 其实目前有用的就是controller层 其他都没有用到(功能还不完全 后期将url存入数据库 可以查看有哪些文件 选择性下载)
单文件上传部分
package com.uchain.file.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;
/**
* @author zty
* @date: 2019/8/3 12:07
* 描述:
*/
@RestController
@Slf4j
public class FileController {
@RequestMapping(value = "/upload")
@ResponseBody
public String upload(@RequestParam("test") MultipartFile file, HttpServletRequest request) {
if (file.isEmpty()) {
return "文件为空";
}
/**
* 获取文件名
*/
String fileName = file.getOriginalFilename();
log.info("上传文件名称为:" + fileName);
/**
* 上传文件类型
*/
String suffixName = fileName.substring(fileName.lastIndexOf("."));
log.info("上传的后缀名为:" + suffixName);
/**
* 上传文件存储路径
*/
String filePath = request.getServletContext().getRealPath("//resources//upload//");
log.info("上传的路径:" + filePath);
/**
* 创建文件对象
*/
File dest = new File(filePath + fileName);
//打印上传文件地址
log.info(dest.getAbsolutePath());
//检测目录是否存在
if(!dest.getParentFile().exists()){
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
return "上传成功!";
}
catch (IllegalStateException e){
e.printStackTrace();
}
catch (IOException e){
e.printStackTrace();
}
return "上传失败";
}
后面的工作就是把这些url存入数据库 方便使用
多文件上传部分(都是写在同一个controller下)
@ResponseBody
@PostMapping("/batch/upload")
public String handleFileUpload(HttpServletRequest request){
String realPath = request.getServletContext().getRealPath("//resources//uploadmulti//");
log.info("多文件上传路径:"+ realPath);
File dest = new File(realPath);
//检测目录是否存在
if (!dest.exists()) {
dest.mkdirs();
}
List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("file");
MultipartFile file = null;
BufferedOutputStream stream = null;
for (int i =0;i<files.size();++i){
file = files.get(i);
if(!file.isEmpty()){
try {
byte[] bytes = file.getBytes();
stream = new BufferedOutputStream(new FileOutputStream(new File(realPath + file.getOriginalFilename())));
stream.write(bytes);
stream.close();
} catch (Exception e) {
// e.printStackTrace();
stream = null;
return "上传失败"+i+"=>"+e.getMessage();
}
}else{
return "上传失败"+i+",文件为空";
}
}
return "上传成功";
}
同样是打印地址
然后再附上简单的html(我放在resources/templates下)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件上传</title>
</head>
<body>
<form action="/upload" method="POST" enctype="multipart/form-data">
文件:<input type="file" name="test"/>
<input type="submit"/>
</form>
<p>多文件上传</p>
<form method="POST" enctype="multipart/form-data" action="/batch/upload">
<p>文件1:<input type="file" name="file"/></p>
<p>文件2:<input type="file" name="file"/></p>
<p><input type="submit" value="上传"/></p>
</form>
</body>
</html>
暂时就这么多。。
后面再补充下载和与数据库连接的