/** * 上传文件 */ @RequestMapping("/upload") @IgnoreAuth public R upload(@RequestParam("file") MultipartFile file,String type) throws Exception { if (file.isEmpty()) { throw new EIException("上传文件不能为空"); } String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1); File path = new File(ResourceUtils.getURL("classpath:static").getPath()); if(!path.exists()) { path = new File(""); } File upload = new File(path.getAbsolutePath(),"/upload/"); if(!upload.exists()) { upload.mkdirs(); } String fileName = new Date().getTime()+"."+fileExt; if(StringUtils.isNotBlank(type) && type.contains("_template")) { fileName = type + "."+fileExt; new File(upload.getAbsolutePath()+"/"+fileName).deleteOnExit(); } File dest = new File(upload.getAbsolutePath()+"/"+fileName); file.transferTo(dest); // FileUtils.copyFile(dest, new File("D:\\springbootq33sd\\src\\main\\resources\\static\\upload"+"/"+fileName)); /**修改了路径以后请将该行最前面的//注释去掉**/ if(StringUtils.isNotBlank(type) && type.equals("1")) { ConfigEntity configEntity = configService.selectOne(new EntityWrapper<ConfigEntity>().eq("name", "faceFile")); if(configEntity==null) { configEntity = new ConfigEntity(); configEntity.setName("faceFile"); configEntity.setValue(fileName); } else { configEntity.setValue(fileName); } configService.insertOrUpdate(configEntity); } return R.ok().put("file", fileName); }
时间: 2023-06-16 08:03:45 浏览: 205
这是一个Java Spring Boot框架下的文件上传接口,使用了@RequestParam注解来获取上传的文件参数。如果文件为空,则会抛出一个EIException异常。接着获取文件的后缀名,并创建一个目录来存储上传的文件。如果type参数不为空并且包含"_template"字符串,则将文件名设置为type加上后缀名,然后删除该文件。然后将上传的文件保存到指定的目录中,并返回文件名。如果type参数等于"1",则将文件名存储到数据库中。
相关问题
@Controller @RequestMapping("/file") public class FileController extends BaseController { /** * 文件上传 */ @RequestMapping(value = "/upload", method = RequestMethod.POST) @ResponseBody public JsonResult upload(@RequestParam("file")MultipartFile file, @RequestParam("key")String key, HttpSession session){ Map<String,String> map = new HashMap<>(); try { map = FileUtil.upload(file); } catch (Exception e) { e.printStackTrace(); return JsonResult.error("上传失败"); } String filePath = map.get("filePath"); //将图片URL存在session中 String sessionKey = Constant.SESSION_IMG_PREFIX + key; List<String> imgList = (List<String>) session.getAttribute(sessionKey); if(imgList == null){ imgList = new ArrayList<>(); } imgList.add(filePath); session.setAttribute(sessionKey,imgList); return JsonResult.success("上传成功"); } }
这段代码是一个 Java Spring MVC 的控制器,用于处理文件上传的请求。该控制器对应的请求 URL 是 "/file/upload",请求方法为 POST。在请求参数中,有一个名为 "file" 的文件参数和一个名为 "key" 的字符串参数。其中,"file" 参数用于上传文件,"key" 参数用于标识上传的文件类型。在文件上传成功后,该控制器将文件路径存储在 HttpSession 中,以便后续处理。同时,该控制器返回一个 JsonResult 对象,其中包含上传结果的信息。
@RestController @RequestMapping("/api/upload") @CrossOrigin(origins = "http://localhost:8081") public class UploadPictureController { @Autowired private UploadPicturesService uploadPicturesService; @PostMapping("/picture") public R uploadPicture(@RequestParam("file") MultipartFile multipartFile) throws IOException { String pictureUrl = uploadPicturesService.uploadPicture(multipartFile); return new R(true,pictureUrl); } }
这是一个使用Spring框架编写的RESTful API接口,用于处理上传图片的请求。该接口位于/api/upload/picture路径下,并允许跨域访问(允许来自http://localhost:8081的请求)。
该接口依赖于UploadPicturesService服务,通过@Autowired注解进行自动注入。在处理POST请求时,接口会从请求参数中获取上传的文件,并将其传递给UploadPicturesService服务进行处理。如果上传成功,接口将返回一个封装了上传图片URL的响应对象R,其中R的状态为true,数据为上传图片的URL。如果上传失败,接口将返回一个封装了错误信息的响应对象R,其中R的状态为false,数据为错误信息。