package springboot_001.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springboot_001.config.R;
import springboot_001.dao.Dao;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
@Controller
@RequestMapping("/ows/test")
public class FileController {
private static String SAVE_FILE_DIR = "./files";
@Autowired
Dao dao;
@PostMapping("/upload")
public @ResponseBody
Map<String, Object> uploadFile(
@RequestParam("file") MultipartFile file, @RequestParam("instanceId") String instanceId,
@RequestParam("fieldName") String fieldName) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
File dir = new File(SAVE_FILE_DIR + "/" + sdf.format(new Date()));
if (!dir.exists()) {
dir.mkdirs();
}
String name = file.getOriginalFilename();
String extName = "";
if (name.contains(".")) {
extName = name.substring(name.lastIndexOf(".") + 1, name.length());
}
String saveFileName = UUID.randomUUID().toString() + "." + extName;
String savePath = dir.getPath() + File.separator + saveFileName;
String saveLocalPath = dir.getCanonicalPath() + File.separator + saveFileName;
file.transferTo(new File(saveLocalPath));
Map<String, Object> fileObj = new HashMap<>();
fileObj.put("NAME", name);
fileObj.put("EXT_NAME", extName);
fileObj.put("SAVE_PATH", savePath);
fileObj.put("FILE_SIZE", file.getSize());
fileObj.put("INSTANCE_ID", instanceId);
fileObj.put("FIELD_NAME", fieldName);
dao.saveOrUpdate(R.FILE, fileObj);
return fileObj;
}
@GetMapping("/download/{fileId}")
public void download(@PathVariable("fileId") String fileId,
HttpServletResponse response) throws Exception {
Map<String, Object> fileObj = dao.findById2Map(R.FILE, fileId, null);
String fileName = (String) fileObj.get("NAME");
String savePath = (String) fileObj.get("SAVE_PATH");
String extName = (String) fileObj.get("EXT_NAME");
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(savePath))) {
fileName = URLEncoder.encode(fileName, "UTF-8");// IE浏览器
// 清空response
response.reset();
// 设置response的Header
response.addHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
if ("xls".equals(extName) ||
"xlsx".equals(extName)) {
response.setContentType("application/vnd.ms-excel;charset=utf-8");
} else if ("doc".equals(extName) ||
"docx".equals(extName)) {
response.setContentType("application/msword;charset=utf-8");
} else if ("ppt".equals(extName) ||
"pptx".equals(extName)) {
response.setContentType("application/vnd.ms-powerpoint;charset=utf-8");
} else if ("pdf".equals(extName)) {
response.setContentType("application/pdf;charset=utf-8");
} else if ("jpg".equals(extName) ||
"jpeg".equals(extName)) {
response.setContentType("image/jpeg;charset=utf-8");
} else if ("png".equals(extName)) {
response.setContentType("image/png;charset=utf-8");
} else if ("gif".equals(extName)) {
response.setContentType("image/gif;charset=utf-8");
} else {
response.setContentType("application/octet-stream;charset=utf-8");
}
try (BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
byte[] buffered = new byte[1024];
int len;
while ((len = bis.read(buffered)) != -1) {
bos.write(buffered, 0, len);
}
}
}
}
}