在win10本地开发springboot项目能上传图片,并能通过URL直接从浏览器访问,但是部署到服务器上后能上传文件,但是通过浏览器无法访问图片
生活随笔
收集整理的這篇文章主要介紹了
在win10本地开发springboot项目能上传图片,并能通过URL直接从浏览器访问,但是部署到服务器上后能上传文件,但是通过浏览器无法访问图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在win10本地開發springboot項目能上傳圖片,并能通過URL直接從瀏覽器訪問,但是部署到服務器上后能上傳文件,但是通過瀏覽器無法訪問圖片
1.首先springboot項目在Window和Linux服務器的項目資源路徑是不一樣的,需要分開來設置路徑:
@Override@Transactional(readOnly = false, rollbackFor = Exception.class)public String uploadImage(MultipartFile image, Comment comment, String rootUrl) throws Exception {if (comment == null) {return null;}String imgRequestUrl = null;if (!image.isEmpty()){File imagePath; //圖片存放地址//獲取文件名稱String imageName = image.getOriginalFilename();String os = System.getProperty("os.name");if (os.toLowerCase().startsWith("win")) { //windows系統String path = System.getProperty("user.dir"); //獲取項目相對路徑imagePath = new File(path+file_path);} else {//linux系統//獲取根目錄//如果是在本地windows環境下,目錄為項目的target\classes下//如果是linux環境下,目錄為jar包同級目錄File rootPath = new File(ResourceUtils.getURL("classpath:").getPath());if (!rootPath.exists()) {rootPath = new File("");}imagePath = new File(rootPath.getAbsolutePath()+file_path);}if (!imagePath.exists()) {//不存在,創建imagePath.mkdirs();}//使用工具類生成一個隨機的文件名防止文件名重復String newImageName = IDUtils.getFilename(imageName);//創建圖片存放地址File imageResultFile = new File(imagePath + "/" + newImageName);imgRequestUrl = rootUrl + "/images/" + newImageName;if (imageResultFile.exists()) {log.info("圖片已經存在!該圖片的訪問請求地址:[{}]", imgRequestUrl);} else {//圖片尚未存在,將圖片保存到指定的路徑中image.transferTo(imageResultFile);}//將該圖片的地址設置到comment中comment.setCommentImagePath(imgRequestUrl);//圖片在磁盤中的實際地址 // String imageResultPath = imageResultFile.getCanonicalPath();log.info("該圖片的訪問請求地址:[{}]", imgRequestUrl);}//設置commentcomment.setIsDeleted(Comment.DEFAULT_ISDELETED_FALSE);comment.setStatus(Comment.DEFAULT_STATUS_TRUE);comment.setCollection_count(Long.valueOf(0L));comment.setLike_count(Long.valueOf(0L));int fla = commentMapper.insert(comment);if (fla == 1){//插入成功返回圖片的地址return imgRequestUrl;}else{//插入失敗返回NULLreturn null;}}2.然后配置一個WebMvcConfigurer用于解析靜態資源圖片,將服務器的靜態資源目錄通過映射到暴露的訪問路徑:
@Configuration public class FileConfig implements WebMvcConfigurer {@Value("${images.url-path}")private String file_path ;@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {//在windows環境下的圖片存放資源路徑String winPath = System.getProperty("user.dir")+file_path;//在Linux環境下的圖片存放資源路徑 // String linuxPath = "/usr/local/my_project/images/";String os = System.getProperty("os.name");if (os.toLowerCase().startsWith("win")) { //windows系統System.out.println(winPath);//第一個方法設置訪問路徑前綴,第二個方法設置資源路徑registry.addResourceHandler("/images/**").addResourceLocations("file:"+winPath);}else{//linux系統File rootPath = null;try {rootPath = new File(ResourceUtils.getURL("classpath:").getPath());} catch (FileNotFoundException e) {e.printStackTrace();}if(!rootPath.exists()){rootPath = new File("");}System.out.println(rootPath.getAbsolutePath()+file_path);File imagePath = new File(rootPath.getAbsolutePath()+file_path);if(!imagePath.exists()){//不存在,創建imagePath.mkdirs();}registry.addResourceHandler("/images/**").addResourceLocations("file:"+rootPath.getAbsolutePath()+file_path);}} }- addResoureHandler:指的是對外暴露的訪問路徑
- addResourceLocations:指的是內部文件放置的目錄
3.本博文已同步到個人博客,如有需要請移步:
http://moyisuiying.com/index.php/javastudy/springboot/476.html
總結
以上是生活随笔為你收集整理的在win10本地开发springboot项目能上传图片,并能通过URL直接从浏览器访问,但是部署到服务器上后能上传文件,但是通过浏览器无法访问图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud Open Fe
- 下一篇: orz扫盲贴。。。