|
|
@ -1,5 +1,7 @@ |
|
|
|
package org.energy.modules.system.service.impl; |
|
|
|
package org.energy.modules.system.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
|
|
|
|
|
|
|
import com.dayu.daf.core.log.exception.ServiceException; |
|
|
|
import org.energy.modules.system.entity.File; |
|
|
|
import org.energy.modules.system.entity.File; |
|
|
|
import org.energy.modules.system.vo.FileVO; |
|
|
|
import org.energy.modules.system.vo.FileVO; |
|
|
|
import org.energy.modules.system.mapper.FileMapper; |
|
|
|
import org.energy.modules.system.mapper.FileMapper; |
|
|
@ -8,6 +10,9 @@ import com.dayu.daf.core.mp.base.BaseServiceImpl; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
import com.baomidou.mybatisplus.core.metadata.IPage; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
@ -42,4 +47,48 @@ public class FileServiceImpl extends BaseServiceImpl<FileMapper, File> implement |
|
|
|
return null; |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void downloadFile(String fileName, HttpServletResponse response) { |
|
|
|
|
|
|
|
File info = super.getOne(Wrappers.<File>lambdaQuery().eq(File::getFileName, fileName), false); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (info == null) { |
|
|
|
|
|
|
|
throw new ServiceException("下载文件出错"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
String path = "." + info.getFilePath() + info.getFileName() + info.getFileSuffix(); |
|
|
|
|
|
|
|
String osName = System.getProperties().getProperty("os.name"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (osName.contains("Windows")) { |
|
|
|
|
|
|
|
path = "C:/" + path; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
java.io.File file = new java.io.File(path); |
|
|
|
|
|
|
|
if (!file.exists()) { |
|
|
|
|
|
|
|
throw new ServiceException("下载文件出错"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
String realName = info.getFileOldName(); |
|
|
|
|
|
|
|
// 3. 设置想办法让浏览器能够支持(Content-Disposition)下载我们需要的东西,中文文件名URLEncoder.encode编码,否则有可能乱码
|
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(realName.getBytes(), "ISO8859-1") + "\""); |
|
|
|
|
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
|
|
|
|
// 4. 获取下载文件的输入流
|
|
|
|
|
|
|
|
FileInputStream in = new FileInputStream(path); |
|
|
|
|
|
|
|
// 5. 创建缓冲区
|
|
|
|
|
|
|
|
int len = 0; |
|
|
|
|
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
|
|
|
|
// 6. 获取OutputStream对象
|
|
|
|
|
|
|
|
ServletOutputStream out = response.getOutputStream(); |
|
|
|
|
|
|
|
// 7. 将FileOutputStream流写入到buffer缓冲区,使用OutputStream将缓冲区中的数据输出到客户端!
|
|
|
|
|
|
|
|
while ((len = in.read(buffer)) > 0) { |
|
|
|
|
|
|
|
out.write(buffer, 0, len); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
in.close(); |
|
|
|
|
|
|
|
out.close(); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
|
|
|
throw new ServiceException("下载文件出错"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|