import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.ResourceLoader;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
@RestController
@Slf4j
@RequestMapping("/downLoadExcel")
@ApiIgnore()
public class DownLoadExcelFromBrowser
{
@Resource
private ResourceLoader resourceLoader;
@GetMapping("/downloadTemplate")
public void downloadTemplate(@RequestParam("filename") String filename, HttpServletResponse response, HttpServletRequest request) {
InputStream inputStream = null;
ServletOutputStream servletOutputStream = null;
try {
System.out.println("filename = " + filename);
// String path = "uploadAndDownload/"+filename;
// org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:"+path);
response.setContentType("application/vnd.ms-excel");
response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
response.addHeader("charset", "utf-8");
response.addHeader("Pragma", "no-cache");
String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);
// inputStream = resource.getInputStream();
String path = System.getProperty("user.dir")+"/src/main/resources/uploadAndDownload/"+filename;
path = path.replaceAll("\\\\", "/");
inputStream = new FileInputStream(path);
{
}
servletOutputStream = response.getOutputStream();
IOUtils.copy(inputStream, servletOutputStream);
response.flushBuffer();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (servletOutputStream != null) {
servletOutputStream.close();
servletOutputStream = null;
}
if (inputStream != null) {
inputStream.close();
inputStream = null;
}
// 召唤jvm的垃圾回收器
System.gc();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 功能描述: 根据路径及名称展示图片
* @Author:xuwu
* @Date: 2019/8/13 17:28
*/
@RequestMapping("showImage")
public void showImage(@RequestParam("filename") String filename,HttpServletResponse response) {
log.info("----------------------进入图片展示方法-------------");
String path = System.getProperty("user.dir")+"/src/main/resources/uploadAndDownload/"+filename+".jpg";
path = path.replaceAll("\\\\", "/");
File file=new File(path);
ServletOutputStream out=null;
try {
FileInputStream instream=new FileInputStream(file);
byte[] b=new byte[1024];
int length=0;
BufferedInputStream buf=new BufferedInputStream(instream);
out=response.getOutputStream();
BufferedOutputStream bot=new BufferedOutputStream(out);
while((length=buf.read(b))!=-1) {
bot.write(b,0, b.length);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}