|
|
@ -1,20 +1,28 @@ |
|
|
|
package com.huoran.iasf.service.impl; |
|
|
|
package com.huoran.iasf.service.impl; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil; |
|
|
|
|
|
|
|
import cn.hutool.core.io.IoUtil; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
|
|
|
import com.huoran.iasf.common.config.FileUploadProperties; |
|
|
|
import com.huoran.iasf.common.config.FileUploadProperties; |
|
|
|
import com.huoran.iasf.common.exception.BusinessException; |
|
|
|
import com.huoran.iasf.common.exception.BusinessException; |
|
|
|
import com.huoran.iasf.common.utils.R; |
|
|
|
|
|
|
|
import com.huoran.iasf.common.utils.DateUtils; |
|
|
|
import com.huoran.iasf.common.utils.DateUtils; |
|
|
|
|
|
|
|
import com.huoran.iasf.common.utils.R; |
|
|
|
import com.huoran.iasf.entity.SysFilesEntity; |
|
|
|
import com.huoran.iasf.entity.SysFilesEntity; |
|
|
|
import com.huoran.iasf.mapper.SysFilesMapper; |
|
|
|
import com.huoran.iasf.mapper.SysFilesMapper; |
|
|
|
import com.huoran.iasf.service.SysFilesService; |
|
|
|
import com.huoran.iasf.service.SysFilesService; |
|
|
|
|
|
|
|
import com.huoran.iasf.vo.req.FileParameters; |
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
import org.apache.commons.io.FileUtils; |
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties; |
|
|
|
|
|
|
|
import org.springframework.http.MediaType; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
|
|
|
|
import javax.annotation.Resource; |
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.File; |
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
|
|
|
|
import java.net.URLEncoder; |
|
|
|
import java.util.*; |
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
@ -27,11 +35,12 @@ import java.util.*; |
|
|
|
@EnableConfigurationProperties(FileUploadProperties.class) |
|
|
|
@EnableConfigurationProperties(FileUploadProperties.class) |
|
|
|
@Service("sysFilesService") |
|
|
|
@Service("sysFilesService") |
|
|
|
public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEntity> implements SysFilesService { |
|
|
|
public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEntity> implements SysFilesService { |
|
|
|
|
|
|
|
|
|
|
|
@Resource |
|
|
|
@Resource |
|
|
|
private FileUploadProperties fileUploadProperties; |
|
|
|
private FileUploadProperties fileUploadProperties; |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public R saveFile(MultipartFile file) { |
|
|
|
public R saveFile(MultipartFile file, FileParameters fileParameters) { |
|
|
|
//存储文件夹
|
|
|
|
//存储文件夹
|
|
|
|
String createTime = DateUtils.format(new Date(), DateUtils.DATEPATTERN); |
|
|
|
String createTime = DateUtils.format(new Date(), DateUtils.DATEPATTERN); |
|
|
|
String newPath = fileUploadProperties.getPath() + createTime + File.separator; |
|
|
|
String newPath = fileUploadProperties.getPath() + createTime + File.separator; |
|
|
@ -45,10 +54,14 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt |
|
|
|
} |
|
|
|
} |
|
|
|
try { |
|
|
|
try { |
|
|
|
String fileName = file.getOriginalFilename(); |
|
|
|
String fileName = file.getOriginalFilename(); |
|
|
|
|
|
|
|
//获取文件大小
|
|
|
|
|
|
|
|
int fileSize = (int) file.getSize(); |
|
|
|
|
|
|
|
//获取文件的后缀名
|
|
|
|
|
|
|
|
String suffixName = fileName.substring(fileName.lastIndexOf(".")+1); |
|
|
|
//id与filename保持一直,删除文件
|
|
|
|
//id与filename保持一直,删除文件
|
|
|
|
String fileNameNew = UUID.randomUUID().toString().replace("-", "") + getFileType(fileName); |
|
|
|
String fileNameNew = UUID.randomUUID().toString().replace("-", "") + getFileType(fileName); |
|
|
|
String newFilePathName = newPath + fileNameNew; |
|
|
|
String newFilePathName = newPath + fileNameNew; |
|
|
|
String url = fileUploadProperties.getUrl() + "/" + createTime + "/" + fileNameNew; |
|
|
|
// String url = fileUploadProperties.getUrl() + "/" + createTime + "/" + fileNameNew;
|
|
|
|
//创建输出文件对象
|
|
|
|
//创建输出文件对象
|
|
|
|
File outFile = new File(newFilePathName); |
|
|
|
File outFile = new File(newFilePathName); |
|
|
|
//拷贝文件到输出文件对象
|
|
|
|
//拷贝文件到输出文件对象
|
|
|
@ -57,10 +70,17 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt |
|
|
|
SysFilesEntity sysFilesEntity = new SysFilesEntity(); |
|
|
|
SysFilesEntity sysFilesEntity = new SysFilesEntity(); |
|
|
|
sysFilesEntity.setFileName(fileName); |
|
|
|
sysFilesEntity.setFileName(fileName); |
|
|
|
sysFilesEntity.setFilePath(newFilePathName); |
|
|
|
sysFilesEntity.setFilePath(newFilePathName); |
|
|
|
sysFilesEntity.setUrl(url); |
|
|
|
// sysFilesEntity.setUrl(url);
|
|
|
|
|
|
|
|
sysFilesEntity.setFileSize(fileSize); |
|
|
|
|
|
|
|
sysFilesEntity.setFormat(suffixName); |
|
|
|
|
|
|
|
sysFilesEntity.setUploader(fileParameters.getUploader()); |
|
|
|
|
|
|
|
sysFilesEntity.setQuote(fileParameters.getQuote()); |
|
|
|
|
|
|
|
sysFilesEntity.setSite(fileParameters.getSite()); |
|
|
|
|
|
|
|
sysFilesEntity.setType(fileParameters.getType()); |
|
|
|
this.save(sysFilesEntity); |
|
|
|
this.save(sysFilesEntity); |
|
|
|
|
|
|
|
String url = fileUploadProperties.getUrl() + "/preview" + "/" + sysFilesEntity.getId(); |
|
|
|
Map<String, String> resultMap = new HashMap<>(); |
|
|
|
Map<String, String> resultMap = new HashMap<>(); |
|
|
|
resultMap.put("src", url); |
|
|
|
resultMap.put("url", url); |
|
|
|
return R.success(resultMap); |
|
|
|
return R.success(resultMap); |
|
|
|
} catch (Exception e) { |
|
|
|
} catch (Exception e) { |
|
|
|
throw new BusinessException("上传文件失败"); |
|
|
|
throw new BusinessException("上传文件失败"); |
|
|
@ -68,7 +88,80 @@ public class SysFilesServiceImpl extends ServiceImpl<SysFilesMapper, SysFilesEnt |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
@Override |
|
|
|
public void removeByIdsAndFiles(List<String> ids) { |
|
|
|
public void preview(Integer id, HttpServletRequest req, HttpServletResponse res) { |
|
|
|
|
|
|
|
if (id != null) { |
|
|
|
|
|
|
|
SysFilesEntity entity = this.getById(id); |
|
|
|
|
|
|
|
String path = entity.getFilePath(); |
|
|
|
|
|
|
|
res.setContentLength(entity.getFileSize()); |
|
|
|
|
|
|
|
File file = new File(path); |
|
|
|
|
|
|
|
byte[] buffer = FileUtil.readBytes(path); |
|
|
|
|
|
|
|
if (file.exists()) { |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
//设置contentType
|
|
|
|
|
|
|
|
res.setContentType(MediaType.ALL_VALUE); |
|
|
|
|
|
|
|
res.setCharacterEncoding("UTF-8"); |
|
|
|
|
|
|
|
//获取outputStream
|
|
|
|
|
|
|
|
ServletOutputStream outputStream = res.getOutputStream(); |
|
|
|
|
|
|
|
//输出
|
|
|
|
|
|
|
|
IoUtil.write(outputStream, true, buffer); |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void download(Integer id, HttpServletRequest req, HttpServletResponse res) { |
|
|
|
|
|
|
|
if (id != null) { |
|
|
|
|
|
|
|
SysFilesEntity entity = this.getById(id); |
|
|
|
|
|
|
|
String path = entity.getFilePath(); |
|
|
|
|
|
|
|
setDownloadContent(entity.getFileName(), req, res); |
|
|
|
|
|
|
|
// 重要,需要设置此值,否则下载后打开文件会提示文件需要修复
|
|
|
|
|
|
|
|
res.setContentLength(entity.getFileSize()); |
|
|
|
|
|
|
|
File file = new File(path); |
|
|
|
|
|
|
|
if (file.exists()) { |
|
|
|
|
|
|
|
byte[] buffer = new byte[1024]; |
|
|
|
|
|
|
|
//输出流
|
|
|
|
|
|
|
|
OutputStream os; |
|
|
|
|
|
|
|
try (FileInputStream fis = new FileInputStream(file); |
|
|
|
|
|
|
|
BufferedInputStream bis = new BufferedInputStream(fis);) { |
|
|
|
|
|
|
|
os = res.getOutputStream(); |
|
|
|
|
|
|
|
int i = bis.read(buffer); |
|
|
|
|
|
|
|
while (i != -1) { |
|
|
|
|
|
|
|
os.write(buffer, 0, i); |
|
|
|
|
|
|
|
i = bis.read(buffer); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (Exception e) { |
|
|
|
|
|
|
|
log.error(e.getMessage(), e); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* 客户端下载文件上response header的设置。 |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param fileName 文件名 |
|
|
|
|
|
|
|
* @param request 请求 |
|
|
|
|
|
|
|
* @param response 响应 |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
private void setDownloadContent(String fileName, HttpServletRequest request, HttpServletResponse response) { |
|
|
|
|
|
|
|
String agent = request.getHeader("User-Agent"); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
if (null != agent && agent.toUpperCase().indexOf("MSIE") > 0) { |
|
|
|
|
|
|
|
fileName = URLEncoder.encode(fileName, "UTF-8"); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
fileName = new String(fileName.getBytes("UTF-8"), "ISO8859-1"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} catch (UnsupportedEncodingException e1) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
response.setContentType("application/x-msdownload;"); |
|
|
|
|
|
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fileName); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
|
|
|
|
public void removeByIdsAndFiles(List<Integer> ids) { |
|
|
|
List<SysFilesEntity> list = this.listByIds(ids); |
|
|
|
List<SysFilesEntity> list = this.listByIds(ids); |
|
|
|
list.forEach(entity -> { |
|
|
|
list.forEach(entity -> { |
|
|
|
//如果之前的文件存在,删除
|
|
|
|
//如果之前的文件存在,删除
|
|
|
|