zbj//1.预览资源 todo未完成
This commit is contained in:
@@ -22,6 +22,7 @@ import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
@@ -57,6 +58,7 @@ public class ResourceController extends BaseController {
|
||||
|
||||
/**
|
||||
* 查询数据
|
||||
*
|
||||
* @return List<ResourceData>
|
||||
*/
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@@ -68,6 +70,20 @@ public class ResourceController extends BaseController {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, result, methodDescribe);
|
||||
}
|
||||
|
||||
/**
|
||||
* 预览资源
|
||||
*/
|
||||
//todo 未完成
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@GetMapping("/previewFile")
|
||||
@ApiOperation("预览资源")
|
||||
@ApiImplicitParam(name = "id", value = "id", required = true)
|
||||
public HttpResult<Object> previewFile(@RequestParam("id") String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
String methodDescribe = getMethodDescribe("downloadFile");
|
||||
iResourceAdministrationService.previewFile(id, request, response);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "成功", methodDescribe);
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改资源
|
||||
*//*
|
||||
@@ -98,20 +114,9 @@ public class ResourceController extends BaseController {
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, flag, methodDescribe);
|
||||
}
|
||||
|
||||
*//**
|
||||
* 下载资源
|
||||
*//*
|
||||
@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
@GetMapping("/downloadFile")
|
||||
@ApiOperation("下载资源")
|
||||
@ApiImplicitParam(name = "id",value = "id",required = true)
|
||||
public HttpResult<Object> downloadFile(@RequestParam("id")String id, HttpServletResponse response) throws IOException {
|
||||
String methodDescribe = getMethodDescribe("downloadFile");
|
||||
iResourceAdministrationService.downloadFile(id,response);
|
||||
return HttpResultUtil.assembleCommonResponseResult(CommonResponseEnum.SUCCESS, "下载成功", methodDescribe);
|
||||
}
|
||||
|
||||
*//**
|
||||
|
||||
/**
|
||||
* 导出数据
|
||||
*//*
|
||||
*//*@OperateInfo(info = LogEnum.BUSINESS_COMMON)
|
||||
|
||||
@@ -7,6 +7,7 @@ import com.njcn.device.pq.pojo.vo.MonitoringPointScaleVO;
|
||||
import com.njcn.system.pojo.po.Resinformation;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
@@ -22,12 +23,15 @@ public interface ResourceService extends IService<ResourceData> {
|
||||
Boolean uploadFile(MultipartFile multipartFile, MultipartFile picture, String name, Integer type);
|
||||
|
||||
List<ResourceData> queryData();
|
||||
|
||||
void previewFile(String id, HttpServletRequest request, HttpServletResponse response) throws IOException;
|
||||
|
||||
/*
|
||||
Boolean updateFile(MultipartFile multipartFile, String id, String name, String type, String description, String systemType);
|
||||
|
||||
Boolean deleteFile(String id);
|
||||
|
||||
void downloadFile(String id, HttpServletResponse response) throws IOException;
|
||||
|
||||
|
||||
void exportData();*/
|
||||
}
|
||||
|
||||
@@ -26,9 +26,9 @@ import org.apache.commons.lang.StringUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.*;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -60,7 +60,7 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, ResourceDat
|
||||
resUrl = fileStorageUtil.uploadMultipart(multipartFile, OssPath.RESOURCEDATA);
|
||||
//判断图片文件对象是否为空,不为空获取图片路径
|
||||
if (null != picture && !picture.isEmpty()) {
|
||||
picUrl = fileStorageUtil.uploadMultipart(picture, OssPath.RESOURCEDATA);
|
||||
picUrl = fileStorageUtil.uploadMultipart(picture, OssPath.RESOURCEDATA);
|
||||
}
|
||||
//创建对象
|
||||
ResourceData resourceData = new ResourceData();
|
||||
@@ -76,8 +76,120 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, ResourceDat
|
||||
|
||||
@Override
|
||||
public List<ResourceData> queryData() {
|
||||
return this.list();
|
||||
List<ResourceData> list = this.list();
|
||||
for (ResourceData resourceData : list) {
|
||||
if (StringUtils.isNotBlank(resourceData.getPicUrl())) {
|
||||
resourceData.setPicUrl(fileStorageUtil.getFileUrl(resourceData.getPicUrl()));
|
||||
}
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
public static long getFileSize(InputStream inputStream) throws IOException {
|
||||
byte[] buffer = new byte[1024];
|
||||
int len;
|
||||
long size = 0;
|
||||
while ((len = inputStream.read(buffer)) != -1) {
|
||||
size += len;
|
||||
}
|
||||
inputStream.close();
|
||||
return size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void previewFile(String id, HttpServletRequest request, HttpServletResponse response) throws IOException {
|
||||
ResourceData resourceData = resourceMapper.selectById(id);
|
||||
//获取文件流
|
||||
InputStream ins = fileStorageUtil.getFileStream(resourceData.getResUrl());
|
||||
if (StringUtils.isNotBlank(resourceData.getPicUrl())) {
|
||||
resourceData.setPicUrl(fileStorageUtil.getFileUrl(resourceData.getPicUrl()));
|
||||
}
|
||||
try (
|
||||
BufferedInputStream bis = new BufferedInputStream(ins);
|
||||
OutputStream out = response.getOutputStream()) {
|
||||
long p = 0L;
|
||||
long toLength = 0L;
|
||||
long contentLength = 0L;
|
||||
int rangeSwitch = 0;
|
||||
long fileLength;
|
||||
String rangBytes = "";
|
||||
fileLength = getFileSize(ins);
|
||||
// get file content
|
||||
// tell the client to allow accept-ranges
|
||||
response.reset();
|
||||
response.setHeader("Accept-Ranges", "bytes");
|
||||
// client requests a file block download start byte
|
||||
String range = request.getHeader("Range");
|
||||
if (range != null && range.trim().length() > 0 && !"null".equals(range)) {
|
||||
response.setStatus(javax.servlet.http.HttpServletResponse.SC_PARTIAL_CONTENT);
|
||||
rangBytes = range.replaceAll("bytes=", "");
|
||||
if (rangBytes.endsWith("-")) { // bytes=270000-
|
||||
rangeSwitch = 1;
|
||||
p = Long.parseLong(rangBytes.substring(0, rangBytes.indexOf("-")));
|
||||
contentLength = fileLength - p; // 客户端请求的是270000之后的字节(包括bytes下标索引为270000的字节)
|
||||
} else { // bytes=270000-320000
|
||||
rangeSwitch = 2;
|
||||
String temp1 = rangBytes.substring(0, rangBytes.indexOf("-"));
|
||||
String temp2 = rangBytes.substring(rangBytes.indexOf("-") + 1, rangBytes.length());
|
||||
p = Long.parseLong(temp1);
|
||||
toLength = Long.parseLong(temp2);
|
||||
contentLength = toLength - p + 1; // 客户端请求的是 270000-320000 之间的字节
|
||||
}
|
||||
} else {
|
||||
contentLength = fileLength;
|
||||
}
|
||||
// 如果设设置了Content-Length,则客户端会自动进行多线程下载。如果不希望支持多线程,则不要设置这个参数。
|
||||
// Content-Length: [文件的总大小] - [客户端请求的下载的文件块的开始字节]
|
||||
response.setHeader("Content-Length", Long.toString(contentLength));
|
||||
// 断点开始
|
||||
// 响应的格式是:
|
||||
// Content-Range: bytes [文件块的开始字节]-[文件的总大小 - 1]/[文件的总大小]
|
||||
if (rangeSwitch == 1) {
|
||||
String contentRange = new StringBuffer("bytes ").append(new Long(p).toString()).append("-")
|
||||
.append(new Long(fileLength - 1).toString()).append("/")
|
||||
.append(new Long(fileLength).toString()).toString();
|
||||
response.setHeader("Content-Range", contentRange);
|
||||
bis.skip(p);
|
||||
} else if (rangeSwitch == 2) {
|
||||
String contentRange = range.replace("=", " ") + "/" + new Long(fileLength).toString();
|
||||
response.setHeader("Content-Range", contentRange);
|
||||
bis.skip(p);
|
||||
} else {
|
||||
String contentRange = new StringBuffer("bytes ").append("0-").append(fileLength - 1).append("/")
|
||||
.append(fileLength).toString();
|
||||
response.setHeader("Content-Range", contentRange);
|
||||
}
|
||||
String fileName = resourceData.getResourceName();
|
||||
response.setContentType("application/octet-stream");
|
||||
response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
|
||||
int n = 0;
|
||||
long readLength = 0;
|
||||
int bsize = 1024;
|
||||
byte[] bytes = new byte[bsize];
|
||||
if (rangeSwitch == 2) {
|
||||
// 针对 bytes=27000-39000 的请求,从27000开始写数据
|
||||
while (readLength <= contentLength - bsize) {
|
||||
n = bis.read(bytes);
|
||||
readLength += n;
|
||||
out.write(bytes, 0, n);
|
||||
}
|
||||
if (readLength <= contentLength) {
|
||||
n = bis.read(bytes, 0, (int) (contentLength - readLength));
|
||||
out.write(bytes, 0, n);
|
||||
}
|
||||
} else {
|
||||
while ((n = bis.read(bytes)) != -1) {
|
||||
out.write(bytes, 0, n);
|
||||
}
|
||||
}
|
||||
out.flush();
|
||||
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage());
|
||||
}
|
||||
//return resourceData;
|
||||
}
|
||||
|
||||
/*
|
||||
@Override
|
||||
public Boolean updateFile(MultipartFile multipartFile, String id, String name, String type, String description, String systemType) {
|
||||
@@ -116,12 +228,7 @@ public class ResourceServiceImpl extends ServiceImpl<ResourceMapper, ResourceDat
|
||||
return i > 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void downloadFile(String id, HttpServletResponse response) throws IOException {
|
||||
Resinformation resinformation = resourceAdministrationMapper.selectById(id);
|
||||
//下载文件
|
||||
fileStorageUtil.downloadStream(response, resinformation.getUrl());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void exportData() {
|
||||
|
||||
Reference in New Issue
Block a user