|
|
@@ -9,10 +9,15 @@ import cn.seecoder.courselearning.vo.ResultVO;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.core.io.Resource;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.OutputStream;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
@Service
|
|
|
public class FileServiceImpl implements FileService {
|
|
|
@@ -33,4 +38,43 @@ public class FileServiceImpl implements FileService {
|
|
|
}
|
|
|
return new ResultVO<>(-1, "服务器错误,请联系网站管理员。");
|
|
|
}
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void downloadFile(String originName, String newName, HttpServletResponse response) {
|
|
|
+ InputStream inputStream = null;
|
|
|
+ OutputStream outputStream = null;
|
|
|
+ response.setContentType("application/x-msdownload");
|
|
|
+ try {
|
|
|
+ Resource resource = FileHelper.loadFileAsResource(path, originName);
|
|
|
+ if(resource == null)
|
|
|
+ return;
|
|
|
+ inputStream = resource.getInputStream();
|
|
|
+ //1.设置文件ContentType类型
|
|
|
+ response.setContentType("application/octet-stream;charset=UTF-8");
|
|
|
+ outputStream = response.getOutputStream();
|
|
|
+ //2.转码 UTF_8为传入的newName编码的格式 ISO_8859_1为浏览器默认编码
|
|
|
+ String convertName = new String(newName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
|
|
|
+ //3.设置 header Content-Disposition
|
|
|
+ response.setHeader("Content-Disposition", "attachment; filename=" + convertName);
|
|
|
+ int b = 0;
|
|
|
+ byte[] buffer = new byte[2048];
|
|
|
+ while (b != -1) {
|
|
|
+ b = inputStream.read(buffer);
|
|
|
+ if (b != -1) {
|
|
|
+ outputStream.write(buffer, 0, b);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (IOException | MyException e) {
|
|
|
+ logger.error("文件下载时出错", e);
|
|
|
+ } finally {
|
|
|
+ try {
|
|
|
+ if(inputStream != null)
|
|
|
+ inputStream.close();
|
|
|
+ if (outputStream != null)
|
|
|
+ outputStream.close();
|
|
|
+ } catch (IOException e) {
|
|
|
+ logger.error("输入流或输出流关闭时出错!", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|