国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

java基于spring boot本地上傳圖片示例解析

瀏覽:37日期:2023-08-26 11:45:53

前幾天項目中剛好需要上傳圖片的需求,當時想的是用七牛云,因為我用七牛云也用了好幾次,就是把圖片上傳到七牛云空間里面,數據庫里面保存的是這張上傳圖片的url地址 那么頁面訪問也就很方便,考慮到項目部署的環境我就用了本地上傳,不牽涉數據庫的操作。我就花了半個小時寫了個本地上傳圖片的小demo。非常的簡單。

下面是需要的依賴 pom.xml文件:

<?xml version='1.0' encoding='UTF-8'?> <project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.3.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>cn.com.sctic</groupId> <artifactId>upload</artifactId> <version>0.0.1-SNAPSHOT</version> <name>upload</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <dependencies> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId> </dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>runtime</scope> </dependency> <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope> </dependency> </dependencies> <build> <plugins><plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId></plugin> </plugins> </build> </project>

控制器: UploadController

@Controller public class UploadController { private Logger logger = LoggerFactory.getLogger(this.getClass()); @Value('${scitc.upload.src}') private String rootPath; @Value('${scitc.upload.host}') private String uploadhost; @RequestMapping(value = '/uploadFile',method = {RequestMethod.POST,RequestMethod.GET}) @ResponseBody public String uploadFile(MultipartFile file) {//文件的完整名稱,如spring.jpeg String filename = file.getOriginalFilename(); //文件名,如spring String name = filename.substring(0,filename.indexOf('.')); //文件后綴,如.jpeg String suffix = filename.substring(filename.lastIndexOf('.'));//創建年月文件夾 Calendar date = Calendar.getInstance(); File dateDirs = new File(date.get(Calendar.YEAR) + File.separator + (date.get(Calendar.MONTH)+1));//目標文件 File descFile = new File(rootPath+File.separator+dateDirs+File.separator+filename); int i = 1; //若文件存在重命名 String newFilename = filename; while(descFile.exists()) {newFilename = name+'('+i+')'+suffix;String parentPath = descFile.getParent();descFile = new File(parentPath+File.separator+newFilename);i++; } //判斷目標文件所在的目錄是否存在 if(!descFile.getParentFile().exists()) {//如果目標文件所在的目錄不存在,則創建父目錄descFile.getParentFile().mkdirs(); } //將內存中的數據寫入磁盤 try {file.transferTo(descFile); } catch (Exception e) {e.printStackTrace();logger.error('上傳失敗,cause:{}',e); } //完整的url String fileUrl = uploadhost + rootPath +dateDirs+ '/'+newFilename; return 'success:' + fileUrl; } }

注意:rootPath,uploadhost是可以通過application.properties或者application.yml進行配置的。

由于要對外部資源進行映射需要創建一個類繼承WebMvcConfigurationSupport這個適配器,下面是WebMvcConfigurer的這個配置類,代碼如下:

@Configuration public class WebMvcConfigurer extends WebMvcConfigurationSupport { @Value('${scitc.upload.src}') private String src; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler(src + '/**').addResourceLocations('file:' + src); } }

注意:這里的src也是從配置文件applicaiton.properties中得到了。

下面是application.properties配置:

server.port=8848 ##文件上傳config scitc.upload.host:127.0.0.1:${server.port} scitc.upload.src=/Users/jswzj/Desktop/uploads/ spring.servlet.multipart.maxFileSize=10MB spring.servlet.multipart.maxRequestSize=10MB server.port=8848 服務器的端口號 scitc.upload.host:服務器ip地址 + server.port scitc.upload.src:你要把用戶上傳的圖片上傳到那個位置**

最后我們訪問這個接口效果圖如下:

java基于spring boot本地上傳圖片示例解析

上傳成功后拿到這個url地址 粘貼到瀏覽器地址上就能訪問了

java基于spring boot本地上傳圖片示例解析

總結:圖片上傳有很多的方式,當然這個是根據業務的需求,很多人都喜歡把圖片的url上傳到數據庫中,用實體類來對圖片的高度、寬度、名稱、url進行封裝,我覺得如果你部署的那臺服務器是有網絡的環境下建議用七牛云上傳,七牛云上傳把圖片保存到七牛云空間,那個url地址是不會發生變化的,不會應為你項目的遷移或者服務器地址發生變化而受影響??锤髯缘男枨蟀?。等有時間我會出一個七牛云上傳的demo讓大家學習。最后謝謝大家的支持,希望大家每天都要收獲。祝大家早日成為大神。

下面是這個demo的github的地址,希望大家fork,start一下,謝謝

https://github.com/zhoubiao188/springboot-upload

到此這篇關于java基于spring boot本地上傳圖片示例解析的文章就介紹到這了,更多相關spring boot本地上傳圖片內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 欧美国产日本 | 免费人成在线观看播放国产 | 国产精品一区二区丝瓜 | 国产成人一区二区三区视频免费 | 成熟女人免费一级毛片 | 九九精品视频在线观看 | 经典三级久久久久 | 欧美成人免费全部色播 | 日韩一区国产二区欧美三区 | 亚洲系列在线 | 亚洲综合精品一二三区在线 | 久久香蕉国产线看观看精品yw | 国产亚洲精品久久久久91网站 | 亚洲成人在线播放 | 亚洲精品精品 | 日韩毛片免费视频一级特黄 | 草草影院国产第一页 | 欧美一级毛片aaaaa | 99视频精品在线 | 亚洲性久久 | 久久国产a | a级毛片毛片免费很很综合 a级毛片免费 | 国产免费一级高清淫曰本片 | 热re91久久精品国产91热 | 中文字幕在线观看一区二区三区 | 中国一级特黄剌激爽毛片 | 一级片在线观看 | 99国产高清久久久久久网站 | 国产www | 中文字幕乱码中文乱码综合 | 国产精品亚洲综合 | 波多野结衣视频免费观看 | 999久久| 亚洲欧美日韩久久一区 | 一区二区三区日韩 | 日本免费不卡在线一区二区三区 | 欧美精品一区二区三区免费 | 欧美白人和黑人xxxx猛交视频 | 美国免费一级片 | 久久国产精品久久精品国产 | 一级毛片aaa |