Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Upload and access springboot files

2025-04-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "uploading and accessing springboot files". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Upload public @ ResponseBody ApiResult upload (@ RequestParam ("file") MultipartFile file, @ RequestParam (value= "appId") String appId, @ RequestParam ("group") String group, @ RequestParam ("fileName") String originFileName) {try {String suffix = file.getOriginalFilename (). Substring (file.getOriginalFilename (). LastIndexOf (".") + 1). ToLowerCase () If (! allowUploadSuffixes.contains (suffix)) {throw new IotBaseException (9999, "this file type is not allowed to upload");} String fileKey = UUID.randomUUID (). ToString () + "." + suffix; FileSystemClient client = FileSystemClient.getClient (group); String url = client.upload (fileKey, file.getBytes (), appId); UploadFileEntity entity = new UploadFileEntity () Entity.setAppId (appId); entity.setGroupName (group); entity.setFileName (originFileName); entity.setFileUrl (url); entity.setMimeType (file.getContentType ()); entity.setProvider (client.getProvider (). Name ()); entity.setCreatedAt (new Date ()); uploadFileEntityMapper.insert (entity) Return new ApiResult (new UploadResult (url, originFileName));} catch (Exception e) {e.printStackTrace (); throw new IotBaseException (ExceptionCode.SYSTEM_ERROR.code, "upload failed");}} II. Visit

Add to the application.yml file

Spring: resources: static-location: classpath:/static/upload/

It seems that it doesn't matter if you don't fill in the above step.

Create configuration

@ Configurationpublic class WebConfig implements WebMvcConfigurer {@ Override public void addResourceHandlers (ResourceHandlerRegistry registry) {registry.addResourceHandler ("/ static/**"). AddResourceLocations ("classpath:/static/");} "upload and access to springboot files". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report