附件上传下载
This commit is contained in:
parent
2ee0d0aefd
commit
301c64543a
|
@ -3,6 +3,7 @@ package com.ruoyi.web.controller.system;
|
|||
|
||||
import cn.dev33.satoken.annotation.SaCheckPermission;
|
||||
import cn.hutool.core.util.ObjectUtil;
|
||||
import com.amazonaws.util.IOUtils;
|
||||
import com.ruoyi.common.annotation.Log;
|
||||
import com.ruoyi.common.core.controller.BaseController;
|
||||
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.validate.QueryGroup;
|
||||
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.system.domain.bo.SysOssBo;
|
||||
import com.ruoyi.system.domain.vo.SysOssVo;
|
||||
|
@ -17,16 +20,22 @@ import com.ruoyi.system.service.ISysOssService;
|
|||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
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.HttpServletResponse;
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
|
@ -93,6 +102,7 @@ public class SysOssController extends BaseController {
|
|||
|
||||
/**
|
||||
* 上传文件到本地
|
||||
*
|
||||
* @param file 文件数据
|
||||
* @return 文件上传地址信息
|
||||
*/
|
||||
|
@ -123,7 +133,7 @@ public class SysOssController extends BaseController {
|
|||
String pathFan = filePath.replace("\\", "/");
|
||||
//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);
|
||||
System.err.println("替换后:" + pathFan);
|
||||
|
@ -136,8 +146,92 @@ 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);
|
||||
// 创建目标目录(如果不存在)
|
||||
File directory = new File(filePath);
|
||||
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对象
|
||||
|
|
|
@ -134,6 +134,9 @@ security:
|
|||
- /official/slideshow/**
|
||||
- /official/jobInfo/**
|
||||
- /official/index/**
|
||||
- /official/message/**
|
||||
- /official/companymessage/**
|
||||
- /system/oss/addFilesUpload
|
||||
# 公共路径
|
||||
- /favicon.ico
|
||||
- /error
|
||||
|
|
Loading…
Reference in New Issue
Block a user