1、jsp使用
2、controller
package com.graduation.controller;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* @author K
*/
@Controller
@RequestMapping("/file")
public class FileController {
@RequestMapping(value = "/download",method = RequestMethod.GET)
private ResponseEntity FileDownload(HttpServletRequest request, @RequestParam(value = "filename",required = true) String filename, Model model)throws IOException {
String path = request.getServletContext().getRealPath("");
String downloadFilePath=path + "\\static\\datafile";//从我们的上传文件夹中去取
filename = filename+".zip";
File file = new File(downloadFilePath+File.separator+filename);//新建一个文件
HttpHeaders headers = new HttpHeaders();//http头信息
String downloadFileName = new String(filename.getBytes("UTF-8"),"iso-8859-1");//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
return new ResponseEntity(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
@RequestMapping(value = "/templateDownload",method = RequestMethod.GET)
private ResponseEntity TemplateDownload(HttpServletRequest request, @RequestParam(value = "filename",required = true) String filename, Model model)throws IOException {
String path = request.getServletContext().getRealPath("");
String downloadFilePath=path + "\\static\\template";//从我们的上传文件夹中去取
filename = filename+".xlsx";
File file = new File(downloadFilePath+File.separator+filename);//新建一个文件
HttpHeaders headers = new HttpHeaders();//http头信息
String downloadFileName = new String(filename.getBytes("UTF-8"),"iso-8859-1");//设置编码
headers.setContentDispositionFormData("attachment", downloadFileName);
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
//MediaType:互联网媒介类型 contentType:具体请求中的媒体类型信息
return new ResponseEntity(FileUtils.readFileToByteArray(file),headers, HttpStatus.CREATED);
}
@ResponseBody
@RequestMapping("/upload")
public Map uploadFile(MultipartFile coverImg,HttpServletRequest request) throws IllegalStateException, IOException {
// 原始名称
String oldFileName;
oldFileName = coverImg.getOriginalFilename();
// 上传图片
if (coverImg != null && oldFileName != null && oldFileName.length() > 0) {
// 新的图片名称
String newFileName = oldFileName;
//存放地址
String saveFilePath = request.getServletContext().getRealPath("")+"\\static\\imgs\\courseImg\\"+newFileName;
// 新图片
File newFile = new File(saveFilePath);
// 将内存中的数据写入磁盘
coverImg.transferTo(newFile);
// 将新图片名称返回到前端
Map map = new HashMap();
map.put("success", "成功啦");
map.put("url", newFileName);
return map;
} else {
Map map = new HashMap();
map.put("error", "图片不合法");
return map;
}
}
@ResponseBody
@RequestMapping("/courseUpload")
public Map uploadCourseFile(MultipartFile file,HttpServletRequest request) throws IllegalStateException, IOException {
// 原始名称
String oldFileName;
oldFileName = file.getOriginalFilename();
String suffix = oldFileName.substring(oldFileName.indexOf(".")+1);
// 上传图片
if (file != null && oldFileName != null && oldFileName.length() > 0) {
// 新的图片名称
String newFileName = oldFileName;
String saveFilePath;
//存放地址
if(suffix.equals("docx") || suffix.equals("doc")){
saveFilePath = request.getServletContext().getRealPath("")+"\\static\\exercises\\"+newFileName;
}else if(suffix.equals("zip")||suffix.equals("rar")){
saveFilePath = request.getServletContext().getRealPath("")+"\\static\\datafile\\"+newFileName;
}else{
saveFilePath = request.getServletContext().getRealPath("")+"\\static\\videos\\"+newFileName;
}
// 新图片
File newFile = new File(saveFilePath);
// 将内存中的数据写入磁盘
file.transferTo(newFile);
// 将新图片名称返回到前端
Map map = new HashMap();
map.put("success", "成功啦");
map.put("url", newFileName);
return map;
} else {
Map map = new HashMap();
map.put("error", "图片不合法");
return map;
}
}
@ResponseBody
@RequestMapping("/planfile")
public Map uploadPlane(MultipartFile planfile,HttpServletRequest request) throws IllegalStateException, IOException {
// 原始名称
String oldFileName;
oldFileName = planfile.getOriginalFilename();
// 上传图片
if (planfile != null && oldFileName != null && oldFileName.length() > 0) {
// 新的图片名称
String newFileName = oldFileName;
//存放地址
String saveFilePath = request.getServletContext().getRealPath("")+"\\static\\teachPlans\\"+newFileName;
// 新图片
File newFile = new File(saveFilePath);
// 将内存中的数据写入磁盘
planfile.transferTo(newFile);
// 将新图片名称返回到前端
Map map = new HashMap();
map.put("success", "成功啦");
map.put("url", newFileName);
return map;
} else {
Map map = new HashMap();
map.put("error", "图片不合法");
return map;
}
}
@ResponseBody
@RequestMapping("/bookUpload")
public Map uploadBook(MultipartFile bookfile,HttpServletRequest request) throws IllegalStateException, IOException {
// 原始名称
String oldFileName;
System.out.println("原始名称:"+bookfile.getOriginalFilename());
oldFileName = bookfile.getOriginalFilename();
// 上传图片
if (bookfile != null && oldFileName != null && oldFileName.length() > 0) {
// 新的图片名称
String newFileName = oldFileName;
//存放地址
String saveFilePath = request.getServletContext().getRealPath("")+"\\static\\chapterPdf\\"+newFileName;
// 新图片
File newFile = new File(saveFilePath);
// 将内存中的数据写入磁盘
bookfile.transferTo(newFile);
// 将新图片名称返回到前端
Map map = new HashMap();
map.put("success", "成功啦");
map.put("url", newFileName);
return map;
} else {
Map map = new HashMap();
map.put("error", "图片不合法");
return map;
}
}
}