|
|
@ -1,5 +1,6 @@
|
|
|
|
package com.yhjd.controller;
|
|
|
|
package com.yhjd.controller;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.yhjd.util.DownloadUtil;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import io.swagger.annotations.Api;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
@ -36,7 +37,7 @@ public class UploadController {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
file.transferTo(dest);
|
|
|
|
file.transferTo(dest);
|
|
|
|
LOGGER.info("上传成功");
|
|
|
|
LOGGER.info("上传成功");
|
|
|
|
return "上传成功";
|
|
|
|
return fileName;
|
|
|
|
} catch (IOException e) {
|
|
|
|
} catch (IOException e) {
|
|
|
|
LOGGER.error(e.toString(), e);
|
|
|
|
LOGGER.error(e.toString(), e);
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -69,40 +70,11 @@ public class UploadController {
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@PostMapping("/download")
|
|
|
|
@GetMapping("/download")
|
|
|
|
@ResponseBody
|
|
|
|
@ResponseBody
|
|
|
|
public void download(HttpServletRequest request, HttpServletResponse response)
|
|
|
|
public void download(String fileName,HttpServletRequest request, HttpServletResponse response)
|
|
|
|
throws ServletException, IOException {
|
|
|
|
throws ServletException, IOException {
|
|
|
|
//获取下载文件,绝对路径
|
|
|
|
|
|
|
|
String fileName = request.getParameter("fileName");
|
|
|
|
|
|
|
|
String filePath = System.getProperty("user.home") + File.separator + "img" + File.separator;
|
|
|
|
String filePath = System.getProperty("user.home") + File.separator + "img" + File.separator;
|
|
|
|
//解码
|
|
|
|
DownloadUtil.downloadFile(filePath+fileName,fileName,response,request);
|
|
|
|
fileName = URLDecoder.decode(fileName, "UTF-8");
|
|
|
|
|
|
|
|
//文件名
|
|
|
|
|
|
|
|
String realName = fileName.substring(fileName.lastIndexOf("/")+1);
|
|
|
|
|
|
|
|
System.out.println(fileName+"--->"+realName);
|
|
|
|
|
|
|
|
File file = new File(filePath+fileName);
|
|
|
|
|
|
|
|
if(!file.exists()){
|
|
|
|
|
|
|
|
request.setAttribute("message", "资源已删除");
|
|
|
|
|
|
|
|
request.getRequestDispatcher("/WEB-INF/download.jsp").forward(request, response);
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
//设置响应头,控制浏览器下载该文件
|
|
|
|
|
|
|
|
realName = new String(realName.getBytes("UTF-8"),"ISO-8859-1");
|
|
|
|
|
|
|
|
response.setHeader("content-disposition", "attachment;filename=" + realName);
|
|
|
|
|
|
|
|
//response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(realName, "UTF-8")); //编码
|
|
|
|
|
|
|
|
//读取要下载的文件,保存到文件输入流
|
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(fileName);
|
|
|
|
|
|
|
|
//输出流
|
|
|
|
|
|
|
|
OutputStream out = response.getOutputStream();
|
|
|
|
|
|
|
|
//缓冲区
|
|
|
|
|
|
|
|
byte buffer[] = new byte[4096];
|
|
|
|
|
|
|
|
int len = 0;
|
|
|
|
|
|
|
|
while((len=in.read(buffer))>0){
|
|
|
|
|
|
|
|
out.write(buffer, 0, len);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
in.close();
|
|
|
|
|
|
|
|
out.close();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|