未完成,明天继续
upload.html
Title
UploadController.java
@Controller
public class UploadController {
@RequestMapping( "toUpload")
public String toUpload(){
return "upload";
}
@PostMapping("/upload")
@ResponseBody
public String upload(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return "上传失败,请选择文件";
}
String fileName = file.getOriginalFilename();
String filePath = "E:\Git_Study\test01\src\main\resources\static\upload\";
File dest = new File(filePath + fileName);
try {
file.transferTo(dest);
return "上传成功";
} catch (IOException e) {
e.printStackTrace();
}
return "上传失败!";
}
}
下载
在写下载的时候,遇到了一个特别无语的问题,如果不是用的a标签,用按钮发送请求,那么只能设置按钮的window.location.href="下载路径",或者是window.open("下载路径")
@RequestMapping("/download")
@ResponseBody
public String download(@RequestParam("fileName") String fileName, HttpServletRequest request, HttpServletResponse response){
String filePath = prefixName+fileName; // 文件的路径
System.out.println(filePath);
File file = new File(filePath);
if (!file.exists()){
return "文件失效";
}
BufferedOutputStream outputStream = null;
ServletOutputStream outputStreams = null;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
byte[] bytes = new byte[1024];
int len = 0;
response.reset();
response.setContentType("application/octet-stream; charset=utf-8");
response.setHeader("Content-Disposition", "attachment; filename="+URLEncoder.encode(fileName,"UTF-8"));//文件名称
outputStreams = response.getOutputStream();
while ((len=inputStream.read(bytes))!=-1){
outputStreams.write(bytes,0,len);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
inputStream.close();
outputStreams.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "下载成功";
}
html
在线播放的东西,在这里使用对象的媒体标签就可以了,可以使用刚刚下载的那个接口。
批量导入
忘情水 download1
download2
download3



