一個springboot專案, 沒有圖片服務器, 想通過 WebMvcConfigurer 的 addResourceHandler\ addResourceLocations方法將本地圖片進行映射, 然后路徑訪問, 但一直不成功, 如圖, 麻煩高人給看下



不知跟springboot版本有關嗎? 我的好像比較老 1.4.4.RELEASE ....
uj5u.com熱心網友回復:
你這個圖片靜態資源要放在classpath下面吧,你放到你的專案的resource檔案下下,然后修改下代碼registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
uj5u.com熱心網友回復:
感謝回復啊 我的意思是不把圖片放到專案里, 只是訪問我本地的圖片.
就類似這篇文章, https://blog.csdn.net/weixin_41413137/article/details/108269164#comments_13178827
uj5u.com熱心網友回復:
寫法沒什么問題,我估計是你這個專案有context-path,但你地址欄沒有(host:port/context-path/img/**)uj5u.com熱心網友回復:
感謝回復 context-path寫過 靜態資源路徑也寫過 但是現在注釋掉了呀..
我很費解..
另外 會是springboot版本的問題嗎? ?
uj5u.com熱心網友回復:
我按著他的寫了一下沒什么問題,我訪問到了,沒什么問題
配置類代碼,我基本沒這么改,一樣用的 /

你訪問路徑為什么是10.0.0.110:8090/img/圖片名,你這個img是在yml檔案中配置的嗎?如果是應該是沒有什么問題的
uj5u.com熱心網友回復:
謝謝回復啊 你的springboot版本多少呢? 我打算換個版本試試...
uj5u.com熱心網友回復:
package cn.lml.lession.config;import java.util.Date;
import java.util.TimeZone;
import javax.annotation.PostConstruct;
import javax.servlet.MultipartConfigElement;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.util.Assert;
import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import cn.lml.lession.util.DateUtils;
import cn.lml.tools.encrypt.MessageDigestUtils;
@Configuration
public class ApplicationConfig implements WebMvcConfigurer {
@Autowired
private RequestMappingHandlerAdapter handlerAdapter;
@PostConstruct
public void addConversionConfig() {
ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter
.getWebBindingInitializer();
if (initializer.getConversionService() != null) {
GenericConversionService genericConversionService = (GenericConversionService) initializer
.getConversionService();
genericConversionService.addConverter(new StringToDateConverter());
}
TimeZone.setDefault(TimeZone.getTimeZone("GMT+8"));
}
public class StringToDateConverter implements Converter<String, Date> {
@Override
public Date convert(String dateString) {
Assert.hasText(dateString, "Null or emtpy date string");
Date date = DateUtils.parseDate(dateString);
if (date == null) {
String errMsg = String.format("Failed to convert [%s] to [%s] for value '%s'", String.class.toString(),
Date.class.toString(), dateString);
throw new IllegalArgumentException(errMsg);
}
return date;
}
}
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("forward:/index.html");
registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
}
/**
* 重寫父類提供的跨域請求處理的介面
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
// 添加映射路徑
registry.addMapping("/**")
// 放行哪些原始域
.allowedOrigins("*")
// 是否發送Cookie資訊
.allowCredentials(true)
// 放行哪些原始域(請求方式)
.allowedMethods("GET", "POST", "PUT", "OPTIONS", "DELETE", "PATCH")
// 放行哪些原始域(頭部資訊)
.allowedHeaders("*")
// 暴露哪些頭部資訊(因為跨域訪問默認不能獲取全部頭部資訊)
.exposedHeaders("Header1", "Header2");
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//registry.addResourceHandler("/MyResources/**").addResourceLocations("classpath:/static/");
//registry.addResourceHandler("/MyResources/**").addResourceLocations("file:E:/static/");
}
/**
* 限制上傳檔案大小
*/
/*
* @Bean public MultipartConfigElement multipartConfigElement(){
* MultipartConfigFactory factory = new MultipartConfigFactory(); //單個檔案最大 10m
* 可以使用讀取配置 factory.setMaxFileSize("10240KB"); //KB,MB /// 設定總上傳資料總大小 50m
* factory.setMaxRequestSize("512000KB"); return
* factory.createMultipartConfig(); }
*/
@Bean
public PasswordEncoder passwordEncoder() {
return new PasswordEncoder() {
MessageDigestUtils md5=MessageDigestUtils.newInstance(MessageDigestUtils.EncryptType.MD5);
@Override
public boolean matches(CharSequence rawPassword, String encodedPassword) {
return encodedPassword.equals(md5.getEncryptString(rawPassword.toString()));
}
@Override
public String encode(CharSequence rawPassword) {
return md5.getEncryptString(rawPassword.toString());
}
};
}
}
uj5u.com熱心網友回復:
親測可以的
uj5u.com熱心網友回復:
2.1.5的,直接pom檔案下載個jar包也不麻煩。我后面也嘗試改動了一些,沒有出現404,除非路徑對應不上uj5u.com熱心網友回復:
感謝回復 我新建了boot版本高的專案 也是可以了.. 可能就是版本問題...
uj5u.com熱心網友回復:
嗯 我新建了個版本高的boot專案 也可以訪問到圖了.. 可能就是版本問題了..
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/12785.html
標籤:Java EE
上一篇:javamail 發送郵件時,出現 java.net.SocketException: Connection reset
