|
|
@ -3,12 +3,16 @@ package com.yxt.demo.system.biz.sys_resources; |
|
|
|
import com.yxt.demo.system.api.sys_resources.SysResources; |
|
|
|
import com.yxt.demo.system.jdbc.service.MybatisBaseService; |
|
|
|
import com.yxt.demo.system.utils.ResultBean; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.stereotype.Service; |
|
|
|
import org.springframework.web.multipart.MultipartFile; |
|
|
|
|
|
|
|
import javax.servlet.ServletOutputStream; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import java.io.File; |
|
|
|
import java.util.UUID; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.net.URLEncoder; |
|
|
|
|
|
|
|
/** |
|
|
|
* @Author dimengzhe |
|
|
@ -18,19 +22,53 @@ import java.util.UUID; |
|
|
|
@Service |
|
|
|
public class SysResourcesService extends MybatisBaseService<SysResourcesMapper, SysResources> { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private SysResourcesMapper sysResourcesMapper; |
|
|
|
|
|
|
|
@Value("${reggie.path}") |
|
|
|
private String basePath; |
|
|
|
|
|
|
|
public ResultBean upload(MultipartFile file) { |
|
|
|
String filename = file.getOriginalFilename(); |
|
|
|
String substring = filename.substring(filename.lastIndexOf(".")); |
|
|
|
String fileName = UUID.randomUUID().toString() + substring; |
|
|
|
public ResultBean upload(MultipartFile multipartFile, String type) { |
|
|
|
ResultBean rb = ResultBean.fireFail(); |
|
|
|
String filename = multipartFile.getOriginalFilename(); |
|
|
|
// int startIndex = filename.replaceAll("\\\\", "/").lastIndexOf("/");
|
|
|
|
// String substring = filename.substring(startIndex + 1).substring(0, filename.indexOf("."));
|
|
|
|
File file1 = new File(basePath); |
|
|
|
if (!file1.exists()){ |
|
|
|
file1.mkdirs(); |
|
|
|
} |
|
|
|
try { |
|
|
|
file.transferTo(new File(basePath+fileName)); |
|
|
|
multipartFile.transferTo(new File(basePath+filename)); |
|
|
|
SysResources sysResources = new SysResources(); |
|
|
|
sysResources.setFilePath(basePath+filename); |
|
|
|
sysResources.setContent(filename); |
|
|
|
sysResources.setType(type); |
|
|
|
int insert = sysResourcesMapper.insert(sysResources); |
|
|
|
if (insert == 0){ |
|
|
|
return rb.setMsg("上传失败"); |
|
|
|
} |
|
|
|
}catch (Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
return rb.setMsg("上传失败"); |
|
|
|
} |
|
|
|
return rb.success(); |
|
|
|
} |
|
|
|
|
|
|
|
public ResultBean download(String name, HttpServletResponse response) { |
|
|
|
try { |
|
|
|
FileInputStream fileInputStream = new FileInputStream(new File(basePath + name)); |
|
|
|
ServletOutputStream outputStream = response.getOutputStream(); |
|
|
|
response.setContentType("application/octet-stream;charset=UTF-8"); |
|
|
|
response.setCharacterEncoding("UTF-8"); |
|
|
|
response.setHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(name, "UTF-8")); |
|
|
|
int len = 0; |
|
|
|
byte[] bytes = new byte[1024]; |
|
|
|
while ((len = fileInputStream.read(bytes)) != -1){ |
|
|
|
outputStream.write(bytes,0,len); |
|
|
|
outputStream.flush(); |
|
|
|
} |
|
|
|
outputStream.close(); |
|
|
|
fileInputStream.close(); |
|
|
|
}catch (Exception e){ |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|