提交 507fc9a3 编写于 作者: sq3536's avatar sq3536

网盘

上级 456fdd2c
package cn.ibizlab.core.disk.extensions.service;
import cn.ibizlab.core.disk.domain.SDFile;
import cn.ibizlab.core.disk.service.ISDFileService;
import cn.ibizlab.util.domain.FileItem;
import cn.ibizlab.util.errors.InternalServerErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.FileCopyUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
@Primary
@Slf4j
@Service
public class DiskCoreService {
@Value("${ibiz.filePath:/app/file/}")
private String fileRoot;
private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyMMdd");
@Autowired
private ISDFileService sdFileService;
public FileItem saveFile(MultipartFile multipartFile) {
return saveFile("",multipartFile);
}
public FileItem saveFile(String folder,MultipartFile multipartFile) {
return saveFile(folder,"",multipartFile);
}
public FileItem saveFile(String folder,String fileId,MultipartFile multipartFile) {
SDFile sdFile = new SDFile();
if(!StringUtils.isEmpty(fileId))
sdFile.setId(fileId);
if(!StringUtils.isEmpty(folder))
sdFile.setId(folder);
return saveFile(sdFile,multipartFile);
}
public FileItem saveFile(SDFile sdFile,MultipartFile multipartFile) {
FileItem item=null;
try {
String fileName = multipartFile.getOriginalFilename();
sdFile.setName(fileName);
String extension="."+getExtensionName(fileName);
sdFile.setExtension(extension);
String digestCode = DigestUtils.md5DigestAsHex(multipartFile.getInputStream());
sdFile.setDigestCode(digestCode);
String fileId = sdFile.getId();
if(StringUtils.isEmpty(fileId))
{
fileId = simpleDateFormat.format(new Date()).concat("-").concat(digestCode);
sdFile.setId(fileId);
}
String folder = sdFile.getFolder();
if(StringUtils.isEmpty(folder))
{
folder = "ibizutil";
sdFile.setFolder(folder);
}
String filePath = sdFile.getFilePath();
if(StringUtils.isEmpty(filePath)) {
filePath = folder.concat(File.separator).concat(fileId).concat(File.separator).concat(fileName);
sdFile.setFilePath(filePath);
}
int size = (int)multipartFile.getSize();
sdFile.setFileSize(size);
String fileFullPath = this.fileRoot.concat(filePath);
fileFullPath = fileFullPath.replace("\\",File.separator).replace("/",File.separator);
File file = new File(fileFullPath);
File parent = new File(file.getParent());
if(!parent.exists())
parent.mkdirs();
FileCopyUtils.copy(multipartFile.getInputStream(),Files.newOutputStream(file.toPath()));
item=new FileItem(fileId,fileName,fileId,fileName,size,extension);
sdFileService.save(sdFile);
} catch (IOException e) {
throw new InternalServerErrorException("文件上传失败");
}
return item;
}
public File getFile(String fileId) {
return getFile("",fileId);
}
public File getFile(String folder,String fileId) {
if(StringUtils.isEmpty(folder))
folder = "ibizutil";
String dirpath = this.fileRoot.concat(folder).concat(File.separator).concat(fileId);
File parent = new File(dirpath);
if (parent.exists() && parent.isDirectory() && parent.listFiles().length > 0) {
return parent.listFiles()[0];
}
return getFile(sdFileService.get(fileId));
}
public File getFile(SDFile sdFile) {
if(!StringUtils.isEmpty(sdFile.getFilePath())){
String fileFullPath = this.fileRoot.concat(sdFile.getFilePath());
fileFullPath = fileFullPath.replace("\\",File.separator).replace("/",File.separator);
File file = new File(fileFullPath);
if(file.exists())
return file;
}
throw new InternalServerErrorException("文件未找到");
}
/**
* 获取文件扩展名
* @param fileName
* @return
*/
public static String getExtensionName(String fileName) {
if ((fileName != null) && (fileName.length() > 0)) {
int dot = fileName.lastIndexOf('.');
if ((dot >-1) && (dot < (fileName.length() - 1))) {
return fileName.substring(dot + 1);
}
}
return fileName;
}
}
\ No newline at end of file
package cn.ibizlab.api.rest.extensions;
import cn.ibizlab.core.disk.extensions.service.DiskCoreService;
import cn.ibizlab.util.domain.FileItem;
import cn.ibizlab.util.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
@Slf4j
@RestController
@RequestMapping("/")
public class DiskCoreResource
{
@Autowired
private DiskCoreService diskCoreService;
@PostMapping(value = "{folder}/upload")
public ResponseEntity<FileItem> upload(@PathVariable("folder") String folder,@RequestParam("file") MultipartFile multipartFile){
return ResponseEntity.ok().body(diskCoreService.saveFile(folder,multipartFile));
}
@GetMapping(value = "{folder}/download/{id}")
@ResponseStatus(HttpStatus.OK)
public void download(@PathVariable("folder") String folder, @PathVariable("id") String id, HttpServletResponse response){
File file= diskCoreService.getFile(folder,id);
response.setHeader("Content-Disposition", "attachment;filename="+getFileName(file.getName()));
this.sendRespose(response, file);
}
@GetMapping(value = "net-disk/{folder}/{id}/{name}.{ext}")
@ResponseStatus(HttpStatus.OK)
public void open(@PathVariable("folder") String folder, @PathVariable("id") String id,
@PathVariable("name") String name, @PathVariable("ext") String ext, HttpServletResponse response){
File file= diskCoreService.getFile(folder,id);
response.setHeader("Content-Disposition", "attachment;filename="+getFileName(file.getName()));
this.sendRespose(response, file);
}
protected void sendRespose(HttpServletResponse response, File file){
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
}
catch (Exception e) {
//throw e;
}
finally {
if (bis != null) {
try {
bis.close();
}
catch (IOException e) {
}
}
if (bos != null) {
try {
bos.close();
}
catch (IOException e) {
}
}
}
}
protected String getFileName(String fileName){
try {
return new String(fileName.getBytes("utf-8"),"iso8859-1");//防止中文乱码
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return fileName;
}
}
\ No newline at end of file
package cn.ibizlab.util.service;
import cn.ibizlab.util.domain.FileItem;
import cn.ibizlab.util.errors.InternalServerErrorException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.DigestUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@Primary
@Slf4j
@Service
public class SimpleFileService implements FileService {
@Value("${ibiz.filePath:/app/file/}")
private String fileRoot;
@Override
public FileItem saveFile(MultipartFile multipartFile) {
FileItem item=null;
// 获取文件名
String fileName = multipartFile.getOriginalFilename();
// 获取文件后缀
String extname="."+getExtensionName(fileName);
try {
String fileid= DigestUtils.md5DigestAsHex(multipartFile.getInputStream());
String fileFullPath = this.fileRoot+"ibizutil"+File.separator+fileid+File.separator+fileName;
File file = new File(fileFullPath);
File parent = new File(file.getParent());
if(!parent.exists())
parent.mkdirs();
FileCopyUtils.copy(multipartFile.getInputStream(),Files.newOutputStream(file.toPath()));
item=new FileItem(fileid,fileName,fileid,fileName,(int)multipartFile.getSize(),extname);
} catch (IOException e) {
throw new InternalServerErrorException("文件上传失败");
}
return item;
}
@Override
public File getFile(String fileid) {
String dirpath = this.fileRoot+"ibizutil"+File.separator+fileid;
File parent = new File(dirpath);
if (parent.exists() && parent.isDirectory() && parent.listFiles().length > 0) {
return parent.listFiles()[0];
}
throw new InternalServerErrorException("文件未找到");
}
/**
* 获取文件扩展名
* @param filename
* @return
*/
public static String getExtensionName(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length() - 1))) {
return filename.substring(dot + 1);
}
}
return filename;
}
public class SimpleFileService {
}
\ No newline at end of file
Markdown 格式
0% or
您添加了 0 到此讨论。请谨慎行事。
先完成此消息的编辑!
想要评论请 注册