前台
参考:漂亮的文件上传控件input file css样式整理;
后台
代码如下:
package com.aitlp.dataArchives.controller;
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.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
/**
* Spring Boot 文件上传下载
* @author 四个空格-https://www.yomige.org
*/
@RestController
@RequestMapping(value = "/file")
public class FileController {
/**
* 多文件上传
*
* @param file
* @return
* @throws Exception
*/
@PostMapping("/bacthFileUpload")
public Map bacthFileUpload(@RequestParam("file") MultipartFile[] file) {
Map resultMap = new HashMap();
resultMap.put("code", "0");
resultMap.put("msg", "success");
for (MultipartFile multipartFile : file) {
singleFileUpload(multipartFile);
}
return resultMap;
}
/**
* 单个文件上传
*
* @param file
* @return
* @throws Exception
*/
@PostMapping("/singleFileUpload")
public Map singleFileUpload(MultipartFile file) {
Map resultMap = new HashMap();
resultMap.put("code", "200");
resultMap.put("msg", "success");
// 获取上传文件路径
String originalFilename = file.getOriginalFilename();
// 获取上传文件的后缀
String fileSuffix = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
// 上传路径
String uploadPath = "F:\\upload\\";
// 上传文件名
String fileName = new Date().getTime() + new Random().nextInt(100) + "." + fileSuffix;
File savefile = new File(uploadPath + fileName);
if (!savefile.getParentFile().exists()) {
savefile.getParentFile().mkdirs();
}
try {
file.transferTo(savefile);
} catch (IOException e) {
resultMap.put("code", "500");
resultMap.put("msg", "error");
e.printStackTrace();
}
return resultMap;
}
/**
* 文件下载
*
* @param request
* @param response
* @return
*/
@GetMapping("/downloadFile")
public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
String fileName = request.getParameter("fileName");
//设置文件路径,这里写死,下载F盘的1557803382139.jpg
File file = new File("F://upload//" + fileName);
//File file = new File(realPath , fileName);
if (file.exists()) {
response.setContentType("application/force-download");// 设置强制下载不打开
response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
byte[] buffer = new byte[1024];
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
fileInputStream = new FileInputStream(file);
bufferedInputStream = new BufferedInputStream(fileInputStream);
OutputStream os = response.getOutputStream();
int i = bufferedInputStream.read(buffer);
while (i != -1) {
os.write(buffer, 0, i);
i = bufferedInputStream.read(buffer);
}
return "下载成功";
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bufferedInputStream != null) {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
return "下载失败";
}
}
参考文章:
最新评论
我的是ipv4网络,如何使用直播源啊!
我今天试了,不想啊,我的是新疆昌吉移动的网络。
收不到验证码电报
现在充值29起了