首页 > 其他分享 >照片上传

照片上传

时间:2024-03-18 09:00:59浏览次数:17  
标签:String filePath fileName 照片 newFileName file new 上传

Web层:PromotionAdController @RestController @RequestMapping("/PromotionSpace") public class PromotionSpaceController { @Autowired private PromotionSpaceService promotionSpaceService; /* 文件上传 */ @RequestMapping("/PromotionAdUpload") public ResponseResult fileupload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException { Postman测试接口 6. 新建&修改广告(自行完成) 1.1 需求分析 新建需求:点击提交按钮,将页面内容保存到数据库 try { //1.判断文件是否为空 if (file.isEmpty()) { throw new RuntimeException(); } //2.获取项目部署路径 String realPath = request.getServletContext().getRealPath("/"); String webappsPath = realPath.substring(0, realPath.indexOf("ssm_web")); //3.获取原文件名 String fileName = file.getOriginalFilename(); //4.新文件名 String newFileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf(".")); //5.上传文件 String uploadPath = webappsPath + "upload\\"; File filePath = new File(uploadPath, newFileName); //如果目录不存在就创建目录 if (!filePath.getParentFile().exists()) { filePath.getParentFile().mkdirs(); System.out.println("创建目录: " + filePath); } file.transferTo(filePath); //6.将文件名和文件路径返回 Map<String, String> map = new HashMap<>(); map.put("fileName", newFileName); map.put("filePath", LOCAL_URL + "/upload/" + newFileName); ResponseResult result = new ResponseResult(true, 200, "响应成功", map); return result; } catch (Exception e) { e.printStackTrace(); } return null; Web层:PromotionAdController @RestController @RequestMapping("/PromotionSpace") public class PromotionSpaceController { @Autowired private PromotionSpaceService promotionSpaceService; /* 文件上传 */ @RequestMapping("/PromotionAdUpload") public ResponseResult fileupload(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException { Postman测试接口 6. 新建&修改广告(自行完成) 1.1 需求分析 新建需求:点击提交按钮,将页面内容保存到数据库 try { //1.判断文件是否为空 if (file.isEmpty()) { throw new RuntimeException(); } //2.获取项目部署路径 String realPath = request.getServletContext().getRealPath("/"); String webappsPath = realPath.substring(0, realPath.indexOf("ssm_web")); //3.获取原文件名 String fileName = file.getOriginalFilename(); //4.新文件名 String newFileName = System.currentTimeMillis() + fileName.substring(fileName.lastIndexOf(".")); //5.上传文件 String uploadPath = webappsPath + "upload\\"; File filePath = new File(uploadPath, newFileName); //如果目录不存在就创建目录 if (!filePath.getParentFile().exists()) { filePath.getParentFile().mkdirs(); System.out.println("创建目录: " + filePath); } file.transferTo(filePath); //6.将文件名和文件路径返回 Map<string, string=""> map = new HashMap<>(); map.put("fileName", newFileName); map.put("filePath", LOCAL_URL + "/upload/" + newFileName); ResponseResult result = new ResponseResult(true, 200, "响应成功", map); return result; } catch (Exception e) { e.printStackTrace(); } return null;

标签:String,filePath,fileName,照片,newFileName,file,new,上传
From: https://www.cnblogs.com/zyb-luckey/p/18079589

相关文章

  • JAVA实现大文件分片上传断点续传
    直接上代码importorg.springframework.web.multipart.MultipartFile;importjava.util.concurrent.CompletableFuture;importorg.apache.commons.lang3.StringUtils;importlombok.extern.slf4j.Slf4j;importjava.text.DecimalFormat;importjava.io.*;@Slf4jpublic......
  • 【课设】/【毕设】ESP32上传温湿度至新版Onenet云平台并由新版Onenet云平台下发命令控
    一、uniapp设计  1、APP生成token秘钥:  1.1.下载HBuider软件:     直接搜索HBuider,进入官网下载软件:  2.新建项目-选择默认模版3.打开index.vue4.点击运行至内置浏览器;2.1获取Onenet云平台API接口 2.1.1进入Onenet云平台点击服务与支持-文档......
  • vue3+ts 上传组件
    本来是用的jeecg-vue3中的上传组件,如下图: 功能上还是蛮全的,就是上图中这个链接的代码死活找不到,查了下,是基于antv的a-upload实现的。但是antv中也没找到,上图这个只有移入删除的功能但是我这边的需求是点击链接可以直接在网页预览,而不是下载,移入后有个删除和下载的功能按钮。......
  • java上传文件到FTP制定文件夹
    JAVA上传文件到FTP/***@ClassNameFTPLoad*@DescriptionTODO*@Authordell*@Date2024/3/1415:56*@Version1.0**/importcn.hutool.core.io.FileUtil;importcn.hutool.json.JSONObject;importcn.hutool.json.JSONUtil;importorg.apache.commons.net.......
  • HttpWebChilent上传与下载进度条
    HttpClientHandlerhand=newHttpClientHandler();ProgressMessageHandlerprocessMessageHander=newProgressMessageHandler(hand);HttpClientlocalHttpClient=newHttpClient(processMessageHander);HttpRequestMessagehttpRequestMessage=newHttpRequestMes......
  • 在Docker上传我们自己的镜像(以springboot项目为例)
    首先确定好在我们的centOS服务器上已经安装并配置好docker配置自己的springboot镜像并运行获取springboot的jar包mavenclean--》mavenuepackage --》复制target目录下生成的jar包在服务器选择一个文件夹上传jar包,我这里选用的文件夹叫做/opt/dockertest在jar包的同一......
  • python上传图片到网站
    使用requests库实现图片上传在Python中,requests库是处理HTTP请求的一个强大工具,它提供了一种简单易用的方法来执行网络请求。在将图片上传到网站的场景中,可以使用requests库中的post方法,将图片作为多部分编码文件(multipart-encodedfile)发送到服务器。第一,需要一份待上传的图......
  • Langchain-ChatGLM源码解读(一)-文档数据上传
    一、简介Langchain-ChatGLM 相信大家都不陌生,近几周计划出一个源码解读,先解锁langchain的一些基础用法。文档问答过程大概分为以下5部分,在Langchain中都有体现。上传解析文档文档向量化、存储文档召回query向量化文档问答今天主要讲langchain在上传解析文档时是怎么实......
  • Node分片上传和OSS上传
    大文件分片切片就是为了解决大文件上传时间过长,优化体验。将大文件拆分成多个小文件,依次上传,上传完毕后合并成源文件。浏览器的Blob提供了slice方法,可以截取某个范围的数据,而文件上传的File就是一种Blob前端可以通过Blob.slice进行文件拆分,然后就是后端文件合并。f......
  • 解决表格文件上传无法删除临时文件的问题Failed to perform cleanup of multipart ite
    java.io.UncheckedIOException:CannotdeleteC:\Users\hasee\AppData\Local\Temp\tomcat如图所示,刚开始以为是apifox没删除的问题,换了之后依旧这样 尝试方案1-失败 方法二-失败 方法三-成功 原文链接报错:StandardServletMultipartResolver:Failedtoperform......