Java:SpringBoot已矣文献上传

夜色资讯

夜色资讯

  • 首页
  • 精品推荐
  • 热门资讯
  • 最新动态
  • 综合新闻
  • 你的位置:夜色资讯 > 最新动态 > Java:SpringBoot已矣文献上传

    Java:SpringBoot已矣文献上传

    发布日期:2022-09-12 12:24    点击次数:118

    Java:SpringBoot已矣文献上传

    后端上传代码如下

    packagecom.mouday.demo.controller;importorg.springframework.stereotype.Controller;importorg.springframework.util.ResourceUtils;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.PostMapping;importorg.springframework.web.bind.annotation.RequestParam;importorg.springframework.web.bind.annotation.ResponseBody;importorg.springframework.web.multipart.MultipartFile;importjava.io.File;importjava.io.FileNotFoundException;importjava.io.IOException;importjava.util.ArrayList;importjava.util.List;/***文献上传*参考*https://blog.csdn.net/gnail_oug/article/details/80324120*/@ControllerpublicclassFileController{//赢得上传测试页面@GetMapping("/upload")publicStringupload{return"upload";}//单文献上传@PostMapping("/upload")@ResponseBodypublicStringupload(MultipartFilefile){if(file.isEmpty){return"fileisEmpty";}Stringfilename=file.getOriginalFilename;try{Filepath=this.getUploadDirectory;Filedest=newFile(path,filename);System.out.println(dest.getPath);file.transferTo(dest);returnnewFile("/upload",filename).toString;}catch(IOExceptione){e.printStackTrace;return"error";}}//多文献上传@PostMapping("/multiUpload")@ResponseBodypublicListupload(@RequestParam("file")Listfiles){Listlist=newArrayList;for(MultipartFilefile:files){list.add(this.upload(file));}returnlist;}/***赢得文献保存旅途*参考:https://www.bbsmax.com/A/GBJrE67Wz0/**@return*@throwsFileNotFoundException*/publicFilegetUploadDirectorythrowsFileNotFoundException{StringpathName=ResourceUtils.getURL("classpath:").getPath;Filepath=newFile(pathName,最新动态"/public/upload");if(!path.exists){path.mkdirs;}returnpath;}}

    前端代码

    src/main/resources/templates/upload.html

    Title单文献上传多文献上传



    栏目分类