图片上传

This commit is contained in:
liuxiao 2024-07-30 16:04:18 +08:00
parent 0fe4c00f8f
commit cef67d95db
3 changed files with 25 additions and 37 deletions

View File

@ -22,14 +22,13 @@ 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 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.IOException; import java.io.IOException;
import java.util.Arrays; import java.text.SimpleDateFormat;
import java.util.HashMap; import java.util.*;
import java.util.List;
import java.util.Map;
/** /**
* 文件上传 控制层 * 文件上传 控制层
@ -45,6 +44,7 @@ public class SysOssController extends BaseController {
@Value("${file.BASE_FILE_SAVE_PATH}") @Value("${file.BASE_FILE_SAVE_PATH}")
private String uploadPath; private String uploadPath;
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
private final ISysOssService iSysOssService; private final ISysOssService iSysOssService;
private final ISysImageService iSysImageService; private final ISysImageService iSysImageService;
@ -98,43 +98,30 @@ public class SysOssController extends BaseController {
*/ */
@Log(title = "本地文件上传", businessType = BusinessType.INSERT) @Log(title = "本地文件上传", businessType = BusinessType.INSERT)
@PostMapping("/addPartsUpload") @PostMapping("/addPartsUpload")
public R<Map<String, String>> addPartsUpload(@RequestPart("file") MultipartFile file) { public R<Map<String, String>> addPartsUpload(@RequestPart("file") MultipartFile file, HttpServletRequest req) {
Map<String,String> map=new HashMap<>(); Map<String,String> map=new HashMap<>();
String realPath = req.getSession().getServletContext().getRealPath("/uploadFile/");
String filePath=uploadPath+"/file/upload/"; String format = sdf.format(new Date());
///www/wwwroot/guanwang/web/image/file/upload/
// String filePath=uploadPath+"/file/upload/";
File folder = new File(realPath + format);
if (!folder.isDirectory()){
folder.mkdirs();
}
String oldName = file.getOriginalFilename();
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."), oldName.length());
try { try {
if (file.isEmpty()) { file.transferTo(new File(realPath,newName));
return R.warn("文件为空"); String filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploadFile" + format + newName;
} Long image = iSysImageService.uploadImage(newName,filePath);
map.put("url",filePath);
String fileName = System.currentTimeMillis()+"-"+file.getOriginalFilename(); map.put("fileName",newName);
//文件上传的路径当前项目的根目录
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://47.121.27.78: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()); map.put("ossId",image.toString());
return R.ok(map); return R.ok(map);
} catch (IOException e) { } catch (IOException ex) {
return R.warn("文件上传失败: "+e); ex.printStackTrace();
} }
return R.warn("文件上传失败!");
} }

View File

@ -41,7 +41,7 @@ public class GwIndexController extends BaseController {
/** /**
* 查询首页封面信息列表 * 查询首页封面信息列表
*/ */
@SaCheckPermission("official:index:list") /* @SaCheckPermission("official:index:list")*/
@GetMapping("/list") @GetMapping("/list")
public TableDataInfo<GwIndexVo> list(GwIndexBo bo, PageQuery pageQuery) { public TableDataInfo<GwIndexVo> list(GwIndexBo bo, PageQuery pageQuery) {
return iGwIndexService.queryPageList(bo, pageQuery); return iGwIndexService.queryPageList(bo, pageQuery);

View File

@ -44,6 +44,7 @@ public class GwSlideshowServiceImpl implements IGwSlideshowService {
GwSlideshowVo gwSlideshowVo = baseMapper.selectVoById(id); GwSlideshowVo gwSlideshowVo = baseMapper.selectVoById(id);
//查询详情时返回上一篇下一篇处理 //查询详情时返回上一篇下一篇处理
List<GwSlideshow> list = baseMapper.selectList(Wrappers.<GwSlideshow>lambdaQuery().eq(GwSlideshow::getType,gwSlideshowVo.getType()).orderByDesc(GwSlideshow::getIsTop).orderByDesc(GwSlideshow::getCreateTime)); List<GwSlideshow> list = baseMapper.selectList(Wrappers.<GwSlideshow>lambdaQuery().eq(GwSlideshow::getType,gwSlideshowVo.getType()).orderByDesc(GwSlideshow::getIsTop).orderByDesc(GwSlideshow::getCreateTime));
//获取文章推荐内容
getPrevNextArticles(id, gwSlideshowVo, list); getPrevNextArticles(id, gwSlideshowVo, list);
return gwSlideshowVo; return gwSlideshowVo;
} }