当前位置: 首页 > news >正文

java后端保存的本地图片通过ip+端口直接访问

直接上代码吧,

首先在项目的根目录设置file文件

package com.ydx.emms.datapro.controller;import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.io.File;/**
* @Description: 通过ip和端口直接查看图片
* @param
* @author zlw
* @date 2024/9/4 15:09
*/
@Configuration
public class TPicController implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {//虚拟url路径registry.addResourceHandler("/file/**").addResourceLocations("file:" + System.getProperty("user.dir").replace(File.separatorChar, '/') + "/file/");//真实本地路径}
}

访问:http://127.0.0.1:端口/file/1.jpg

补充:从一个文件夹移动某一个文件到另一个文件夹

@RequestMapping(value = "/moveAlarmPic")
Result moveAlarmPic(HttpServletRequest request) {// 获取请求参数Map<String, Object> params = ParamUtil.getParams(request);String ip = MapUtils.getString(params, "ip");String time = MapUtils.getString(params, "time");// 参数判断if (ParamUtil.isOneEmpty(ip, time)) {return new Result("rest_ACK", Result.Code.ParamError.value(), false, "参数不正确,必填参数有为空");} else {//在告警文件夹下创建指定目录makeDir(ip);//移动指定的图片// 源文件路径Path sourcePath = Paths.get(System.getProperty("user.dir") + "/pic/" + ip+"/"+time+".jpg");Path sourcePathCsv = Paths.get(System.getProperty("user.dir") + "/pic/" + ip+"/"+time+".csv");// 目标文件夹路径Path destinationPath = Paths.get(System.getProperty("user.dir").replace(File.separatorChar, '/') + "/file/"+ip);// 目标文件完整路径Path targetPath = destinationPath.resolve(sourcePath.getFileName());Path targetPathCsv = destinationPath.resolve(sourcePathCsv.getFileName());try {// 复制图片Files.copy(sourcePath, targetPath, StandardCopyOption.REPLACE_EXISTING);//复制矩阵Files.copy(sourcePathCsv, targetPathCsv, StandardCopyOption.REPLACE_EXISTING);System.out.println("文件复制成功!");return new Result("rest_ACK", Result.Code.Success.value(), true);} catch (IOException e) {System.out.println("文件复制失败: " + e.getMessage());return new Result("rest_ACK", Result.Code.Success.value(), false);}}
}
/*** @param* @Description: 指定目录下创建文件夹* @author zlw* @date 2024/7/26 14:06*/
public void makeDir(String dirName) {// 指定要创建的文件夹路径String directoryPath = System.getProperty("user.dir").replace(File.separatorChar, '/') + "/file/"+dirName;// 创建File对象File dir = new File(directoryPath);// 如果文件夹不存在,则创建文件夹if (!dir.exists()) {boolean result = dir.mkdir();if (result) {System.out.println("文件夹创建成功:" + directoryPath);} else {System.out.println("文件夹创建失败:" + directoryPath);}} else {System.out.println("文件夹已存在:" + directoryPath);}
}


http://www.mrgr.cn/news/23413.html

相关文章:

  • 喜报!安畅检测取得国网信通信集团供应商资质
  • ICM20948 DMP代码详解(11)
  • 在 ArkTS 中,如何有效地进行内存管理和避免内存泄漏?
  • Spring Boot 自动配置
  • TCP 三次握手和四次挥手
  • HalconDotNet的图像模式识别详解
  • Pycharm Remote Development 报错解决
  • 在IDEA中如何创建web项目?——不使用Archetype
  • Edge-Triggered模式:反应堆
  • 人人都想转行的AI产品经理到底是啥?看完这一篇你就知道了
  • 如何理解API与数据源?
  • 摩洛哥的预扣税及企业所得税
  • Find My皮套|苹果Find My技术与皮套结合,智能防丢,全球定位
  • 酷柚易汛ERP全新APP端上线啦!
  • 油耳朵耳屎怎么清理?可视耳勺使用方法
  • SmartNews如何赋能日本市场解锁购买力强劲广告营销新篇章
  • 价值流架构指南:构建业务创新与竞争优势的全面方法论
  • 教师节重磅福利!《动手学强化学习》作者亲自带你学强化学习
  • VUCA时代与传统企业数字化转型
  • 一分钟了解小程序的等保测评