package com.ruoyi.service.impl;
|
|
import com.ruoyi.common.core.domain.AjaxResult;
|
import com.ruoyi.service.DownLoadFileService;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.io.*;
|
|
/**
|
* @Version 1.0
|
* @Author Jin_quan Ou
|
* @Date 2023-03-19 17:33
|
*/
|
@Service
|
public class DownLoadFileServiceImpl implements DownLoadFileService {
|
|
@Value("${ruoyi.profile}")
|
private String basePath;
|
|
@Override
|
public void downLoadFile(String path, HttpServletResponse response) {
|
|
path=basePath+path.substring(8);
|
File file = new File(path);
|
byte[] buffer = new byte[1024];
|
BufferedInputStream bis = null;
|
OutputStream os = null;
|
try {
|
//文件是否存在
|
if (file.exists()) {
|
//设置响应
|
response.setContentType("application/octet-stream;charset=UTF-8");
|
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
|
response.setHeader("Content-Disposition","attachment;filename=");
|
response.setCharacterEncoding("UTF-8");
|
os = response.getOutputStream();
|
bis = new BufferedInputStream(new FileInputStream(file));
|
while(bis.read(buffer) != -1){
|
os.write(buffer);
|
}
|
}
|
}catch (Exception e) {
|
e.printStackTrace();
|
} finally {
|
try {
|
if(bis != null) {
|
bis.close();
|
}
|
if(os != null) {
|
os.flush();
|
os.close();
|
}
|
} catch (IOException e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
}
|