Merge branch 'main' of http://git.hivekion.com:3000/guowenhao/weilaizhixing_web_stie
This commit is contained in:
commit
91cb2b50c8
|
@ -3,6 +3,7 @@ package com.ruoyi.web.controller.system;
|
||||||
|
|
||||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||||
import cn.hutool.core.util.ObjectUtil;
|
import cn.hutool.core.util.ObjectUtil;
|
||||||
|
import com.amazonaws.util.IOUtils;
|
||||||
import com.ruoyi.common.annotation.Log;
|
import com.ruoyi.common.annotation.Log;
|
||||||
import com.ruoyi.common.core.controller.BaseController;
|
import com.ruoyi.common.core.controller.BaseController;
|
||||||
import com.ruoyi.common.core.domain.PageQuery;
|
import com.ruoyi.common.core.domain.PageQuery;
|
||||||
|
@ -10,6 +11,8 @@ import com.ruoyi.common.core.domain.R;
|
||||||
import com.ruoyi.common.core.page.TableDataInfo;
|
import com.ruoyi.common.core.page.TableDataInfo;
|
||||||
import com.ruoyi.common.core.validate.QueryGroup;
|
import com.ruoyi.common.core.validate.QueryGroup;
|
||||||
import com.ruoyi.common.enums.BusinessType;
|
import com.ruoyi.common.enums.BusinessType;
|
||||||
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
|
import com.ruoyi.common.utils.file.FileUtils;
|
||||||
import com.ruoyi.official.service.ISysImageService;
|
import com.ruoyi.official.service.ISysImageService;
|
||||||
import com.ruoyi.system.domain.bo.SysOssBo;
|
import com.ruoyi.system.domain.bo.SysOssBo;
|
||||||
import com.ruoyi.system.domain.vo.SysOssVo;
|
import com.ruoyi.system.domain.vo.SysOssVo;
|
||||||
|
@ -17,16 +20,22 @@ import com.ruoyi.system.service.ISysOssService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.validation.annotation.Validated;
|
import org.springframework.validation.annotation.Validated;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
import org.springframework.web.util.UriUtils;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
@ -66,7 +75,7 @@ public class SysOssController extends BaseController {
|
||||||
@SaCheckPermission("system:oss:list")
|
@SaCheckPermission("system:oss:list")
|
||||||
@GetMapping("/listByIds/{ossIds}")
|
@GetMapping("/listByIds/{ossIds}")
|
||||||
public R<List<String>> listByIds(@NotEmpty(message = "主键不能为空")
|
public R<List<String>> listByIds(@NotEmpty(message = "主键不能为空")
|
||||||
@PathVariable Long[] ossIds) {
|
@PathVariable Long[] ossIds) {
|
||||||
List<String> list = iSysOssService.listByIds(Arrays.asList(ossIds));
|
List<String> list = iSysOssService.listByIds(Arrays.asList(ossIds));
|
||||||
return R.ok(list);
|
return R.ok(list);
|
||||||
}
|
}
|
||||||
|
@ -93,6 +102,7 @@ public class SysOssController extends BaseController {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上传文件到本地
|
* 上传文件到本地
|
||||||
|
*
|
||||||
* @param file 文件数据
|
* @param file 文件数据
|
||||||
* @return 文件上传地址信息
|
* @return 文件上传地址信息
|
||||||
*/
|
*/
|
||||||
|
@ -123,7 +133,7 @@ public class SysOssController extends BaseController {
|
||||||
String pathFan = filePath.replace("\\", "/");
|
String pathFan = filePath.replace("\\", "/");
|
||||||
//filePath获取到的地址斜杠是“ \ ”的(单斜杠是特殊符号,得用双斜杠代替),得换成“ / ”才能访问到
|
//filePath获取到的地址斜杠是“ \ ”的(单斜杠是特殊符号,得用双斜杠代替),得换成“ / ”才能访问到
|
||||||
//返回全路径图片地址
|
//返回全路径图片地址
|
||||||
String returnPath = ("http://47.121.27.78:8008/upload/" + fileName).replace("\\", "/");
|
String returnPath = ("http://39.101.76.77:8008/upload/" + fileName).replace("\\", "/");
|
||||||
//修改数据库
|
//修改数据库
|
||||||
Long image = iSysImageService.uploadImage(fileName, returnPath);
|
Long image = iSysImageService.uploadImage(fileName, returnPath);
|
||||||
System.err.println("替换后:" + pathFan);
|
System.err.println("替换后:" + pathFan);
|
||||||
|
@ -136,18 +146,102 @@ public class SysOssController extends BaseController {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 上传文件到本地
|
||||||
|
*
|
||||||
|
* @param file 文件数据
|
||||||
|
* @return 文件上传地址信息
|
||||||
|
*/
|
||||||
|
@Log(title = "本地文件上传Files", businessType = BusinessType.INSERT)
|
||||||
|
@PostMapping("/addFilesUpload")
|
||||||
|
public R<Map<String, String>> addFilesUpload(@RequestPart("file") MultipartFile file) {
|
||||||
|
Map<String, String> map = new HashMap<>();
|
||||||
|
|
||||||
|
String filePath = uploadPath + "/file/files/";
|
||||||
|
try {
|
||||||
|
if (file.isEmpty()) {
|
||||||
|
return R.warn("文件为空");
|
||||||
|
}
|
||||||
|
|
||||||
|
String fileName = System.currentTimeMillis() + "-" + file.getOriginalFilename();
|
||||||
|
//文件上传的路径(当前项目的根目录)
|
||||||
|
|
||||||
/**
|
System.err.println(filePath);
|
||||||
* 下载OSS对象
|
// 创建目标目录(如果不存在)
|
||||||
*
|
File directory = new File(filePath);
|
||||||
* @param ossId OSS对象ID
|
if (!directory.exists()) {
|
||||||
*/
|
directory.mkdirs();
|
||||||
|
}
|
||||||
|
// 保存文件到目标目录
|
||||||
|
File uploadFile = new File(directory.getAbsolutePath() + File.separator + fileName);
|
||||||
|
file.transferTo(uploadFile);
|
||||||
|
//上传服务器地址
|
||||||
|
String pathFan = filePath.replace("\\", "/");
|
||||||
|
//filePath获取到的地址斜杠是“ \ ”的(单斜杠是特殊符号,得用双斜杠代替),得换成“ / ”才能访问到
|
||||||
|
//返回全路径图片地址
|
||||||
|
String returnPath = ("http://39.101.76.77:8008/upload/" + fileName).replace("\\", "/");
|
||||||
|
//修改数据库
|
||||||
|
Long image = iSysImageService.uploadImage(fileName, returnPath);
|
||||||
|
System.err.println("替换后:" + pathFan);
|
||||||
|
map.put("url", returnPath);
|
||||||
|
map.put("fileName", fileName);
|
||||||
|
map.put("ossId", image.toString());
|
||||||
|
return R.ok(map);
|
||||||
|
} catch (IOException e) {
|
||||||
|
return R.warn("文件上传失败: " + e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 通用下载请求
|
||||||
|
*/
|
||||||
|
@GetMapping("/download")
|
||||||
|
public void bianPdfDownload(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||||
|
// 请求类型、请求参数、请求头 根据需求制定即可
|
||||||
|
// 获取到需要下载的文件
|
||||||
|
// 如何生成文件,根据实际业务需求改造即可
|
||||||
|
//http://39.101.76.77:8008/upload/
|
||||||
|
String fileName = request.getParameter("imageUrl").substring(32);
|
||||||
|
String filePath = uploadPath + "/file/files/";
|
||||||
|
String outputFilePath = filePath + fileName;
|
||||||
|
|
||||||
|
log.info("待下载文件:{}", outputFilePath);
|
||||||
|
// 下载文件
|
||||||
|
// 设置响应的内容类型,让浏览器知道下载的是一个文件
|
||||||
|
ServletContext context = request.getServletContext();
|
||||||
|
// get MIME type of the file
|
||||||
|
String mimeType = context.getMimeType(outputFilePath);
|
||||||
|
if (mimeType == null) {
|
||||||
|
// set to binary type if MIME mapping not found
|
||||||
|
mimeType = "application/octet-stream";
|
||||||
|
log.info("context getMimeType is null");
|
||||||
|
}
|
||||||
|
log.info("MIME type: " + mimeType);
|
||||||
|
// 设置响应头信息,告诉浏览器文件的名称和长度
|
||||||
|
// set content attributes for the response
|
||||||
|
response.setContentType(mimeType);
|
||||||
|
// 设置编码
|
||||||
|
response.setCharacterEncoding("utf-8");
|
||||||
|
// 设置请求头参数以及下载的文件名称,中文名称转义防止乱码
|
||||||
|
// Copy the stream to the response's output stream.
|
||||||
|
try {
|
||||||
|
InputStream myStream = new FileInputStream(outputFilePath);
|
||||||
|
IOUtils.copy(myStream, response.getOutputStream());
|
||||||
|
response.flushBuffer();
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 下载OSS对象
|
||||||
|
*
|
||||||
|
* @param ossId OSS对象ID
|
||||||
|
*/
|
||||||
@SaCheckPermission("system:oss:download")
|
@SaCheckPermission("system:oss:download")
|
||||||
@GetMapping("/download/{ossId}")
|
@GetMapping("/download/{ossId}")
|
||||||
public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException {
|
public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException {
|
||||||
iSysOssService.download(ossId,response);
|
iSysOssService.download(ossId, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -134,6 +134,9 @@ security:
|
||||||
- /official/slideshow/**
|
- /official/slideshow/**
|
||||||
- /official/jobInfo/**
|
- /official/jobInfo/**
|
||||||
- /official/index/**
|
- /official/index/**
|
||||||
|
- /official/message/**
|
||||||
|
- /official/companymessage/**
|
||||||
|
- /system/oss/addFilesUpload
|
||||||
# 公共路径
|
# 公共路径
|
||||||
- /favicon.ico
|
- /favicon.ico
|
||||||
- /error
|
- /error
|
||||||
|
|
Loading…
Reference in New Issue
Block a user