网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

SpringBoot文件上传(官方案例)-创新互联

  1. 在线文档
  2. 项目结构
    1.源码克隆:git clone https://github.com/spring-guides/gs-uploading-files.git
    2.包含两个项目initial和complete,initial可以根据文档练习完善,complete是完整项目
    3.功能描述:构建接受文件上传的应用程序,并且通过简单的 HTML 界面来测试文件上传下载

    专注于为中小企业提供做网站、成都做网站服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业龙口免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
  3. 源码分析

    1.POM依赖org.springframework.bootspring-boot-starter-thymeleaforg.springframework.bootspring-boot-starter-weborg.springframework.bootspring-boot-starter-testtest
    2.入口类
    // https://blog.csdn.net/tongxin_tongmeng/article/details/128401278
    @SpringBootApplication
    // https://blog.csdn.net/tongxin_tongmeng/article/details/128401815
    @EnableConfigurationProperties(StorageProperties.class)
    public class UploadingFilesApplication {
    
    	public static void main(String[] args) {
    		SpringApplication.run(UploadingFilesApplication.class, args);
    	}
    
        // https://blog.csdn.net/tongxin_tongmeng/article/details/128402169
    	@Bean
    	CommandLineRunner init(StorageService storageService) {
    		return (args) ->{
    			storageService.deleteAll();
    			storageService.init();
    		};
    	}
    }
    3.控制器
    @Controller
    public class FileUploadController {
    
    	private final StorageService storageService;
    
    	// https://blog.csdn.net/tongxin_tongmeng/article/details/128402579
    	@Autowired
    	public FileUploadController(StorageService storageService) {
    		this.storageService = storageService;
    	}
    
    	// https://blog.csdn.net/tongxin_tongmeng/article/details/128405403
    	@GetMapping("/")
    	public String listUploadedFiles(Model model) throws IOException {
    
    		model.addAttribute("files", storageService.loadAll().map(
    				path ->MvcUriComponentsBuilder.fromMethodName(FileUploadController.class,
    						"serveFile", path.getFileName().toString()).build().toUri().toString())
    				.collect(Collectors.toList()));
    
    		return "uploadForm";
    	}
    
    	@GetMapping("/files/{filename:.+}")
    	// https://blog.csdn.net/tongxin_tongmeng/article/details/128406009
    	@ResponseBody
    	public ResponseEntityserveFile(@PathVariable String filename) {
    
    		Resource file = storageService.loadAsResource(filename);
    		return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
    				"attachment; filename=\"" + file.getFilename() + "\"").body(file);
    	}
    
    	@PostMapping("/")
    	public String handleFileUpload(@RequestParam("file") MultipartFile file,
    			RedirectAttributes redirectAttributes) {
    
    		storageService.store(file);
    		// https://blog.csdn.net/tongxin_tongmeng/article/details/128406963
    		redirectAttributes.addFlashAttribute("message",
    				"You successfully uploaded " + file.getOriginalFilename() + "!");
    
    		return "redirect:/";
    	}
    
    	// https://blog.csdn.net/tongxin_tongmeng/article/details/128406477
    	@ExceptionHandler(StorageFileNotFoundException.class)
    	public ResponseEntityhandleStorageFileNotFound(StorageFileNotFoundException exc) {
    		return ResponseEntity.notFound().build();
    	}
    
    }
  4. 项目演示

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享文章:SpringBoot文件上传(官方案例)-创新互联
当前URL:http://bjjierui.cn/article/dihegi.html

其他资讯