RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
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文件上传(官方案例)-创新互联
文章出自:http://lswzjz.com/article/dihegi.html