|
|
|
@ -9,16 +9,22 @@ import com.heai.common.utils.Query; |
|
|
|
import com.heai.modules.oss.dao.SysOssDao; |
|
|
|
import com.heai.modules.oss.entity.SysOssEntity; |
|
|
|
import com.heai.modules.oss.service.SysOssService; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang.StringUtils; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.*; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Service("sysOssService") |
|
|
|
public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> implements SysOssService { |
|
|
|
|
|
|
|
@Value("${ftp.location}") |
|
|
|
private String ftpLocation; |
|
|
|
@Override |
|
|
|
public PageUtils queryPage(Map<String, Object> params) { |
|
|
|
IPage<SysOssEntity> page = this.page( |
|
|
|
@ -34,4 +40,101 @@ public class SysOssServiceImpl extends ServiceImpl<SysOssDao, SysOssEntity> impl |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void downLoadObjectFile(Long id, HttpServletResponse response) throws IOException { |
|
|
|
//处理路径和名称 |
|
|
|
// List<SysOssEntity> getFileData = eamObjectMapper.getFileData(id); |
|
|
|
SysOssEntity resultData =getById(id); |
|
|
|
if(resultData == null){ |
|
|
|
throw new RuntimeException("该文件不存在,请刷新列表"); |
|
|
|
} |
|
|
|
String fileAddress=ftpLocation+"/"+resultData.getUrl()+"/"+resultData.getNewFileName(); |
|
|
|
fileAddress=fileAddress.replace("/","\\"); |
|
|
|
log.info("下载文件地址:" + fileAddress); |
|
|
|
File file = new File(fileAddress); |
|
|
|
//读取缓存1kb |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
FileInputStream fis = null; |
|
|
|
BufferedInputStream bis = null; |
|
|
|
FileOutputStream fos = null; |
|
|
|
BufferedOutputStream bos = null; |
|
|
|
response.setContentType("application/force-download;charset=utf-8"); |
|
|
|
String encodedFileName = new String(resultData.getFileName().getBytes("gbk"), "iso8859-1"); |
|
|
|
response.setHeader("Content-disposition", "attachment; filename=\"" + encodedFileName + "\""); |
|
|
|
// log.info("下载文件名称:" + encodedFileName); |
|
|
|
//response.setHeader("Content-disposition", "attachment; filename=" + new String(resultData.getFileName().getBytes("UTF-8"),"ISO-8859-1")); |
|
|
|
// response.setHeader("Content-disposition", "attachment; filename=\"" + resultData.getFileName() + "\""); |
|
|
|
try { |
|
|
|
//读取文件 |
|
|
|
fis = new FileInputStream(file); |
|
|
|
//加缓存 |
|
|
|
bis = new BufferedInputStream(fis); |
|
|
|
//开始读取 |
|
|
|
int i = bis.read(buffer); |
|
|
|
//设置输出流 |
|
|
|
ServletOutputStream os = response.getOutputStream(); |
|
|
|
//开始循环读取 |
|
|
|
while (i != -1) { |
|
|
|
os.write(buffer, 0, i); |
|
|
|
i = bis.read(buffer); |
|
|
|
} |
|
|
|
os.close(); |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException(e.getMessage()); |
|
|
|
}finally { |
|
|
|
if(bis != null) { |
|
|
|
bis.close(); |
|
|
|
} |
|
|
|
if(fis != null) { |
|
|
|
fis.close(); |
|
|
|
} |
|
|
|
|
|
|
|
if(bos != null) { |
|
|
|
bos.close(); |
|
|
|
} |
|
|
|
if(fos != null) { |
|
|
|
fos.close(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public void downLoadObjectFile2(Long id, HttpServletResponse response) throws IOException { |
|
|
|
//处理路径和名称 |
|
|
|
// List<SysOssEntity> getFileData = eamObjectMapper.getFileData(id); |
|
|
|
SysOssEntity resultData =getById(id); |
|
|
|
if(resultData == null){ |
|
|
|
throw new RuntimeException("该文件不存在,请刷新列表"); |
|
|
|
} |
|
|
|
String fileAddress=ftpLocation+"/"+resultData.getUrl()+"/"+resultData.getNewFileName(); |
|
|
|
fileAddress=fileAddress.replace("/","\\"); |
|
|
|
log.info("下载文件地址:" + fileAddress); |
|
|
|
File file = new File(fileAddress); |
|
|
|
try { |
|
|
|
if (file.exists()) { |
|
|
|
// 设置响应头 |
|
|
|
response.setContentType("application/octet-stream"); |
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + resultData.getFileName()); |
|
|
|
|
|
|
|
// 读取文件并输出到响应流 |
|
|
|
FileInputStream fis = new FileInputStream(file); |
|
|
|
OutputStream os = response.getOutputStream(); |
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
int bytesRead; |
|
|
|
while ((bytesRead = fis.read(buffer)) != -1) { |
|
|
|
os.write(buffer, 0, bytesRead); |
|
|
|
} |
|
|
|
fis.close(); |
|
|
|
os.close(); |
|
|
|
} else { |
|
|
|
throw new RuntimeException("该文件不存在!"); |
|
|
|
} |
|
|
|
} catch (Exception e) { |
|
|
|
throw new RuntimeException(e.getMessage()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |